1

There are multiple related questions but none of them solved my problem. I have a C# (WPF) Application which connects to a sqlite database. There are 2 model classes - Dealer and Order and 2 corresponding tables in the Database.

I am able to do CRUD operations in Dealer table but when trying to do the same in Order table, I get an Exception - SQL Logic error or missing database. I am pasting some of the related code:

For Dealer

using(SQLiteConnection conn = new SQLiteConnection(connectionString)) {
conn.Open();
using(SQLiteCommand cmd = new SQLiteCommand(conn)) {
  cmd.CommandText = "INSERT INTO Dealer(name, address, contact, note) VALUES (@Name, @Address, @Contact, @Note)";
  cmd.Prepare();
  cmd.Parameters.AddWithValue("@Name", dealer.Name);
  cmd.Parameters.AddWithValue("@Address", dealer.Address);
  cmd.Parameters.AddWithValue("@Contact", dealer.Contact);
  cmd.Parameters.AddWithValue("@Note", dealer.Note);
  try {
   result = cmd.ExecuteNonQuery();
  }

For Order

using(SQLiteConnection conn = new SQLiteConnection(connectionString)) {
conn.Open();
using(SQLiteCommand cmd = new SQLiteCommand(conn)) {
  cmd.CommandText = "INSERT INTO Order(dealer_id, note) VALUES (@DealerId, @Note)";
  cmd.Prepare();
  cmd.Parameters.AddWithValue("@DealerId", order.DealerId);
  cmd.Parameters.AddWithValue("@Note", order.Note);
  try {
   result = cmd.ExecuteNonQuery();
  }

I get the exception at cmd.ExecuteNonQuery(); statement for Order.

I have cross verified column name spellings and tried SELECT * FROM Order which again returned the same exception.

I also tried SELECT name FROM sqlite_master WHERE type='table' AND name='Order'; from the For Order code above to check if table exists and it returned the correct table name.

Please help. Thanks in advance.

0

1 Answer 1

3

ORDER is a reserved keyword in SQLite so you should quote it, for example using [ ... ]:

cmd.CommandText = "INSERT INTO [Order] (dealer_id, note) VALUES (@DealerId, @Note)";

There are four ways of quoting keywords in SQLite: https://sqlite.org/lang_keywords.html

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

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.