赞
踩
最近在接着看<php经典实例>非常不错的一本书,内容挺丰富的,
//-------------关于PDO SQLITE的使用-----------------------------
$db = new PDO('sqlite:file_book');
//创建表并自动插入数据
$db->beginTransaction();
//试着查找名称为zodiac的表
//$q = $db->query("select name from sqlite_master where type='table' and name='zodiac'");
$q = $db->query("select * from zodiac");
//如果查询中没有返回结果行,就创建这个表并插入数据
while($row= $q->fetch()){
$result[] = $row;
}
print_r($result);
if($q->fetch()===false){//exit('s');
$db->exec("
CREATE TABLE zodiac(
id INT UNSIGNED NOT NULL,
sign CHAR(11),
symbol CHAR(13),
PRIMARY KEY(id)
)");
//独立的SQL语句
$sql = "INSERT INTO zodiac VALUES(1,'ARIES','MARS');
INSERT INTO zodiac VALUES(2,'bill','gates');
INSERT INTO zodiac VALUES(3,'micle','jack');
INSERT INTO zodiac VALUES(4,'fuck','She');
INSERT INTO zodiac VALUES(5,'KKK','HEE');
INSERT INTO zodiac VALUES(6,'ATSES','MSF');";
//var_dump($sql);
//将SQL语句按行分割并逐一执行
foreach(explode("\n",trim($sql)) as $q){
$db->exec(trim($q));
}
$db->commit();
}else{
//什么也不做结束事务
$db->rollback();
}
//只适用于sqlite2版本,非PDO方式创建 sqlite_open() only supports SQLite2
if ($db = sqlite_open('php_change')) {
$result = sqlite_query($db,'select * from zodiac');
// $result = sqlite_query($db,"select * from zodiac");
var_dump(sqlite_fetch_array($result));
} else {
exit('ssdf');
die($sqliteerror);
}
//pdo标准也不支持sqlite2标准
$db = new PDO('sqlite:3file_book');
//创建表并自动插入数据
$db->beginTransaction();
//试着查找名称为zodiac的表
//$q = $db->query("select name from sqlite_master where type='table' and name='zodiac'");
$q = $db->query("select * from zodiac");
//如果查询中没有返回结果行,就创建这个表并插入数据
while($row= $q->fetch()){
$result[] = $row;
}
print_r($result);
*/
//适用于sqlite3版本,非PDO方式访问
//SQLite3::open('3file_book'); 坑爹的,文档上是这种写法结果结果照着做说是open为非静态方法,非静态就不要这样写了,这种写法大家一开始看的时候肯定会这么认为的
class MyDB extends SQLite3
{
function __construct()
{
$this->open('3file_book');
}
}
$db = new MyDB();
$result = $db->query('select * from zodiac');
// $result = sqlite_query($db,"select * from zodiac");
var_dump($result->fetchArray());
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。