About the use of sqlite3 in PHP

藏色散人
Release: 2023-04-08 06:52:02
forward
3126 people have browsed it

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;
}
}
 
?>
Copy after login

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!

Related labels:
php
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template