当前位置:数据库 > SQLite >>

在php中使用sqlite

答案:

连接 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.jv易做图.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

上一个:php5_sqlite使用举例
下一个:创建连接数据库SQLite 3.x的Rails网络应用软件

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,