Sqlite China  
首页 | 各种语言的sqlite编程 |sqlite研究 | sqlite应用实例与杂谈 | sqlite相关下载 | SQlite论坛
当前位置 : 主页>各种语言的sqlite编程>列表
在php中使用sqlite
来源:工友 作者:工友 时间:2007-12-21

连接 sqlite 数据库
 $ php -r ‘dl("sqlite.so"); \
    $error="";\
    if($db=sqlite_open("sqlite_test.db",0666,$error)){\
         sqlite_close($db);\
         echo "sqlite closed!\n";\
   }else{echo $error;};’
  # 如果指定的sqlite数据库不存在,则创建之


使用sqlite

dl("sqlite.so");
$db_file = "test.db";
$db = sqlite_open($db_file,0666,$con_error); #con_error is return!
if($db === false){
    die($con_error);
}
//$query_string = "select * from sqlite_master ";
//query($db,"CREATE TABLE test_table ( MyColumnA INTEGER PRIMARY KEY, MyColumnB TEXT(32) );");
query($db,"select * from sqlite_master ");
query($db,"select * from test_table ");
query($db,’INSERT INTO test_table (MyColumnB) VALUES ("’.md5(microtime()).’");’);
sqlite_close($db);

 function query($db,$qs){
     $result = sqlite_query($db, $qs);
    if($error=sqlite_last_error($db)){
        $error_str=sqlite_error_string($error);
    }
     $changes=sqlite_changes($db);
     $last_insert_id=sqlite_last_insert_rowid($db);
     if(is_resource($result)){
         $num_fields=sqlite_num_fields($result);
         $fns=array();
         for($i=0;$i<$num_fields;$i++){
             $fns[]=sqlite_field_name($result,$i);
         }
         $rs=array();
         $result = sqlite_fetch_all($result, SQLITE_ASSOC);
         foreach ($result as $row) {
             $r=array();
             foreach($fns as $f){
                $r[$f]=$row[$f];
            }
            $rs[]=$r;
         }

         echo $qs,"\n",$changes,"\n",$last_insert_id,"\n",$num_fields,"\n";
         print_r($fns);
         print_r($rs);
     }else{
         echo $qs,"\n",$changes,"\n",$last_insert_id,"\n",$num_fields,"\n",$result,"\n";
     }
 }
?>

 通过ez_sql使用sql_lite

 下载 ez_sql 包 download ez_sql.zip

 拷贝 ez_sql 包到 应用ez_sql的目录

 由于我的sqlite不是默认安装的 所以在 ez_sql_sqlite.php 中添加了    

    if(!function_exists(’sqlite_open’)){
        dl(’sqlite.so’);
    }
 手动导入sqlite扩展的代码

 没有发现sqlite的 close代码 所以安全起见 添加之

        function close()
        {
            if($this->dbh){
                sqlite_close($this->dbh);
            }
        }

 在 ezSQL_sqlite 类定义中添加 显示关闭连接的代码

    // Include ezSQL core
    include_once "ez_sql_core.php";
    // Include ezSQL database specific component
    include_once "ez_sql_sqlite.php";
    // Initialise database object and establish a connection
    // at the same time - db_path / db_name
    $db = new ezSQL_sqlite(’./’,'test.db’);
    // Create a table..
    $db->query("CREATE TABLE test_table2 ( MyColumnA INTEGER PRIMARY KEY, MyColumnB TEXT(32) );");

    // Insert test data
    for($i=0;$i<3;++$i)
    {
        echo $db->query(’INSERT INTO test_table2 (MyColumnB) VALUES ("’.md5(microtime()).’");’);
    }
   
    // Get list of tables from current database..
    $my_tables = $db->get_results("SELECT * FROM sqlite_master WHERE sql NOTNULL;");
    // Print out last query and results..
    $db->debug();

    // Loop through each row of results..
    foreach ( $my_tables as $table )
    {
        // Get results of DESC table..
        $result=$db->get_results("SELECT * FROM $table->name;");
        print_r($result);
        // Print out last query and results..
        //$db->debug();
    }

    // Get rid of the table we created..
    $db->query("DROP TABLE test_table2;");
?>

常用方法

bool     $db->query(query)
var       $db->get_var(query)
mixed $db->get_row(query)
mixed $db->get_results(query)

php的开源的sql操作封装包
http://www.jvmultimedia.com/portal/node/6 download ez_sql.zip
参考 http://justinvincent.com/docs/ezsql/ez_sql_help.htm
sqlite 参考
http://sqlite.org/lang.html
http://sqlite.org/quickstart.html
php sqlite 参考
http://www.php.net/manual/zh/ref.sqlite.php

(阅读次数:
上一篇:创建连接数据库SQLite 3.x的Rails网络应用软件 下一篇:php5_sqlite使用举例
[收藏] [推荐] [评论(0条)] [返回顶部] [打印本页] [关闭窗口]
用户名: 新注册) 密码: 匿名评论
评论内容:(不能超过250字,需审核后才会公布,请自觉遵守互联网相关政策法规。
§最新评论
热点文章
·简单的在Java中使用SQLite
·C/C++中调用SQLITE3的基本
·PHP+SQLITE制作简单的视频
·DISQLite3 简介 SQLite de
·VB.NET 中使用 SQLite3 的
·SQLiteJDBC 100%纯JAVA的s
·如何在Windows下编译SQLit
·关于SQLite的一些简单介绍
·在VC6.0中使用C++访问sqli
·C/C++中调用SQLITE3的基本
·SQLite 与 PHP 结合开发(
·在VC6.0中使用C++访问sqli
·使用SQLite进行网站搜索
·SQLite ADO.NET 驱动(C#
·在.NET C#中使用sqlite
·python模块之sqlite数据库
·PHP中的SQlite数据库应用
·如何在PHP5中通过PDO连接S
·PHP中如何使用sqlite_crea
·SQLite 、 PHP混合扩展编
相关文章
·让你的PHP4也用上Sqlite3
·VB.NET 中使用 SQLite3 的
·C/C++中调用SQLITE3的基本
·python模块之sqlite数据库
·在.NET C#中使用sqlite
·用Ruby进行SQLite的开发指
·PHP5中的 sqlite_create_f
·SQLite ADO.NET 驱动(C#
·使用SQLite进行网站搜索
·如何在Windows下编译SQLit

版权Power by DedeCms   后台登陆
Copyright @ 2007