To check if this entry exists, you can simplify your code
cmd.CommandText = "SELECT count(*) FROM wordlist WHERE word='word'"; int count = Convert.ToInt32(cmd.ExecuteScalar()); if(count == 0) { cmd.CommandText = "INSERT INTO wordlist(word) VALUES ('word')"; cmd.ExecuteNonQuery(); }
ExecuteScalar will return the first column in the first row returned by your query.
(The link is for SqlServer, but it is identical for SQLite, because SQLiteCommand must implement the IDbCommand interface )
Another approach to use is as follows
cmd.CommandText = "INSERT INTO wordlist (word) SELECT ('word') WHERE NOT EXISTS (SELECT 1 FROM wordlist WHERE word = 'word');"; cmd.ExecuteNonQuery();
This is even better because you are using one query and not two (although the difference in the local database should be minimal)
Steve source share