PHP连接SQLite数据库
1.创建数据库
<?php class SQLiteDB extends SQLite3 { function __construct() { $this->open('phpdb.db'); } } $db = new SQLiteDB(); if(!$db){ echo $db->lastErrorMsg(); } else { echo "Yes, Opened database successfully\n"; } ?>
2.创建表
<?php class SQLiteDB extends SQLite3 { function __construct() { $this->open('phpdb.db'); } } $db = new SQLiteDB(); if(!$db){ echo $db->lastErrorMsg(); } else { echo "Yes, Opened database successfully<br/>\n"; } $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 "Yes, Table created successfully<br/>\n"; } $db->close();
3.插入数据
<?php class SQLiteDB extends SQLite3 { function __construct() { $this->open('phpdb.db'); } } $db = new SQLiteDB(); if(!$db){ echo $db->lastErrorMsg(); } else { echo "Yes, Opened database successfully<br/>\n"; } // 先删除后创建表 $sql = "DROP table company"; $ret = $db->exec($sql); // 创建表语句 $sql =<<<EOF CREATE TABLE if not exists 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 "Yes, Table created successfully<br/>\n"; } // $db->close(); $sql =<<<EOF INSERT INTO company (ID,NAME,AGE,ADDRESS,SALARY) VALUES (1, 'Maxsu', 26, 'Haikou', 20000.00 ); INSERT INTO company (ID,NAME,AGE,ADDRESS,SALARY) VALUES (2, 'Allen', 25, 'Guangzhou', 15000.00 ); INSERT INTO company (ID,NAME,AGE,ADDRESS,SALARY) VALUES (3, 'Tenny', 23, 'Shanghai', 20000.00 ); INSERT INTO company (ID,NAME,AGE,ADDRESS,SALARY) VALUES (4, 'Weiwang', 25, 'Beijing ', 65000.00 ); EOF; $ret = $db->exec($sql); if(!$ret){ echo $db->lastErrorMsg(); } else { echo "Yes, Some Records has Inserted successfully<br/>\n"; } $db->close();
4.查询数据
<?php class SQLiteDB extends SQLite3 { function __construct() { $this->open('phpdb.db'); } } $db = new SQLiteDB(); if(!$db){ echo $db->lastErrorMsg(); } else { echo "Yes, Opened database successfully<br/>\n"; } // 先删除后创建表 $sql = "DROP table company"; $ret = $db->exec($sql); // 创建表 $sql =<<<EOF CREATE TABLE if not exists 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 "Yes, Table created successfully<br/>\n"; } // $db->close(); $sql =<<<EOF INSERT INTO company (ID,NAME,AGE,ADDRESS,SALARY) VALUES (1, 'Maxsu', 26, 'Haikou', 20000.00 ); INSERT INTO company (ID,NAME,AGE,ADDRESS,SALARY) VALUES (2, 'Allen', 25, 'Guangzhou', 15000.00 ); INSERT INTO company (ID,NAME,AGE,ADDRESS,SALARY) VALUES (3, 'Tenny', 23, 'Shanghai', 20000.00 ); INSERT INTO company (ID,NAME,AGE,ADDRESS,SALARY) VALUES (4, 'Weiwang', 25, 'Beijing ', 65000.00 ); EOF; $ret = $db->exec($sql); if(!$ret){ echo $db->lastErrorMsg(); } else { echo "Yes, Some Records has Inserted successfully<br/>\n"; } // 查询表中的数据 echo "<b> Select Data from company table :</b><hr/>"; $sql =<<<EOF SELECT * from COMPANY; EOF; $ret = $db->query($sql); while($row = $ret->fetchArray(SQLITE3_ASSOC) ){ echo "ID = ". $row['ID'] . "<br/>\n"; echo "NAME = ". $row['NAME'] ."<br/>\n"; echo "ADDRESS = ". $row['ADDRESS'] ."<br/>\n"; echo "SALARY = ".$row['SALARY'] ."<br/>\n\n"; echo '----------------------------------<br/>'; } echo "Operation done successfully\n"; $db->close();
5.更新数据
<?php class SQLiteDB extends SQLite3 { function __construct() { $this->open('phpdb.db'); } } $db = new SQLiteDB(); if(!$db){ echo $db->lastErrorMsg(); } else { echo "Yes, Opened database successfully<br/>\n"; } // 先删除后创建表 $sql = "DROP table company"; $ret = $db->exec($sql); // 创建表 $sql =<<<EOF CREATE TABLE if not exists 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 "Yes, Table created successfully<br/>\n"; } // $db->close(); $sql =<<<EOF INSERT INTO company (ID,NAME,AGE,ADDRESS,SALARY) VALUES (1, 'Maxsu', 26, 'Haikou', 20000.00 ); INSERT INTO company (ID,NAME,AGE,ADDRESS,SALARY) VALUES (2, 'Allen', 25, 'Guangzhou', 15000.00 ); INSERT INTO company (ID,NAME,AGE,ADDRESS,SALARY) VALUES (3, 'Tenny', 23, 'Shanghai', 20000.00 ); INSERT INTO company (ID,NAME,AGE,ADDRESS,SALARY) VALUES (4, 'Weiwang', 25, 'Beijing ', 65000.00 ); EOF; $ret = $db->exec($sql); if(!$ret){ echo $db->lastErrorMsg(); } else { echo "Yes, Some Records has Inserted successfully<br/>\n"; } // 更新ID=1的薪水为:29999 $sql = 'UPDATE COMPANY set SALARY = 29999.00 where ID=1'; $ret = $db->exec($sql); if(!$ret){ echo $db->lastErrorMsg(); } else { echo $db->changes(), " Record(ID=1) updated successfully<br/>\n"; } // 查询表中的数据 echo "<b> Select Data from company table :</b><hr/>"; $sql =<<<EOF SELECT * from COMPANY; EOF; $ret = $db->query($sql); while($row = $ret->fetchArray(SQLITE3_ASSOC) ){ echo "ID = ". $row['ID'] . "<br/>\n"; echo "NAME = ". $row['NAME'] ."<br/>\n"; echo "ADDRESS = ". $row['ADDRESS'] ."<br/>\n"; echo "SALARY = ".$row['SALARY'] ."<br/>\n\n"; echo '----------------------------------<br/>'; } echo "Operation done successfully\n"; $db->close();
6.删除数据
<?php class SQLiteDB extends SQLite3 { function __construct() { $this->open('phpdb.db'); } } $db = new SQLiteDB(); if(!$db){ echo $db->lastErrorMsg(); } else { echo "Yes, Opened database successfully<br/>\n"; } // 先删除后创建表 $sql = "DROP table company"; $ret = $db->exec($sql); // 创建表 $sql =<<<EOF CREATE TABLE if not exists 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 "Yes, Table created successfully<br/>\n"; } // $db->close(); $sql =<<<EOF INSERT INTO company (ID,NAME,AGE,ADDRESS,SALARY) VALUES (1, 'Maxsu', 26, 'Haikou', 20000.00 ); INSERT INTO company (ID,NAME,AGE,ADDRESS,SALARY) VALUES (2, 'Allen', 25, 'Guangzhou', 15000.00 ); INSERT INTO company (ID,NAME,AGE,ADDRESS,SALARY) VALUES (3, 'Tenny', 23, 'Shanghai', 20000.00 ); INSERT INTO company (ID,NAME,AGE,ADDRESS,SALARY) VALUES (4, 'Weiwang', 25, 'Beijing ', 65000.00 ); EOF; $ret = $db->exec($sql); if(!$ret){ echo $db->lastErrorMsg(); } else { echo "Yes, Some Records has Inserted successfully<br/>\n"; } // 更新ID小于等于2的数据记录 $sql =<<<EOF DELETE from COMPANY where ID<=2; EOF; $ret = $db->exec($sql); if(!$ret){ echo $db->lastErrorMsg(); } else { echo $db->changes(), " Record(ID<=2) deleted successfully<br/>\n"; } // 查询表中的数据 echo "<b> Select Data from company table :</b><hr/>"; $sql =<<<EOF SELECT * from COMPANY; EOF; $ret = $db->query($sql); while($row = $ret->fetchArray(SQLITE3_ASSOC) ){ echo "ID = ". $row['ID'] . "<br/>\n"; echo "NAME = ". $row['NAME'] ."<br/>\n"; echo "ADDRESS = ". $row['ADDRESS'] ."<br/>\n"; echo "SALARY = ".$row['SALARY'] ."<br/>\n\n"; echo '----------------------------------<br/>'; } echo "Operation done successfully\n"; $db->close();
<完>