繁体   English   中英

SQLIte 无法打开数据库

[英]SQLIte unable to open database

我刚开始开发一个示例应用程序,该应用程序仅调用我的 SQLite 数据库中的一些表,并且我已经设法解决了除此问题之外发生的其他问题。

尽管我已经搜索过这个,但针对连接字符串、权限问题等的建议解决方案似乎都不是有效的并且对我有用。 对于权限,我添加了具有完全控制权限的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/

我真的很感谢你在这方面的帮助。

根据您的评论,您收到“无法打开数据库文件”错误,因为您将代码指向不存在的文件。

“找不到表”错误意味着它找到了数据库,但没有找到您要查找的表。 另一方面,“无法打开数据库文件”意味着它甚至找不到数据库,甚至没有费心寻找表。 当您收到“找不到表”时,您就离它正常工作更近了。

您应该将路径改回以匹配磁盘上的文件,然后使用Firefox SQLite Manager 之类的工具确认english_words表确实存在于您的数据库中。

如果没有,您应该使用该工具创建它,如果有,您应该在此处发布另一个关于“找不到表”错误的问题。

希望这有帮助。

当我遇到了这个错误,我不得不设置parseViaFrameworkSQLiteConnection构造函数true

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

我在共享托管服务器上遇到了同样的问题,我的 C# 代码能够从 SQLIte db 文件中读取数据。 但是在添加/更新数据时抛出错误“无法打开数据库”

我尝试了 stackoverflow 上建议的许多选项但是在参考https://stackoverflow.com/a/17780808/2021073
https://www.sqlite.org/pragma.html#pragma_journal_mode我尝试添加日志模式=关闭; 到连接字符串

这对我有用

示例代码

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM