Error: file is encrypted or is not a database

2019-01-07 19:27发布

I used PHP to create a database with a table. I did it in the following way:

<?php
$db = new SQLiteDatabase("test.db");
unset($db);
$db = sqlite_open("test.db");
sqlite_query($db,"create table students (names char(255))");
sqlite_close($db);
?>

After I execute my PHP file from the command line: "php test.php" I get a new file in my directory which is called "test.db" (this is what I wanted). Than, in the command line, I type "sqlite3 test.db". In this way I enter in the sqlite command line session. Then, withing sqlite3, I type ".tables" (I wanted to check if a new database contains tables which it is supposed to contain). As the result I get:

Error: file is encrypted or is not a database 

So, it does not work. Does anybody know something about this problem? Thank you in advance for any help.

7条回答
地球回转人心会变
2楼-- · 2019-01-07 19:58

This is a version mismatch issue.

To open a database using PHP5 and SQLite we need to use a PDO and not the sqlite_open() function. An example of how to open or create a database:

try 
{
    /*** connect to SQLite database ***/

    $dbh = new PDO("sqlite:VPN0.sqlite");
    echo "Handle has been created ...... <br><br>";

}
catch(PDOException $e)
{
    echo $e->getMessage();
    echo "<br><br>Database -- NOT -- loaded successfully .. ";
    die( "<br><br>Query Closed !!! $error");
}

echo "Database loaded successfully ....";
查看更多
登录 后发表回答