6

I have a WPF application where I access a SQLite database via ADO.NET (http://adodotnetsqlite.sourceforge.net/). So far everything works fine, but when I try to execute the following SQL:

 sqlite_cmd.CommandText = "CREATE TABLE IF NOT EXISTS notes (id integer primary key, text varchar(100));";
 sqlite_cmd.ExecuteNonQuery();

I get the following exception:

An exception of type 'Finisar.SQLite.SQLiteException' occurred in SQLite.NET.dll but was not handled in user code.
Additional information: near "NOT": syntax error

When I remove the IF NOT EXISTS part it works fine, but I want to create the table only if it is not already there. Is there anything I'm doing wrong?

2

2 Answers 2

5

This question has some answers which may be helpful. From that question, however, this answer suggests that SQLite 3.3 and above support IF NOT EXISTS.

Based on that question's answers, you could try selecting the COUNT of tables named 'notes' using this (slightly modified) query:

SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name='notes';

You can then test the result of that query. If there were 0 results, create the table. Otherwise, don't create the table.

Sign up to request clarification or add additional context in comments.

Comments

1

I don't know how it runs with or without "IF NOT EXISTS" because that's not cause of the error. The CREATE statement use TEXT as the name of the field and TEXT is a field type. You need to use brackets around [TEXT].

This statement is a valid one only because the brackets:

CREATE TABLE IF NOT EXISTS [TABLE] (ID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, [TEXT] TEXT);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.