如何使用 PHP 连接到 SQLite 数据库?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16728265/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 11:36:47  来源:igfitidea点击:

How do I connect to an SQLite database with PHP?

phpsqlite

提问by user2412936

I have an SQLite database and am trying to connect to it with PHP. This is what I'm using:

我有一个 SQLite 数据库,正在尝试使用 PHP 连接到它。这是我正在使用的:

<?php
    $dbconn = sqlite_open('combadd.sqlite');

    if ($dbconn) {
        $result = sqlite_query($dbconn,  "SELECT * FROM combo_calcs WHERE options='easy'");
        var_dump(sqlite_fetch_array($result, SQLITE_ASSOC));
    } else {
        print "Connection to database failed!\n";
    }
?>

However, I get this error:

但是,我收到此错误:

Warning: sqlite_open()[function.sqlite-open]: file is encrypted or is not a database in C:\xampp\htdocs\deepthi\combadd\combadd_db.phpon line 4
Connection to database failed!

警告:sqlite_open()[function.sqlite-open]:文件已加密或不是C:\xampp\htdocs\deepthi\combadd\combadd_db.php第 4 行中的数据库
连接到数据库失败!

What's wrong and how can I fix it?

出了什么问题,我该如何解决?

回答by Antonio Carlos Ribeiro

Try to use PDO instead of sqlite_open:

尝试使用 PDO 而不是 sqlite_open:

$dir = 'sqlite:/[YOUR-PATH]/combadd.sqlite';
$dbh  = new PDO($dir) or die("cannot open the database");
$query =  "SELECT * FROM combo_calcs WHERE options='easy'";
foreach ($dbh->query($query) as $row)
{
    echo $row[0];
}
$dbh = null; //This is how you close a PDO connection

回答by Yogus

Connecting To DatabaseFollowing PHP code shows how to connect to an existing database. If database does not exist, then it will be created and finally a database object will be returned.

连接到数据库以下 PHP 代码显示了如何连接到现有数据库。如果数据库不存在,则将创建它并最终返回一个数据库对象。

<?php
   class MyDB extends SQLite3
   {
      function __construct()
      {
         $this->open('combadd.sqlite');
      }
   }
   $db = new MyDB();
   if(!$db){
      echo $db->lastErrorMsg();
   } else {
      echo "Opened database successfully\n";
   }
?>

Now let's run above program to create our database test.db in the current directory. You can change your path as per your requirement. If database is successfully created then it will give following message:

现在让我们运行上面的程序在当前目录中创建我们的数据库 test.db。您可以根据需要更改路径。如果数据库创建成功,那么它会给出以下消息:

Open database successfully

SELECT Operation

选择操作

Following PHP program shows how we can fetch and display records

以下 PHP 程序展示了我们如何获取和显示记录

<?php
   class MyDB extends SQLite3
   {
      function __construct()
      {
         $this->open('combadd.sqlite');
      }
   }
   $db = new MyDB();
   if(!$db){
      echo $db->lastErrorMsg();
   } else {
      echo "Opened database successfully\n";
   }

   $sql =<<<EOF
      SELECT * FROM combo_calcs WHERE options='easy';
EOF;

   $ret = $db->query($sql);
   while($row = $ret->fetchArray(SQLITE3_ASSOC) ){
      echo "ID = ". $row['ID'] . "\n";
   }
   echo "Operation done successfully\n";
   $db->close();
?>

回答by justnajm

<?php

    if ($db = sqlite_open('sampleDB', 0666, $sqliteerror) ) { 
        $result = sqlite_query($db, 'select bar from foo');
        var_dump(sqlite_fetch_array($result) ); 
    } else {
        die($sqliteerror);
    }

?>

Make sure sqlite support is enable, check phpinfo()

确保启用了 sqlite 支持,检查 phpinfo()

One another solution to your problem is: Using sqlite3 module instead

您的问题的另一种解决方案是:改用 sqlite3 模块

class DB extends SQLite3
{
        function __construct( $file )
        {
            $this->open( $file );
        }
}

$db = new DB( 'sampleDB.sqlite' );