首页 >后端开发 >php教程 >php读取sqlite数据库入门实例_PHP教程

php读取sqlite数据库入门实例_PHP教程

WBOY
WBOY原创
2016-07-13 10:27:201034浏览

php读取sqlite数据库的例子,php编程中操作sqlite入门实例.
原文参考:http://www.jbxue.com/article/php/22383.html
在使用SQLite前,要确保php.ini中已经启用sqlite和pdo配置

打开PHP.INI文件,打下以下扩展:
extension=php_pdo.dll

extension=php_pdo_sqlite.dll
extension=php_sqlite.dll
sqlite_open命令是打开一个数据库文件。
如果没有文件则创建。

sqlite_query可以执行SQL语句。
创建一个表并插入数据。

sqlite_unbuffered_query发出SELECT语句。
循环并显示结果。

unable to open a temporary database file for storing temporary tables
无法打开存储临时表的临时数据库文件,在Windows环境中,如果出现上述错误,
请使用putenv("TMP=C:/temp");指定临时文件夹。

具体请看代码:

<?<span>php
</span><span>//</span><span>临时目录 在Windows环境中,如果出现上述错误,请使用putenv("TMP=C:/temp");指定临时文件夹。
//putenv("TMP=C:/temp");

//打开数据库 www.jbxue.com</span>
<span>if</span> (<span>$db</span> = <span>sqlite_open</span>("test.db",0666,<span>$sqliteerror</span><span>)) {

</span><span>//</span><span>创建表</span>
<span>sqlite_query</span>(<span>$db</span>, "create table user(id integer primary key,name text);"<span>);

</span><span>//</span><span>INSERT语句</span>
<span>$sql</span> = "insert into user values(NULL, '名字')"<span>;

</span><span>//</span><span>执行SQL语句</span>
<span>$res</span> = <span>sqlite_query</span>(<span>$db</span>, <span>$sql</span><span>);

</span><span>//</span><span>SELECT语句</span>
<span>$sql</span> = "select * from user order by id desc limit 20"<span>;

</span><span>//</span><span>执行SQL语句</span>
<span>$res</span> = <span>sqlite_unbuffered_query</span>(<span>$db</span>, <span>$sql</span><span>);

</span><span>//</span><span>显示结果</span>
<span>while</span> (<span>$item</span> = <span>sqlite_fetch_array</span>(<span>$res</span>,<span> SQLITE_ASSOC)) {
</span><span>print</span> "ID:".<span>$item</span>["id"] ."NAME:".<span>$item</span>["name"<span>];
</span><span>print</span> "<BR>"<span>;
};

</span><span>//</span><span>关闭数据库</span>
<span>sqlite_close</span>(<span>$db</span><span>);

} </span><span>else</span><span> {
</span><span>print</span> <span>$sqliteerror</span><span>;
}
</span>?>

PHP+SQLite数据库操作教程与实例

<?<span>php
</span><span>//</span><span>设置脚本最大执行时间</span>
<span>set_time_limit</span>(0<span>);
</span><span>//</span><span>sqlite数据库文件名</span>
<span>$db_name</span> = 'md5.db'<span>;
</span><span>//</span><span>打开sqlite数据库</span>
<span>$db</span> = <span>sqlite_open</span>(<span>$db_name</span><span>);
</span><span>//</span><span>异常处理</span>
<span>if</span>( !<span>$db</span><span> ) {
</span><span>echo</span> '不能连接SQlite文件:',<span>$db_name</span>,'<br />'<span>;
}</span><span>else</span><span>{
</span><span>echo</span> '成功连接SQlite文件:',<span>$db_name</span>,'<br />'<span>;
}
</span><span>//</span><span>创建数据表:MD5密码表</span>
<span>sqlite_query</span>(<span>$db</span>, "CREATE TABLE md5 (s int(4) PRIMARY KEY,d varchar(32))"<span>);
</span><span>//</span><span>插入记录</span>
<span>$s</span> = 0<span>;
</span><span>while</span>(<span>$s</span> <= 999999<span>){
</span><span>$d</span> = <span>md5</span>(<span>$s</span><span>);
</span><span>sqlite_query</span>(<span>$db</span>, "INSERT INTO md5 VALUES (<span>$s</span>,'{<span>$d</span>}')"<span>);
</span><span>$s</span>++<span>;
}
</span><span>//</span><span>检索所有记录</span>
<span>$result</span> = <span>sqlite_query</span>(<span>$db</span>, 'SELECT * FROM md5'<span>);
</span><span>echo</span> '<pre class="brush:php;toolbar:false">'<span>;
</span><span>while</span> (<span>$row</span> = <span>sqlite_fetch_array</span>(<span>$result</span>,<span> SQLITE_BOTH)) {
</span><span>echo</span> 'Md5:',<span>$row</span>['d'],' Src:',<span>$row</span>['s'], '<br />'<span>;
}
</span><span>echo</span> '
'; //关闭SQLite连接 sqlite_close($db); ?>

php读取sqlite入门版

<?<span>php
</span><span>//</span><span>打开sqlite数据库
//$db = @sqlite_open("MM.sqlite", 0666, $error); // 不支持
//$db = new PDO('sqlite:MM.sqlite');
//异常处理</span>
<span>if</span> (!<span>$db</span>) <span>die</span>("Connection Sqlite failed.\n"<span>);
</span><span>//</span><span>添加一个叫做foo的数据库
//@sqlite_query($db, "CREATE TABLE foo (bar varchar(10))");
//插入一条记录
//@sqlite_query($db, "INSERT INTO foo VALUES ('fnord')");
//检索所有记录</span>
<span>$result</span> = <span>$db</span>->query('select BottleEncryptUsrName from BottleTable4'<span>);
</span><span>//</span><span>打印获取的结果</span>
<span>foreach</span>(<span>$result</span> <span>as</span> <span>$row</span><span>){
</span><span>echo</span> <span>$row</span>[0<span>];
</span><span>echo</span> "<br>"<span>;
}
</span>?>

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/818150.htmlTechArticlephp读取sqlite数据库的例子,php编程中操作sqlite入门实例. 原文参考:http://www.jbxue.com/article/php/22383.html 在使用SQLite前,要确保php.ini中已经启...
声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn