开发者

Opening a connection in SQLite

开发者 https://www.devze.com 2023-04-06 15:21 出处:网络
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.

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.Exe开发者_Go百科cuteReader();// 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.


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();


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


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号