4

I am having issues connecting to my sqlite database. The file is located in the application's folder. Here is the connection string

string path = "Data Source=MY.db";

I can get it to work if I use the absolute path, but it gives me a "table not found" error if I try to use a relative path. Any ideas?

Matt Grogan
  • 317
  • 1
  • 6
  • 18

4 Answers4

12

You are opening up a different -- perhaps a new -- database that does not have said table. (Yes, SQLite will happily create a new database with the default connection settings.)

Make sure the correct database is opened. Remember, relative path is relative to the Current Working Directory, which is likely not that which is expected.

(The working directory is influenced from where, and how, the process is loaded. The working directory for a "Debug" session can be set under Project Settings / Debug / Start Options, for instance.)

Happy coding.

See also:

Community
  • 1
  • 1
  • This was indeed the problem. When backing up and restoring the sqlite files in my app, "i" always used the same working directory. But when moving/copying the file to a new working directory, Sqlite would create an empty db with that file name, frustrating the issue. – Michael K Mar 06 '20 at 19:26
0

This happened when you haven't saved the database and its table while using GUI Manager for SQLite . Two solution; 1) Save your database and its table with CTR+S in GUI Manager 2) Or Simply Just close your GUI manager of SQlite and save all .

Important ! I am using GUI manger for SQLITE (DB Browser for SQLITE) and its all about that.

0

I've had the same problem for both my windows application (C#) and web application (ASP.net). I usually use SQLite because I found it more easier, especially when I worked with connection strings. But the main obstacle for me was to put a relative path in my code, so I can publish it without worrying about being unable to find the database. I've tried many things(using "|Data Directory|", "~/", "./", ...), and none of them works until I found these solutions. It seems the code is working for me, but wonder if I'm using them right?!

Web Application:

    SQLiteConnection sql_con = new SQLiteConnection("Data Source =" + Server.MapPath("~/") + "mydb.db; Version = 3; New = false;);

Windows App:

    SQLiteConnection sql_con = new SQLiteConnection("Data Source =" + System.IO.Path.GetDirectoryName(Application.ExecutablePath) + "mydb.db; Version = 3; New = false; Read Only = true");
AhmadReza
  • 21
  • 1
0

just replace your .database file into \bin\Debug in project folder, because in your case compiler creates DB file with same name but its totally empty 0bytes