Rama
Rama

Reputation: 71

SQLIte unable to open database

I just started working on a sample application that only calls some tables on my SQLite database, and I have managed to solve other issues that have happened except this one.

Although I have searched for this, none of the suggested solutions for the connectionstring, permission issues and etc seem to be valid and working for me. For the permissions, I added the Everyone user with full control, and I still get the same error.

Below is the code that I am trying to execute:

// 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;
        }
    }
}

Note: I used the following assembly:

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/

I really appreciate your help on this.

Upvotes: 6

Views: 45012

Answers (3)

Alvin
Alvin

Reputation: 11

I was facing same issue on the shared hosting server, My C# code was able to read data from SQLIte db file. But while add / update data it was throwing Error "unable to open database"

I tried many options suggested on stackoverflow But after referring https://stackoverflow.com/a/17780808/2021073 and
https://www.sqlite.org/pragma.html#pragma_journal_mode I tried adding journal mode=Off; to the Connection string

and it worked for me

sample code

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

Upvotes: 0

Peet
Peet

Reputation: 766

When I ran into this error I had to set parseViaFramework in the SQLiteConnection constructor to true.

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

Upvotes: 11

Bobson
Bobson

Reputation: 13696

Per your comment, you're getting a "Unable to open database file" error because you're pointing the code at a file that doesn't exist.

A "Table not found" error means it found the database, but not the table you were looking for. On the other hand, "Unable to open database file" means that it couldn't even find the database, and didn't even bother looking for a table. You're much closer to it working correctly when you're getting "Table not found".

You should change the path back to match the file on disk, then use a tool like the Firefox SQLite Manager to confirm that the english_words table does exist in your database.

If it doesn't, you should create it with that tool, and if it does, you should post another question here about the "Table not found" error.

Hopefully that helps.

Upvotes: 11

Related Questions