Created
June 11, 2018 02:58
-
-
Save tsonglew/c776265d55eb1b93e3edaf942b62c5a1 to your computer and use it in GitHub Desktop.
C# check if table exists in sqlite
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private bool tableExists(string tableName, SQLiteConnection db) | |
{ | |
bool exists; | |
db.Open(); | |
try | |
{ | |
var cmd = new SQLiteCommand( | |
"select case when exists((select * from information_schema.tables where table_name = '" + tableName + "')) then 1 else 0 end", db); | |
exists = (int)cmd.ExecuteScalar() == 1; | |
} | |
catch | |
{ | |
try | |
{ | |
exists = true; | |
var cmdOthers = new SQLiteCommand("select 1 from " + tableName + " where 1 = 0", db); | |
cmdOthers.ExecuteNonQuery(); | |
} | |
catch | |
{ | |
exists = false; | |
} | |
} | |
db.Close(); | |
return exists; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment