sqlite无法打开数据库文件是加密的还是不是数据库?

我正在使用sqlite数据库的Windows应用程序.net 2.0,我的连接字符串保持在app.config之类

   

在连接字符串中我已将密码定义为“mypassword”,如果我删除此密码一切正常,但是当我使用密码子句时,它给出了connection.open()语法错误

 File opened that is not a database file file is encrypted or is not a database 

我在网上搜索并发现了一些版本问题,但我只使用版本3,因为我在连接字符串中说明我也尝试删除“版本= 3”但问题仍然是相同的。

我是第一次这样做,它的解决方案是什么?

当您在连接字符串中指定密码并且数据库已存在时,SQLite会假定数据库已加密并将尝试使用所述密码对其进行解密。 如果尚未在数据库上设置密码,则会导致“文件已加密”错误,因为提供的密码不能用于解密未加密的数据库。

您可以删除数据库,SQLite将使用连接字符串中的密码创建新的加密数据库。 或者,您可以使用ChangePassword()方法加密现有数据库:

 // Opens an unencrypted database SQLiteConnection cnn = new SQLiteConnection("Data Source=c:\\test.db3"); cnn.Open(); // Encrypts the database. The connection remains valid and usable afterwards. cnn.ChangePassword("mypassword"); 

参考: 加密,解密和附加到加密数据库

2Toad的回答大多是正确的,但我想添加自己的答案,因为有一些澄清要做。 正如2Toad所说,这是正确的:

当您在连接字符串中指定密码并且数据库已存在时,SQLite会假定数据库已加密并将尝试使用所述密码对其进行解密。 如果尚未在数据库上设置密码,则会导致“文件已加密”错误,因为提供的密码不能用于解密未加密的数据库。

但是,如果在连接字符串中已经有另一个之后尝试使用conn.SetPassword("something") ,也会发生此错误。 或者如果你做了conn.ChangePassword("somethingelse") ,但仍然在连接字符串中有Password=something

有几种情况需要考虑:

  1. 数据库已应用密码,并且位于连接字符串中。
  2. 您在连接字符串中有密码,但数据库没有应用密码,或者字符串中的密码与DB不匹配。
  3. 数据库从未有过密码,您想要更改密码。
  4. 数据库有密码,您想要更改它。

决议:

  1. 因此,为执行conn.ChangePassword("somethingelse")提供的代码2Toad只是半正确的,并没有考虑到你在哪里,你做了什么,以及你将来想做什么。 如果您有一个现有密码并且想要更改它,这是正确的,但您还必须确保之后更新连接字符串,否则后续连接将因file is encrypted错误而失败。

  2. 如果您使用conn.SetPassword("")密码然后尝试conn.ChangePassword("somethingelse")而没有首先连接到数据库而没有连接字符串中的Password=something ,则会出现这种情况。 那个Password=something必须从连接字符串中删除,因为密码已经从DB中以编程方式删除,DB将尝试连接该密码。 如果它没有从连接字符串中删除,同时它以编程方式从DB中删除,那么您将获得相同的file is encrypted错误。

  3. 因为当我没有应用密码时,我开始通过做一个conn.SetPassword("something")来开始(我相信这是这样做的方式),我无法在不创建另一个SQLite的情况下validation以下内容DB,但我不相信如果你从来没有密码,你可以调用conn.ChangePassword("something") 。 您应该为初始集执行conn.SetPassword("something") ,然后在您的连接字符串中Password=something

  4. 我更改密码的方式是在执行conn.SetPassword("")并清除Password=something来自连接字符串的内容之后,我执行了conn.ChangePassword("somethingelse")

     // Changes an encrypted database to unencrypted and removes password string connString = "Data Source=c:\\test.db3;Password=something"; SQLiteConnection conn = new SQLiteConnection(connString); conn.SetPassword(""); //conn.Open(); // doesn't work because connString hasn't been updated // Update connString connString = "Data Source=c:\\test.db3;"; conn = new SQLiteConnection(connString); conn.Open(); // we've opened the DB without a password // Re-encrypts the database. The connection remains valid and usable afterwards until closed - then the connection string needs updating. conn.ChangePassword("somethingelse"); conn.Close(); // Update connString connString = "Data Source=c:\\test.db3;Password=somethingelse"; conn = new SQLiteConnection(connString); // must re-instantiate! conn.Open(); // we've opened the DB with our new password 

这很好。 我想您也无法从连接字符串中清除它,只需执行conn.ChangePassword("somethingelse") ,然后将Password=somethingelse添加到您的字符串中,之后:

  // Opens an encrypted database string connString = "Data Source=c:\\test.db3;Password=something"; SQLiteConnection conn = new SQLiteConnection(connString); conn.Open(); // Encrypts the database. The connection remains valid and usable afterwards until closed - then the connection string needs updating. conn.ChangePassword("somethingelse"); conn.Close(); // Update connString connString = "Data Source=c:\\test.db3;Password=somethingelse"; conn = new SQLiteConnection(connString); conn.Open(); // we've opened the DB with our new password 

就个人而言,我将密码以加密的forms存储在app(web).config文件中,并将其调用到我的应用程序onload中的变量中,并从中动态构建我的连接字符串。

我知道,如果你删除一个SQLite数据库并尝试调用它,你只会得到一个错误 – 不是用你的连接字符串中的新密码重新创建的SQLite数据库 – 至少在从C#使用和调用它时.NET应用程序

更新如果您需要一个将用于更新密码的函数,您不希望使用.SetPassword() ,但.ChangePassword() 。 我发现最好总是将其删除,然后更改它,就像我在#4中的第一个例子一样。