Abdullah
Abdullah

Reputation: 518

WPF SQLite queries exception

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.

Upvotes: 1

Views: 363

Answers (1)

mm8
mm8

Reputation: 169200

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

Upvotes: 3

Related Questions