Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLIte unable to open database

Tags:

c#

sqlite

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.

like image 554
Rama Avatar asked Nov 19 '12 18:11

Rama


People also ask

How do I open a SQLite database?

Open a command prompt (cmd.exe) and 'cd' to the folder location of the SQL_SAFI. sqlite database file. run the command 'sqlite3' This should open the SQLite shell and present a screen similar to that below.

How do I run a .db file?

In Windows Explorer, navigate to the drive or folder containing the Access database file you want to open and double-click the database. Access starts and the database is opened.

Could not open database file reason database is locked?

Reasons Responsible for Error Database is Locked. This error code occurs when the user tries to perform two inappropriate operations on a database at the same detail and on the same database connection.


2 Answers

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.

like image 139
Bobson Avatar answered Oct 17 '22 21:10

Bobson


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();
like image 10
Peet Avatar answered Oct 17 '22 23:10

Peet