VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > PHP >
  • 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.  
  50. *@param string $ limit数量  
  51.  
  52. */ 
  53. function getlist($table , $wheres = "1=1",  $colums = '*' ,$limits = '3000',$orderbys="id desc") { 
  54.  $query = $this -> query("select ".$colums." from ".$table." where ".$wheres." order by  ".$orderbys."  limit ".$limits, $type, $expires, $dbname); 
  55.  while($rs = $this -> fetch_array($query)){ 
  56.   $datas[]=$rs; 
  57.   } 
  58.  //print_r("select ".$colums." from ".$table." where ".$wheres." limit ".$limits); 
  59.  //print_r($rs);die(); 
  60.  $this -> free_result($query); 
  61.  return $datas ; 
  62. } 
  63. function add_one($table , $colums ,$data ) { 
  64.  //die("insert into ".$table." (".$colums.") values(".$data.")"); 
  65.  $query = $this -> query("insert into ".$table." (".$colums.") values(".$data.")", $type, $expires, $dbname); 
  66.  //return $this->insert_id(); 
  67.  return $query; 
  68. } 
  69. function delist($table , $idarray,$wheres="no") { 
  70.  if($wheres=='no') 
  71.   $query = $this -> query("delete from ".$table." where id in(".$idarray.")", $type, $expires, $dbname); 
  72.  else 
  73.   $query = $this -> query("delete from ".$table." where ".$wheres, $type, $expires, $dbname); 
  74.  return $query; 
  75. } 
  76. function updatelist($table , $updatedata,$idarray) { 
  77.  $query = $this -> query("update ".$table." set ". $updatedata."  where id in(".$idarray.")", $type, $expires, $dbname); 
  78.  return $query; 
  79. } 
  80. //update max_vote set maxtitle='$title',maxban='$ban', 
  81. /** 
  82.  * 执行sql语句,只得到一条记录 
  83.  *  
  84.  * @param string $ sql语句 
  85.  * @param string $ 默认为空,可选值为 cache unbuffered 
  86.  * @param int $ cache以秒为单位的生命周期 
  87.  * @return array  
  88.  */ 
  89. function get_one($sql, $type = '', $expires = 3600, $dbname = '') { 
  90.  $query = $this -> query($sql, $type, $expires, $dbname); 
  91.  $rs = $this -> fetch_array($query); 
  92.  $this -> free_result($query); 
  93.  return $rs ; 
  94. } 
  95. /** 
  96.  * 从结果集中取得一行作为关联数组 
  97.  *  
  98.  * @param resource $ 数据库查询结果资源 
  99.  * @param string $ 定义返回类型 
  100.  * @return array  
  101.  */ 
  102. function fetch_array($query, $result_type = sqlite_assoc) { 
  103.  return sqlite_fetch_array($query, $result_type); 
  104. } 
  105. /** 
  106.  * 取得前一次 sqlite操作所影响的记录行数 
  107.  *  
  108.  * @return int  
  109.  */ 
  110. function affected_rows() { 
  111.  return sqlite_changes($this -> conn); 
  112. } 
  113. /** 
  114.  * 取得结果集中行的数目 
  115.  *  
  116.  * @return int  
  117.  */ 
  118. function num_rows($query) { 
  119.  return sqlite_num_rows($query); 
  120. } 
  121. /** 
  122.  * 返回结果集中字段的数目 
  123.  *  
  124.  * @return int  
  125.  */ 
  126. function num_fields($query) { 
  127.  return sqlite_num_fields($query); 
  128. } 
  129. /** 
  130.  *  
  131.  * @return array 备用,一般不用. 
  132.  */ 
  133. function result($query, $row) { 
  134.  return @sqlite_fetch_all($query, sqlite_assoc); 
  135. }  
  136. /** 
  137.  * sqlite没有相应函数 
  138.  */ 
  139. function free_result($query) { 
  140.  return ; 
  141. } 
  142. /** 
  143.  * 取得上一步 insert 操作产生的 id 
  144.  *  
  145.  * @return int  
  146.  */ 
  147. function insert_id() { 
  148.  return sqlite_last_insert_rowid($this -> connid); 
  149. } 
  150. /** 
  151.  *  
  152.  * @return array 只得到数字索引 
  153.  */ 
  154. function fetch_row($query) { 
  155.  return sqlite_fetch_array($query, sqlite_num); 
  156. }  
  157. /** 
  158.  */ 
  159. function fetch_assoc($query) { 
  160.  return $this -> fetch_array($query, sqlite_assoc); 
  161. }  
  162. /** 
  163.  *  
  164.  * @return string  
  165.  */ 
  166. function version() { 
  167.  return sqlite_libversion(); 
  168. } 
  169. function close() { 
  170.  return sqlite_close($this -> conn); 
  171. } 
  172. /** 
  173.  *  
  174.  * @return string  
  175.  */ 
  176. function error() { 
  177.  return sqlite_error_string($this -> errno); 
  178. } 
  179. /** 
  180.  *  
  181.  * @return int  
  182.  */ 
  183. function errno() { 
  184.  return sqlite_last_error($this -> conn); 
  185. } 
  186. /** 
  187.  * 显示mysql教程错误信息 
  188.  */ 
  189. function halt($message = '', $sql = '') { 
  190.  exit("sqlitequery:$sql <br> sqliteerror:" . $this -> error() . " <br> sqliteerrno:" . $this -> errno() . " <br> message:$message"); 
  191. } //开源代码phpfensi.com
  192.  

出处:http://www.phpfensi.com/php/20140911/5383.html


相关教程