Skip to content

Instantly share code, notes, and snippets.

@tsonglew
Created June 11, 2018 02:58
Show Gist options
  • Save tsonglew/c776265d55eb1b93e3edaf942b62c5a1 to your computer and use it in GitHub Desktop.
Save tsonglew/c776265d55eb1b93e3edaf942b62c5a1 to your computer and use it in GitHub Desktop.
C# check if table exists in sqlite
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