Your Ad Here

Posted By

Keef on 04/13/09


Tagged


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

nkrstevski
aleksanderek


SQLite lightweight wrapper


 / Published in: PHP
 

  1. /*
  2. * Description: Very simple and lightweight SQLite wrapper class, it also has some SQL injection protection.
  3. * License: Competely public domain with no warranty whatsoever.
  4. */
  5.  
  6. Class SQLiteDB {
  7. private $db;
  8. private $debug = true;
  9. var $argChar = '?';
  10.  
  11. // For debugging purposes.
  12. var $statement;
  13. var $old_statements = array();
  14.  
  15. function __construct($filename = null) {
  16. $this->open($filename);
  17. }
  18.  
  19. function open($filename = null) {
  20. // If no file is specified create a temporary table in memory.
  21. if (empty($filename))
  22. $filename = ':memory:';
  23.  
  24. if (!$this->db = sqlite_open($filename, 0666, $sqliteerror))
  25. die($sqliteerror);
  26. }
  27.  
  28. function execute($statement, $arguments = null) {
  29. if (!empty($arguments)) {
  30. for ($i = 0; $i < count($arguments); $i++) {
  31. $arguments[$i] = "'". addslashes(htmlspecialchars($arguments[$i], ENT_QUOTES)). "'";
  32. }
  33.  
  34. $statement = vsprintf(str_replace($this->argChar, '%s', $statement), $arguments);
  35. }
  36.  
  37. // For debugging purposes.
  38. if ($debug) {
  39. $this->statement = $statement;
  40. $this->old_statements[] = $statement;
  41. }
  42.  
  43. $rs = sqlite_query($this->db, $this->statement);
  44.  
  45. return new ResultSet($rs);
  46. }
  47.  
  48. function close() {
  49. sqlite_close($this->db);
  50. }
  51.  
  52. function lastInsertRowId() {
  53. return sqlite_last_insert_rowid($db);
  54. }
  55. }
  56.  
  57.  
  58. Class ResultSet {
  59. private $rs;
  60.  
  61. function __construct($rs) {
  62. $this->rs = $rs;
  63. }
  64.  
  65. function isValidRow() {
  66. return sqlite_valid($this->rs);
  67. }
  68.  
  69. function next() {
  70. return sqlite_next($this->rs);
  71. }
  72.  
  73. function fieldCount() {
  74. $row = sqlite_current($this->rs);
  75.  
  76. return (count($row) / 2);
  77. }
  78.  
  79. function fieldName($fieldIndex) {
  80. return sqlite_field_name($this->rs, $fieldIndex);
  81. }
  82.  
  83. function field($fieldIndex) {
  84. $row = sqlite_current($this->rs);
  85.  
  86. return stripslashes(htmlspecialchars_decode($row[$fieldIndex]));
  87. }
  88. }

Report this snippet  

You need to login to post a comment.