sqlite 数据库连接类

sqlite 数据库连接类就是利用了php 与sqlite进行连接操作,代码如下:

  1. */
  2. lass db_class {
  3. var $conn=null;
  4. var $querynum = 0;
  5. /**
  6. * 数据库连接,返回数据库连接标识符
  7. *
  8. * @param string $ 数据库服务器主机
  9. * @param string $ 数据库服务器帐号
  10. * @param string $ 数据库服务器密码
  11. * @param string $ 数据库名
  12. * @param bool $ 是否保持持续连接,1为持续连接,0为非持续连接
  13. * @return link_identifier $dbuser, $dbpw, $dbname,
  14. */
  15. function connect($dbhost, $pconnect = 0) {
  16. $error = '';
  17. $func = $pconnect == 1 ? 'sqlite_popen' : 'sqlite_open';
  18. if (!$this -> conn = $func($dbhost, 0666, $error)) {
  19. $this -> halt($error);
  20. }
  21. return $this -> conn;
  22. }
  23. /**
  24. * 执行sql语句
  25. *
  26. * @param string $ sql语句
  27. * @param string $ 默认为空,可选值为 cache unbuffered
  28. * @param int $ cache以秒为单位的生命周期
  29. * @return resource
  30. */
  31. function query($sql , $type = '' , $expires = 3600, $dbname = '') {
  32. $error = '';
  33. $func = $type == 'unbuffered' ? 'sqlite_unbuffered_query' : 'sqlite_query';
  34. if (preg_match("/^s*select/i", $sql)) {
  35. $query = $func($this -> conn, $sql, sqlite_assoc, $error);
  36. } else {
  37. $query = sqlite_exec($this -> conn, $sql, $error);
  38. }
  39. if ($error) {
  40. $this -> halt($error, $sql);
  41. }
  42. $this -> querynum++;
  43. return $query;
  44. }
  45. /*
  46. *@param string $ table名
  47. *@param string $ where条件
  48. *@param string $ colum名
  49. *@param string $ limit数量
  50. */
  51. function getlist($table , $wheres = "1=1", $colums = '*' ,$limits = '3000',$orderbys="id desc") {
  52. $query = $this -> query("select ".$colums." from ".$table." where ".$wheres." order by ".$orderbys." limit ".$limits, $type, $expires, $dbname);
  53. while($rs = $this -> fetch_array($query)){
  54. $datas[]=$rs;
  55. }
  56. //print_r("select ".$colums." from ".$table." where ".$wheres." limit ".$limits);
  57. //print_r($rs);die();
  58. $this -> free_result($query);
  59. return $datas ;
  60. }
  61. function add_one($table , $colums ,$data ) {
  62. //die("insert into ".$table." (".$colums.") values(".$data.")");
  63. $query = $this -> query("insert into ".$table." (".$colums.") values(".$data.")", $type, $expires, $dbname);
  64. //return $this->insert_id();
  65. return $query;
  66. }
  67. function delist($table , $idarray,$wheres="no") {
  68. if($wheres=='no')
  69. $query = $this -> query("delete from ".$table." where id in(".$idarray.")", $type, $expires, $dbname);
  70. else
  71. $query = $this -> query("delete from ".$table." where ".$wheres, $type, $expires, $dbname);
  72. return $query;
  73. }
  74. function updatelist($table , $updatedata,$idarray) {
  75. $query = $this -> query("update ".$table." set ". $updatedata." where id in(".$idarray.")", $type, $expires, $dbname);
  76. return $query;
  77. }
  78. //update max_vote set maxtitle='$title',maxban='$ban',
  79. /**
  80. * 执行sql语句,只得到一条记录
  81. *
  82. * @param string $ sql语句
  83. * @param string $ 默认为空,可选值为 cache unbuffered
  84. * @param int $ cache以秒为单位的生命周期
  85. * @return array
  86. */
  87. function get_one($sql, $type = '', $expires = 3600, $dbname = '') {
  88. $query = $this -> query($sql, $type, $expires, $dbname);
  89. $rs = $this -> fetch_array($query);
  90. $this -> free_result($query);
  91. return $rs ;
  92. }
  93. /**
  94. * 从结果集中取得一行作为关联数组
  95. *
  96. * @param resource $ 数据库查询结果资源
  97. * @param string $ 定义返回类型
  98. * @return array
  99. */
  100. function fetch_array($query, $result_type = sqlite_assoc) {
  101. return sqlite_fetch_array($query, $result_type);
  102. }
  103. /**
  104. * 取得前一次 sqlite操作所影响的记录行数
  105. *
  106. * @return int
  107. */
  108. function affected_rows() {
  109. return sqlite_changes($this -> conn);
  110. }
  111. /**
  112. * 取得结果集中行的数目
  113. *
  114. * @return int
  115. */
  116. function num_rows($query) {
  117. return sqlite_num_rows($query);
  118. }
  119. /**
  120. * 返回结果集中字段的数目
  121. *
  122. * @return int
  123. */
  124. function num_fields($query) {
  125. return sqlite_num_fields($query);
  126. }
  127. /**
  128. *
  129. * @return array 备用,一般不用.
  130. */
  131. function result($query, $row) {
  132. return @sqlite_fetch_all($query, sqlite_assoc);
  133. }
  134. /**
  135. * sqlite没有相应函数
  136. */
  137. function free_result($query) {
  138. return ;
  139. }
  140. /**
  141. * 取得上一步 insert 操作产生的 id
  142. *
  143. * @return int
  144. */
  145. function insert_id() {
  146. return sqlite_last_insert_rowid($this -> connid);
  147. }
  148. /**
  149. *
  150. * @return array 只得到数字索引
  151. */
  152. function fetch_row($query) {
  153. return sqlite_fetch_array($query, sqlite_num);
  154. }
  155. /**
  156. */
  157. function fetch_assoc($query) {
  158. return $this -> fetch_array($query, sqlite_assoc);
  159. }
  160. /**
  161. *
  162. * @return string
  163. */
  164. function version() {
  165. return sqlite_libversion();
  166. }
  167. function close() {
  168. return sqlite_close($this -> conn);
  169. }
  170. /**
  171. *
  172. * @return string
  173. */
  174. function error() {
  175. return sqlite_error_string($this -> errno);
  176. }
  177. /**
  178. *
  179. * @return int
  180. */
  181. function errno() {
  182. return sqlite_last_error($this -> conn);
  183. }
  184. /**
  185. * 显示mysql教程错误信息
  186. */
  187. function halt($message = '', $sql = '') {
  188. exit("sqlitequery:$sql <br> sqliteerror:" . $this -> error() . " <br> sqliteerrno:" . $this -> errno() . " <br> message:$message");
  189. } //开源代码phpfensi.com