Home  >  Article  >  Backend Development  >  About the use of sqlite3 in PHP

About the use of sqlite3 in PHP

藏色散人
藏色散人forward
2019-12-30 13:14:483134browse

SQLite is a lightweight relational database that can be embedded in our application and released together, so that we do not need additional database support when deploying the application.

If you want to use sqlite in php, you only need to enable the php_sqlite3.dll extension, which is very convenient. The following is a tool class that I wrote myself for operating a SQLite database in one of my previous projects.

<?php
 
class SQLiteDB extends SQLite3 {
function __construct(){
try {
$this->open(dirname(__FILE__).&#39;/../data/sqlite_ecloud.db&#39;);
}catch (Exception $e){
die($e->getMessage());
}
}
}
 
class DBUtils {
private static $db;
private static function instance(){
if (!self::$db) {
self::$db = new SQLiteDB();
}
}
 
/**
* 创建表
* @param string $sql
*/
public static function create($sql){
self::instance();
$result = @self::$db->query($sql);
if ($result) {
return true;
}
return false;
}
 
/**
* 执行增删改操作
* @param string $sql
*/
public static function execute($sql){
self::instance();
$result = @self::$db->exec($sql);
if ($result) {
return true;
}
return false;
}
 
/**
* 获取记录条数
* @param string $sql
* @return int
*/
public static function count($sql){
self::instance();
$result = @self::$db->querySingle($sql);
return $result ? $result : 0;
}
 
/**
* 查询单个字段
* @param string $sql
* @return void|string
*/
public static function querySingle($sql){
self::instance();
$result = @self::$db->querySingle($sql);
return $result ? $result : &#39;&#39;;
}
 
/**
* 查询单条记录
* @param string $sql
* @return array
*/
public static function queryRow($sql){
self::instance();
$result = @self::$db->querySingle($sql,true);
return $result;
}
 
/**
* 查询多条记录
* @param string $sql
* @return array
*/
public static function queryList($sql){
self::instance();
$result = array();
$ret = @self::$db->query($sql);
if (!$ret) {
return $result;
}
while($row = $ret->fetchArray(SQLITE3_ASSOC) ){
array_push($result, $row);
}
return $result;
}
}
 
?>

Calling method:

Introduce the DBUtils.php file, and then use the form DBUtils::method name to operate the SQLite database.

For more PHP related knowledge, please visit PHP Tutorial!

The above is the detailed content of About the use of sqlite3 in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete