php 错误:文件已加密或不是数据库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1513849/
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 02:53:33  来源:igfitidea点击:

Error: file is encrypted or is not a database

phpsqlite

提问by Verrtex

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

我用 PHP 创建了一个带有表的数据库。我是通过以下方式做到的:

<?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:

从命令行执行我的 PHP 文件后:“php test.php”我在我的目录中得到一个名为“test.db”的新文件(这就是我想要的)。然后,在命令行中,我输入“sqlite3 test.db”。这样我就进入了sqlite命令行会话。然后,使用 sqlite3,我输入“.tables”(我想检查一个新数据库是否包含它应该包含的表)。结果我得到:

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.

所以,它不起作用。有人知道这个问题吗?预先感谢您的任何帮助。

回答by togo31

This is a version mismatch issue.

这是一个版本不匹配问题。

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

要使用PHP5和SQLite打开一个数据库,我们需要使用PDO和没有sqlite_open()功能。如何打开或创建数据库的示例:

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 ....";

回答by Ted Xu

I faced the same problem, use pdo instead of sqlite_open()

我遇到了同样的问题,使用 pdo 而不是 sqlite_open()

this linkis very helpful and here is my code snippet, works perfectly, hope it helps

此链接非常有用,这是我的代码片段,效果很好,希望对您有所帮助

$dir = 'sqlite:YourPath/DBName.db';
$dbh  = new PDO($dir) or die("cannot open the database");
$query =  "SELECT * from dummy_table";
foreach ($dbh->query($query) as $row)
{
    echo $row[0];
}

回答by fin

this is most likely an version mismatch between the php sqlite version and your standalone sqlite executable.

这很可能是 php sqlite 版本和您的独立 sqlite 可执行文件之间的版本不匹配。

see this: http://us3.php.net/manual/en/book.sqlite.php- under "user contributed notes", from Andrew Paul Dickey.

看到这个:http: //us3.php.net/manual/en/book.sqlite.php- 在“用户贡献的笔记”下,来自 Andrew Paul Dickey。

for a quick solution you can install and use the sqlite2 standalone executable.

要获得快速解决方案,您可以安装和使用 sqlite2 独立可执行文件。

回答by ridgerunner

I recently encountered this exact same problem and have figured out what is going on. (Yes all the other answers are correct - it is a version mismatch problem.) A am posting this to provide some additional information that may be helpful to others encountering this problem...

我最近遇到了这个完全相同的问题,并且已经弄清楚发生了什么。(是的,所有其他答案都是正确的 - 这是一个版本不匹配问题。)我发布此信息以提供一些可能对遇到此问题的其他人有所帮助的其他信息......

Summary:

概括:

The error is due to the fact that the sqlite3.execommand line tool (which implements SQLite version 3), cannot read database files created using PHP's procedural interface to SQLite (which implements SQlite version 2).

该错误是由于sqlite3.exe命令行工具(实现 SQLite 版本 3)无法读取使用 PHP 的 SQLite 过程接口(实现 SQlite 版本 2)创建的数据库文件。

Discussion:

讨论:

I am following a tutorial which describes how to use SQLITE with PHP: SQLite PHP tutorial(Note that I am running PHP 5.2.14 on Windows XP). As it turns out PHP 5.2 has two (incompatible) ways of interfacing with the SQLite database management system; a procedural API (SQLite) and an object oriented API (SQLite Functions (PDO_SQLITE)). The procedural API uses SQLite version 2 and the OOP API uses SQLite version 3. For Windows PHP platforms, the procedural API is enabled by uncommenting this line in php.ini:

我正在关注一个介绍如何将 SQLITE 与 PHP 结合使用的教程SQLite PHP 教程(请注意,我在 Windows XP 上运行的是 PHP 5.2.14)。事实证明,PHP 5.2 有两种(不兼容的)与 SQLite 数据库管理系统交互的方式;一个过程 API ( SQLite) 和一个面向对象的 API ( SQLite 函数 (PDO_SQLITE))。过程 API 使用 SQLite 版本 2,OOP API 使用 SQLite 版本 3。对于 Windows PHP 平台,过程 API 通过取消注释以下行来启用php.ini

extension=php_sqlite.dll

extension=php_sqlite.dll

While the OOP API is enabled by uncommenting these lines:

虽然通过取消注释这些行来启用 OOP API:

extension=php_pdo.dll
extension=php_pdo_sqlite.dll

extension=php_pdo.dll
extension=php_pdo_sqlite.dll

Note that there is a free Win32 tool that allows administration and manipulation of any version of SQLite databases: SQLite Administrator. This tool allows one to convert a version 2 database (created with the procedural API of PHP) into a version 3 database that can be read using the sqlite3.execommand line tool.

请注意,有一个免费的 Win32 工具允许管理和操作任何版本的 SQLite 数据库:SQLite Administrator。该工具允许将版本 2 数据库(使用 PHP 的过程 API 创建)转换为可以使用sqlite3.exe命令行工具读取的版本 3 数据库。

回答by Martin Vseticka

The question is two years old but now it can be solved this way:

这个问题已经两年了,但现在可以通过这种方式解决:

class MyDB extends SQLite3
{
    function __construct()
    {
            $dbFile = __DIR__ . '/../../../adminer/Dictionary.sqlite';
            $this->open($dbFile);
    }
}

$db = new MyDB();
$db->exec('CREATE TABLE students (names VARCHAR(80))');
echo "done";

Source: http://php.net/manual/en/sqlite3.open.php

来源:http: //php.net/manual/en/sqlite3.open.php

回答by svens

Why do you open the db two times ?

你为什么要打开数据库两次?

Try this code:

试试这个代码:

<?php
$db = sqlite_open( "test.db", 066, $err );
sqlite_query( $db, "CREATE TABLE students (names VARCHAR(80))" );
sqlite_close( $db );
?>

Edit: Fin is probably right; maybe you have to check the SQLite version with phpinfo().

编辑:Fin 可能是对的;也许您必须使用 phpinfo() 检查 SQLite 版本。

回答by Centurion

There's one more simple way to solve that problem. You can convert sqlite 2 database file to sqlite 3. I did that with SQLiteManager program on Mac OS X.

还有一种更简单的方法可以解决这个问题。您可以将 sqlite 2 数据库文件转换为 sqlite 3。我在 Mac OS X 上使用 SQLiteManager 程序做到了这一点。