<?php //自 PHP 5.3.0 起默认启用 SQLite3 扩展。可以在编译时使用 --without-sqlite3 禁用 SQLite3 扩展。Windows 用户必须启用 php_sqlite3.dll 才能使用该扩展。自 PHP 5.3.0 起,这个 DLL 被包含在 PHP 的 Windows 分发版中。 //1.连接 class MyDB extends SQLite3{ function __construct() { $this->open('data.db');//要用全路径,防止其他目录找不到 } } $db = new MyDB(); if(!$db){ echo $db->lastErrorMsg(); } else { echo "Opened database successfully\n"; } //2.创建表或插入、更新与删除 $sql =<<<EOF CREATE TABLE COMPANY (ID INT PRIMARY KEY NOT NULL, NAME TEXT NOT NULL, AGE INT NOT NULL, ADDRESS CHAR(50), SALARY REAL); EOF; $ret = $db->exec($sql); if(!$ret){ echo $db->lastErrorMsg(); } else { echo "Table created successfully\n"; //echo $db->changes(), " Record deleted successfully\n";//最近一次的 SQL 语句更新或插入或删除的数据库行数。 } $db->close(); //插入、更新时与创建表,只是sql语句不同,其它都一样。如下: $sql =<<<EOF INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) VALUES (1, 'Paul', 32, 'California', 20000.00 ); INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) VALUES (2, 'Allen', 25, 'Texas', 15000.00 ); INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) VALUES (3, 'Teddy', 23, 'Norway', 20000.00 ); INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) VALUES (4, 'Mark', 25, 'Rich-Mond ', 65000.00 ); EOF; $sql =<<<EOF UPDATE COMPANY set SALARY = 25000.00 where ID=1; EOF; $sql =<<<EOF DELETE from COMPANY where ID=2; EOF; //3.查询 $sql =<<<EOF SELECT * from COMPANY; EOF; $ret = $db->query($sql); while($row = $ret->fetchArray(SQLITE3_ASSOC) ){ echo "ID = ". $row['ID'] . "\n"; echo "NAME = ". $row['NAME'] ."\n"; echo "ADDRESS = ". $row['ADDRESS'] ."\n"; echo "SALARY = ".$row['SALARY'] ."\n\n"; } echo "Operation done successfully\n"; $db->close(); //其他函数,SQLite3::escapeString ( string $value )该例程返回一个字符串,在 SQL 语句中,出于安全考虑,该字符串已被正确地转义。
php操作sqlite3数据库类及增删除改查操作
阅读:3950 输入:2021-04-20 14:15:29