Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I check in SQLite whether a database exists C#

Tags:

I'm currently programming an app in C# and using sqlite as an embedded database. I have got my app to create a new database on start-up, but how do I get it to check if the database exists? If it does exist how do I get it to use it, and if not how to create a new database?

This is what i have so far:

private void MainWindow_Loaded(object sender, EventArgs e) {     SQLiteConnection sqlite_conn;     SQLiteCommand sqlite_cmd;     bool newdb = false;     if (newdb == true)     {         sqlite_conn = new SQLiteConnection("DataSource=database.db;Version=3;");         sqlite_conn.Open();         MessageBox.Show("31");     }     else     {         sqlite_conn = new SQLiteConnection("Data Source=database.db;Version=3;New=True;Compress=True;");         sqlite_conn.Open();             sqlite_cmd = sqlite_conn.CreateCommand();             sqlite_cmd.CommandText = "CREATE TABLE Client (id integer primary key, Title  varchar(100),Name  varchar(100),Surname  varchar(100),Dateofbirth DateTime , Propertyname varchar(100),Moveindate DateTime,Relationship varchar(100),Spouse  varchar(100),Gender  varchar(100), spTitle  varchar(100),SpouseName  varchar(100),SpouseSurname  varchar(100),spDateofbirth DateTime ,spRelationship varchar(100),spSpouse  varchar(100),spGender  varchar(100));";         sqlite_cmd.ExecuteNonQuery();     }         sqlite_conn.Close();     MessageBox.Show("dasdas");     } 
like image 842
SQLTrooper Avatar asked Jan 16 '14 10:01

SQLTrooper


People also ask

How can I tell if a SQLite database is open?

To see if it's opened, check the pointer to see if it's NULL (which you of course must initialize it to, or else it has an unspecified value). After you close it, assign it to NULL again, and there's your "is open" flag. By the way, creating a DatabaseHandle class using RAII is strongly suggested.

How can I tell if SQLite database is corrupted?

To verify that you're truly suffering from database corruption, enter the following command into the shell: sqlite> PRAGMA integrity_check; If the response is anything other than ok, the database is integrity checks have failed and needs to be repaired.


2 Answers

How about:

if(File.Exists("database.db")) 

Edit: Changed path to match source in original code.

like image 121
TylerD87 Avatar answered Sep 20 '22 22:09

TylerD87


public async Task<bool> IsDbExists(string fileName)     {         try         {             var item = await ApplicationData.Current.LocalFolder.GetFileAsync(fileName);             var db = new SQLiteConnection(DbHelper.DBPATH);             var tb1 = db.GetTableInfo("Domain");             var tb2 = db.GetTableInfo("Account");             var tb3 = db.GetTableInfo("Product");             var tb4 = db.GetTableInfo("Review");             if (item == null || tb1.Count == 0 || tb2.Count == 0 || tb3.Count == 0 || tb4.Count == 0)             {                 return false;             }             else             {                 return true;             }         }         catch         {             return false;         }     } 

This is one of the solution

like image 40
Kabilan Smart Avatar answered Sep 22 '22 22:09

Kabilan Smart