SQLite lightweight wrapper


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  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. }
  54. }
  55.  
  56.  
  57. Class ResultSet {
  58. private $rs;
  59.  
  60. function __construct($rs) {
  61. $this->rs = $rs;
  62. }
  63.  
  64. function isValidRow() {
  65. return sqlite_valid($this->rs);
  66. }
  67.  
  68. function next() {
  69. return sqlite_next($this->rs);
  70. }
  71.  
  72. function fieldCount() {
  73. $row = sqlite_current($this->rs);
  74.  
  75. return (count($row) / 2);
  76. }
  77.  
  78. function fieldName($fieldIndex) {
  79. return sqlite_field_name($this->rs, $fieldIndex);
  80. }
  81.  
  82. function field($fieldIndex) {
  83. $row = sqlite_current($this->rs);
  84.  
  85. return stripslashes(htmlspecialchars_decode($row[$fieldIndex]));
  86. }
  87. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.