SQLITE无法打开数据库

jm2pwxwz  于 2022-12-13  发布在  SQLite
关注(0)|答案(3)|浏览(293)

我刚开始开发一个示例应用程序,它只调用SQLite数据库中的一些表,并且我已经设法解决了除此之外的其他问题。
虽然我已经搜索了这个问题,但是对于connectionstring、权限问题等,没有一个建议的解决方案对我有效。对于权限,我添加了具有完全控制权的Everyone用户,但我仍然得到同样的错误。
下面是我尝试执行的代码:

// calling function
void getRecords2()
    {
        MySqlLite.DataClass ss = new MySqlLite.DataClass();
        DataTable dt = ss.selectQuery("select * from english_words"); 
    }

// the SQLite class that execute the code
using System.Data;
using System.Data.SQLite;

namespace MySqlLite
{
    class DataClass
    {
        private SQLiteConnection sqlite;

        public DataClass()
        {            
            //This part killed me in the beginning.  I was specifying "DataSource"
            //instead of "Data Source"
            sqlite = new SQLiteConnection(@"Data Source=C:\testwork\db\MrPick.sqlite3.db;Version=3;FailIfMissing=True");

        }

        public DataTable selectQuery(string query)
        {
            SQLiteDataAdapter ad;
            DataTable dt = new DataTable();

            try
            {
                SQLiteCommand cmd;
                sqlite.Open();  //Initiate connection to the db
                cmd = sqlite.CreateCommand();
                cmd.CommandText = query;  //set the passed query
                ad = new SQLiteDataAdapter(cmd);
                ad.Fill(dt); //fill the datasource

                cmd.Dispose();
                sqlite.Dispose();  

            }
            catch (SQLiteException ex)
            {
                //Add your exception code here.
            }
            sqlite.Close();
            return dt;
        }
    }
}

注意:我使用了以下程序集:

ADO.NET SQLite Data Provider
Version 1.0.82.0 September 3, 2012
Using SQLite 3.7.14
Originally written by Robert Simpson
Released to the public domain, use at your own risk!
Official provider website: http://system.data.sqlite.org/

我真的很感谢你的帮助。

i34xakig

i34xakig1#

根据您的评论,您将得到一个“无法打开数据库文件”错误,因为您将代码指向一个不存在的文件。
一个“找不到表”的错误意味着它找到了数据库,但不是你要找的表。另一方面,“无法打开数据库文件”意味着它甚至找不到数据库,甚至没有费心去寻找一个表。当你得到“找不到表”时,你就离它正常工作更近了。
您应该将路径改回与磁盘上的文件匹配,然后使用Firefox SQLite Manager之类的工具确认数据库中确实存在english_words表。
如果没有,您应该使用该工具创建它,如果有,您应该在这里发布另一个关于“Table not found”错误的问题。
希望能帮上忙。

rbpvctlc

rbpvctlc2#

当我遇到这个错误时,我不得不将SQLiteConnection构造函数中的parseViaFramework设置为true

SQLiteConnection connection = new SQLiteConnection(connectionString, true);
connection.Open();
de90aj5v

de90aj5v3#

我在共享主机服务器上遇到了同样的问题,我的C#代码能够从SQLIte数据库文件中读取数据。但在添加/更新数据时,它抛出了错误“无法打开数据库”
我尝试了很多关于stackoverflow建议的选项,但是在引用了https://stackoverflow.com/a/17780808/2021073
https://www.sqlite.org/pragma.html#pragma_journal_mode 我试着添加**日志模式=Off;**到连接字符串
这对我很有效
样本代码

SQLiteConnection connection = new SQLiteConnection("Data Source=G:\dbfolder\sqlite3.db;Version=3;Mode=ReadWrite;journal mode=Off;", true);

相关问题