>  기사  >  백엔드 개발  >  PHP를 사용하여 구현된 SQLite 데이터베이스 연결 클래스

PHP를 사용하여 구현된 SQLite 데이터베이스 연결 클래스

怪我咯
怪我咯원래의
2017-07-04 14:13:222049검색

이 글은 SQLite 데이터베이스 연결, 추가, 삭제, 수정 및 기타 SQL 작업을 포함하는 PHP로 구현된 sqlite 데이터베이스 연결 클래스를 주로 소개합니다. 도움이 필요한 친구들이 참고할 수 있습니다. 이 기사의 예제에서는 PHP로 구현된 sqlite 데이터베이스 연결 클래스를 설명합니다. 참고할 수 있도록 모든 사람과 공유하세요. 구체적인 구현 방법은 다음과 같습니다.

sqlite 데이터베이스 연결 클래스는 php와 sqlite를 사용하여 연결 작업을 수행합니다.

코드는 다음과 같습니다.

*/ 
lass db_class { 
var $conn=null; 
var $querynum = 0; 
/** 
 * 数据库连接,返回数据库连接标识符 
 *  
 * @param string $ 数据库服务器主机 
 * @param string $ 数据库服务器帐号 
 * @param string $ 数据库服务器密码 
 * @param string $ 数据库名 
 * @param bool $ 是否保持持续连接,1为持续连接,0为非持续连接 
 * @return link_identifier $dbuser, $dbpw, $dbname, 
 */ 
function connect($dbhost, $pconnect = 0) { 
 $error = ''; 
 $func = $pconnect == 1 ? 'sqlite_popen' : 'sqlite_open'; 
 if (!$this -> conn = $func($dbhost, 0666, $error)) { 
  $this -> halt($error); 
 } 
 return $this -> conn; 
} 
/** 
 * 执行sql语句 
 *  
 * @param string $ sql语句 
 * @param string $ 默认为空,可选值为 cache unbuffered 
 * @param int $ cache以秒为单位的生命周期 
 * @return resource  
 */ 
function query($sql , $type = '' , $expires = 3600, $dbname = '') { 
 $error = ''; 
 $func = $type == 'unbuffered' ? 'sqlite_unbuffered_query' : 'sqlite_query'; 
 if (preg_match("/^s*select/i", $sql)) { 
  $query = $func($this -> conn, $sql, sqlite_assoc, $error); 
 } else { 
  $query = sqlite_exec($this -> conn, $sql, $error); 
 }  
 if ($error) { 
  $this -> halt($error, $sql); 
 } 
 $this -> querynum++; 
 return $query; 
}  
/* 
*@param string $ table名 
*@param string $ where条件 
*@param string $ colum名 
 
*@param string $ limit数量  
 
*/ 
function getlist($table , $wheres = "1=1",  $colums = '*' ,$limits = '3000',$orderbys="id desc") { 
 $query = $this -> query("select ".$colums." from ".$table." where ".$wheres." order by  ".$orderbys."  limit ".$limits, $type, $expires, $dbname); 
 while($rs = $this -> fetch_array($query)){ 
  $datas[]=$rs; 
  } 
 //print_r("select ".$colums." from ".$table." where ".$wheres." limit ".$limits); 
 //print_r($rs);die(); 
 $this -> free_result($query); 
 return $datas ; 
} 
function add_one($table , $colums ,$data ) { 
 //die("insert into ".$table." (".$colums.") values(".$data.")"); 
 $query = $this -> query("insert into ".$table." (".$colums.") values(".$data.")", $type, $expires, $dbname); 
 //return $this->insert_id(); 
 return $query; 
} 
function delist($table , $idarray,$wheres="no") { 
 if($wheres=='no') 
  $query = $this -> query("delete from ".$table." where id in(".$idarray.")", $type, $expires, $dbname); 
 else 
  $query = $this -> query("delete from ".$table." where ".$wheres, $type, $expires, $dbname); 
 return $query; 
} 
function updatelist($table , $updatedata,$idarray) { 
 $query = $this -> query("update ".$table." set ". $updatedata."  where id in(".$idarray.")", $type, $expires, $dbname); 
 return $query; 
} 
//update max_vote set maxtitle='$title',maxban='$ban', 
/** 
 * 执行sql语句,只得到一条记录 
 *  
 * @param string $ sql语句 
 * @param string $ 默认为空,可选值为 cache unbuffered 
 * @param int $ cache以秒为单位的生命周期 
 * @return array  
 */ 
function get_one($sql, $type = '', $expires = 3600, $dbname = '') { 
 $query = $this -> query($sql, $type, $expires, $dbname); 
 $rs = $this -> fetch_array($query); 
 $this -> free_result($query); 
 return $rs ; 
} 
/** 
 * 从结果集中取得一行作为关联数组 
 *  
 * @param resource $ 数据库查询结果资源 
 * @param string $ 定义返回类型 
 * @return array  
 */ 
function fetch_array($query, $result_type = sqlite_assoc) { 
 return sqlite_fetch_array($query, $result_type); 
} 
/** 
 * 取得前一次 sqlite操作所影响的记录行数 
 *  
 * @return int  
 */ 
function affected_rows() { 
 return sqlite_changes($this -> conn); 
} 
/** 
 * 取得结果集中行的数目 
 *  
 * @return int  
 */ 
function num_rows($query) { 
 return sqlite_num_rows($query); 
} 
/** 
 * 返回结果集中字段的数目 
 *  
 * @return int  
 */ 
function num_fields($query) { 
 return sqlite_num_fields($query); 
} 
/** 
 *  
 * @return array 备用,一般不用. 
 */ 
function result($query, $row) { 
 return @sqlite_fetch_all($query, sqlite_assoc); 
}  
/** 
 * sqlite没有相应函数 
 */ 
function free_result($query) { 
 return ; 
} 
/** 
 * 取得上一步 insert 操作产生的 id 
 *  
 * @return int  
 */ 
function insert_id() { 
 return sqlite_last_insert_rowid($this -> connid); 
} 
/** 
 *  
 * @return array 只得到数字索引 
 */ 
function fetch_row($query) { 
 return sqlite_fetch_array($query, sqlite_num); 
}  
/** 
 */ 
function fetch_assoc($query) { 
 return $this -> fetch_array($query, sqlite_assoc); 
}  
/** 
 *  
 * @return string  
 */ 
function version() { 
 return sqlite_libversion(); 
} 
function close() { 
 return sqlite_close($this -> conn); 
} 
/** 
 *  
 * @return string  
 */ 
function error() { 
 return sqlite_error_string($this -> errno); 
} 
/** 
 *  
 * @return int  
 */ 
function errno() { 
 return sqlite_last_error($this -> conn); 
} 
/** 
 * 显示mysql教程错误信息 
 */ 
function halt($message = '', $sql = '') { 
 exit("sqlitequery:$sql <br> sqliteerror:" . $this -> error() . " <br> sqliteerrno:" . $this -> errno() . " <br> message:$message"); 
}

위 내용은 PHP를 사용하여 구현된 SQLite 데이터베이스 연결 클래스의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.