Zain Ali
Zain Ali

Reputation: 15953

Opening a connection in SQLite

I am trying to use SQLite in Windows ce 5.1.17. I create a SQLite database using SQLite Administrator in debug folder of my project. Then I add System.Data.SQLite.dll and SQLite.Interop.065.dll to my project. I am trying the following code to connect to database.

SQLiteConnection myConnection = null;
myConnection = new SQLiteConnection(@"Data Source=" + System.IO.Path.Combine(System.IO.Path. GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase), Stock.s3db)+ "; Version=3; Cache Size =100");
                myConnection.Open();

                SQLiteCommand cmd = new SQLiteCommand();
                cmd.CommandText = "select * from shops;";
                cmd.CommandType = CommandType.Text;
                cmd.Connection = myConnection;
                cmd.ExecuteReader();// I get exception no such table: shops

Also when I check myConnection database property it shows 'Database = "main"'. I do not know why database name is 'main', but in myConnection I set datasource to Stock.s3db.

Upvotes: 2

Views: 7194

Answers (4)

zeFrenchy
zeFrenchy

Reputation: 6591

Use the connection string builder to ensure you get a correctly formatted string

SQLiteConnectionStringBuilder builder = new SQLiteConnectionStringBuilder();
builder.FailIfMissing = true;
builder.DataSource = "Insert the fully qualified path to your sqlite db";
SQLiteConnection connection = new SQLiteConnection(builder.ConnectionString);
connection.Open();

Upvotes: 8

ctacke
ctacke

Reputation: 67168

Putting the database in the debug folder of the build output does not mean the file will end up on the target device. You need to copy the database over to your target device (there are several mechanisms like a USB disk, Remote File Viewer, adding it as a Content item to your project, etc.).

I'd also be inclined to use a fully qualified path to the database, as CE doesn't have any concept of a working directory.

Upvotes: 1

zeFrenchy
zeFrenchy

Reputation: 6591

If you look inside your Debug/Release folder, you'll probably find the empty database files created for you since the file you specify does not exist. 'main' is the standard db name with sqlite.

Copy your db in said folder for testing, or specifiy the full path in the connection string.

try this query to determine where the file you are using is located.

PRAGMA database_list

Upvotes: 1

MPelletier
MPelletier

Reputation: 16657

Most likely your database file is not in the correct folder. Try giving this line:

myConnection = new SQLiteConnection(@"Data Source=Stock.s3db; Version=3;");

the full path to your database.

Upvotes: 0

Related Questions