发布于 2015-07-25 11:41:33 | 345 次阅读 | 评论: 0 | 来源: 网络整理
Windows用户必须启用php_sqlite3.dll为了使用这个扩展。这个DLL包含与Windows分派PHP 如 PHP 5.3.0。
有关详细的安装说明,请查看我们的PHP教程和其官方网站上。
以下是重要的PHP程序,就足够完成工作要求从PHP程序使用SQLite数据库。如果要找一个更复杂的应用程序,那么你可以看看到PHP官方文件。
S.N. | API & 描述 |
---|---|
1 | public void SQLite3::open ( filename, flags, encryption_key )
打开一个SQLite3数据库。如果构建包括加密,然后它会试图使用密钥。 If the filename is given as ':memory:', SQLite3::open() will create an in-memory database in RAM that lasts only for the duration of the session. If filename is actual device file name, SQLite3::open() attempts to open the database file by using its value. If no file by that name exists then a new database file by that name gets created. Optional flags used to determine how to open the SQLite database. By default, open uses SQLITE3_OPEN_READWRITE | SQLITE3_OPEN_CREATE. |
2 | public bool SQLite3::exec ( string $query )
This routine provides a quick, easy way to execute SQL commands provided by sql argument which can consist of more than one SQL command. This routine is used to execute a result-less query against a given database. |
3 | public SQLite3Result SQLite3::query ( string $query )
This routine executes an SQL query, returning an SQLite3Result object if the query returns results. |
4 | public int SQLite3::lastErrorCode ( void )
This routine returns the numeric result code of the most recent failed SQLite request |
5 | public string SQLite3::lastErrorMsg ( void )
This routine returns english text describing the most recent failed SQLite request. |
6 | public int SQLite3::changes ( void )
This routine returns the number of database rows that were updated or inserted or deleted by the most recent SQL statement |
7 | public bool SQLite3::close ( void )
This routine closes a database connection previously opened by a call to SQLite3::open(). |
8 | public string SQLite3::escapeString ( string $value )
This routine returns a string that has been properly escaped for safe inclusion in an SQL statement. |
下面的PHP代码显示了如何连接到一个现有的数据库。如果数据库不存在,那么它就会被创建,终于将返回一个数据库对象。
<?php
class MyDB extends SQLite3
{
function __construct()
{
$this->open('test.db');
}
}
$db = new MyDB();
if(!$db){
echo $db->lastErrorMsg();
} else {
echo "Opened database successfullyn";
}
?>
现在,让我们运行上面的程序在当前目录中创建我们的数据库test.db。按要求可以改变你的路径。如果数据库创建成功,则它会给下面的消息:
Open database successfully
下面的PHP程序将使用以前创建的数据库中创建一个表:
<?php
class MyDB extends SQLite3
{
function __construct()
{
$this->open('test.db');
}
}
$db = new MyDB();
if(!$db){
echo $db->lastErrorMsg();
} else {
echo "Opened database successfullyn";
}
$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 successfullyn";
}
$db->close();
?>
上述程序执行时,它会创建表COMPANYtest.db的,它会显示以下消息:
Opened database successfully
Table created successfully
PHP程序,显示我们如何能够创造我们的COMPANY在上面的例子中创建表中的记录:
<?php
class MyDB extends SQLite3
{
function __construct()
{
$this->open('test.db');
}
}
$db = new MyDB();
if(!$db){
echo $db->lastErrorMsg();
} else {
echo "Opened database successfullyn";
}
$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;
$ret = $db->exec($sql);
if(!$ret){
echo $db->lastErrorMsg();
} else {
echo "Records created successfullyn";
}
$db->close();
?>
上述程序执行时,它会创建COMPANY表中的记录,并显示以下两行:
Opened database successfully
Records created successfully
PHP程序表明我们如何能够获取并显示COMPANY 表在上面的例子中创建表的记录:
<?php
class MyDB extends SQLite3
{
function __construct()
{
$this->open('test.db');
}
}
$db = new MyDB();
if(!$db){
echo $db->lastErrorMsg();
} else {
echo "Opened database successfullyn";
}
$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'] ."nn";
}
echo "Operation done successfullyn";
$db->close();
?>
当上述程序执行时,它会产生以下结果:
Opened database successfully
ID = 1
NAME = Paul
ADDRESS = California
SALARY = 20000
ID = 2
NAME = Allen
ADDRESS = Texas
SALARY = 15000
ID = 3
NAME = Teddy
ADDRESS = Norway
SALARY = 20000
ID = 4
NAME = Mark
ADDRESS = Rich-Mond
SALARY = 65000
Operation done successfully
下面的PHP代码显示了如何我们可以使用UPDATE语句来更新任何记录,然后获取并显示更新的记录,从COMPANY 表:
<?php
class MyDB extends SQLite3
{
function __construct()
{
$this->open('test.db');
}
}
$db = new MyDB();
if(!$db){
echo $db->lastErrorMsg();
} else {
echo "Opened database successfullyn";
}
$sql =<<<EOF
UPDATE COMPANY set SALARY = 25000.00 where ID=1;
EOF;
$ret = $db->exec($sql);
if(!$ret){
echo $db->lastErrorMsg();
} else {
echo $db->changes(), " Record updated successfullyn";
}
$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'] ."nn";
}
echo "Operation done successfullyn";
$db->close();
?>
当上述程序执行时,它会产生以下结果:
Opened database successfully
1 Record updated successfully
ID = 1
NAME = Paul
ADDRESS = California
SALARY = 25000
ID = 2
NAME = Allen
ADDRESS = Texas
SALARY = 15000
ID = 3
NAME = Teddy
ADDRESS = Norway
SALARY = 20000
ID = 4
NAME = Mark
ADDRESS = Rich-Mond
SALARY = 65000
Operation done successfully
下面的PHP代码,表明我们如何能够使用DELETE语句删除任何记录,然后获取并显示剩余的记录COMPANY 表:
<?php
class MyDB extends SQLite3
{
function __construct()
{
$this->open('test.db');
}
}
$db = new MyDB();
if(!$db){
echo $db->lastErrorMsg();
} else {
echo "Opened database successfullyn";
}
$sql =<<<EOF
DELETE from COMPANY where ID=2;
EOF;
$ret = $db->exec($sql);
if(!$ret){
echo $db->lastErrorMsg();
} else {
echo $db->changes(), " Record deleted successfullyn";
}
$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'] ."nn";
}
echo "Operation done successfullyn";
$db->close();
?>
当上述程序执行时,它会产生以下结果:
Opened database successfully
1 Record deleted successfully
ID = 1
NAME = Paul
ADDRESS = California
SALARY = 25000
ID = 3
NAME = Teddy
ADDRESS = Norway
SALARY = 20000
ID = 4
NAME = Mark
ADDRESS = Rich-Mond
SALARY = 65000
Operation done successfully