关于SQLITE的使用

前端之家收集整理的这篇文章主要介绍了关于SQLITE的使用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

最近在接着看<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());

原文链接:https://www.f2er.com/sqlite/202053.html

猜你在找的Sqlite相关文章