SQLITE3在php中的运用
php中操作sqlite3数据库的类文件。
一般用法:
$filepath = "/apps/www/htdocs/databasename" ; $db = new SPSQLite3( $filepath ); //打开此路径数据库文件 $sql = "select * from tablename" ; //查询记录 $sql2 = "delete from tablename" ; //删除表内所有记录 $db ->exec_sql( $sql ); //执行查询语句 $db ->struct_sql( $sql2 ); //执行删除 $result = $db ->get_all_data(); //返回操作记录集合 $numbers = count ( $result ); //得到有效记录数 $fields = $result [0][ "field" ]; //取字段内容 $db ->close(); //关闭 |
需要在装入的头文件中加入:@dl('sqlite3.so');调用动态链接库(linux中)
class SPSQLite3 { var $db_name ; //连接的sqlite数据库服务器的用户密码 var $db_link ; //数据库连接的句柄 var $result ; //执行sqlite语句的资源句柄 var $insert_autoid = 0; //增加记录时自动编号的最后一个ID号 var $get_data = array (); //获取记录内容 var $get_all_data = array (); //获取所有记录内容 var $num_rows = 0; //执行SELECT语句时获取了多少条记录 var $affected_rows = 0; //执行除SELECT语句所影响的记录行数 var $error = "" ; function SPSQLite3( $db_name = "sqlite" ) { $this ->db_name = $db_name ; $this ->open_db( $db_name ); //打开数据库 } //构造函数 function __destruct() { $this ->free(); $this ->close(); } //释放查询值 function free() { if ( $this ->result) { unset( $this ->result); } } //关闭数据库连接 function close() { if ( $this ->db_link) { $this ->free(); return @sqlite3_close( $this ->db_link); } } //严重错误时停执行 function halt( $msg ) { $this ->free(); $this ->close(); //调试函数 print (" 程序遇到一个严重而至命的错误,以至停止执行! 错误描述:{ $msg } "); exit (1); } //打开数据连接,并选择数据库 function open_db( $db_name = "sqlite" ) { if ( $db_name == "" ) { $db_name = $this ->db_name; } if ( $db_name == "" ) { $this ->halt( "未能找到指定要操作的数据库名:{$db_name}!" ); } $this ->db_link = @sqlite3_open( $db_name ) or die ( "数据库连接失败" ); if ( $this ->db_link) { return $this ->db_link; } else { $this ->halt( "您所要操作的文件不能写,权限不足!" ); } } //返回sqlite连接句柄 function db_link() { return $this ->db_link; } //执行对数据查询操作的SQL语句 function exec_sql( $sql_cmd ) { $this ->result = @sqlite3_query( $this ->db_link, $sql_cmd ); return $this ->result; } //执对数据库操作(create,update,delete,insert)的SQL语句 function struct_sql( $sql_cmd ) { $this ->result = @sqlite3_exec( $this ->db_link, $sql_cmd ); return $this ->result; } //获取所有数据内容存入数组中 function get_data() { if (! $this ->result) { //$this->halt("没有记录可获取!"); return false; } $this ->get_data = @sqlite3_fetch_array( $this ->result); if (! is_array ( $this ->get_data)) { $this ->get_data = null; } return $this ->get_data; } //获取所有数据内容存入数组中 function get_all_data() { if (! $this ->result) { //$this->halt("没有记录可获取!"); return false; } $this ->get_all_data = array (); while ( $row = @sqlite3_fetch_array( $this ->result)) { $this ->get_all_data[] = $row ; } return $this ->get_all_data; } //记录操作返回数 function num_rows() { if (! $this ->result) { //$this->halt("没有记录可获取!"); return false; } //禁用num_rows函数.. //$this->num_rows = sqlite3_changes($this->result); $this ->num_rows = 0; return $this ->num_rows; } //seek函数 function seek( $nu ) { if (! $this ->result) { //$this->halt("没有记录可获取!"); return false; } $seek_result = @sqlite_seek(! $this ->result, $nu ); if ( $seek_result ) { return $seek_result ; } else { $this ->halt( "数据记录移动范围超出了记录!" ); } } //获取最后增加记录的自动编号 function insert_autoid() { $this ->insert_autoid = @sqlite3_last_insert_rowid( $this ->db_link); return $this ->insert_autoid; } //执行除SELECT语句所影响的记录行数 function affected_rows() { $this ->affected_rows = sqlite3_changes( $this ->db_link); return $this ->affected_rows; } //创建数据库函数 function create_db( $db_name ) { if ( $db_name == "" ) { return false; } $str = "~!@#$$%^&*(_+=|[{:;" '?/.,<>}])"; for ( $i = 0; $i < strlen ( $str ); $i ++) { if ( eregi ( $str { $i }, $db_name )) { return false; } } return ( $this ->struct_sql( "CREATE DATABASE {$db_name} ;" )) ? true : false; } //删除数据库 function drop_db( $db_name ) { if ( $db_name == "" ) { return false; } $str = "~!@#$$%^&*(_+=|[{:;" '?/.,<>}])"; for ( $i = 0; $i < strlen ( $str ); $i ++) { if ( eregi ( $str { $i }, $db_name )) { return false; } } return ( $this ->struct_sql( "DROP DATABASE {$db_name} ;" )) ? true : false; } //清空数据表 function truncate_table( $t_name ) { if ( $t_name == "" ) { return false; } $str = "~!@#$$%^&*(_+=|[{:;" '?/.,<>}])"; for ( $i = 0; $i < strlen ( $str ); $i ++) { if ( eregi ( $str { $i }, $t_name )) { return false; } } return ( $this ->struct_sql( "DELETE FROM {$t_name} ;" )) ? true : false; } //获取最后数据库出错信息 function get_last_error() { $error = array (); $error [ "number" ] = sqlite3_error( $this ->db_link); $error [0] = $error [ "number" ]; $error [ "string" ] = sqlite3_error( $error [0]); $error [1] = $error [ "string" ]; return $error ; } } ?> |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)