2

I am trying to insert a record into a table in C#, but when I get to the line of code to execute the query to insert the record into the table, it just comes up with this error:

An exception of type 'System.Data.SQLite.SQLiteException' occurred in System.Data.SQLite.dll but was not handled in user code

Additional information: datatype mismatch

The code that I have to insert the data into the database is as follows:

string stmt = string.Format("INSERT INTO quotes (id, user, quote, date) VALUES ('', '{0}', '{1}', '{2}')", person, quote, date);

SQLiteCommand cmd = new SQLiteCommand(stmt, connection);

int rowsAffected = cmd.ExecuteNonQuery();

person, quote and date are all strings and the structure of the database is as follows:

id = INTEGER PRIMARY KEY, user = STRING(255), quote = STRING(255) and date = STRING(255)

8
  • It looks like you're trying to insert a string (the first element in the values clause) into an integer (id). Commented Dec 4, 2016 at 17:46
  • I do not know about missmatch problem but you can write query like that too string stmt = string.Format("INSERT INTO quotes (user, quote, date) VALUES ('{0}', '{1}', '{2}')", person, quote, date); if id is autoincremented. Commented Dec 4, 2016 at 17:46
  • Start by using SQL PArameters. If the text includes something like O'Toole it will crash; they will also assure the right type is passed Commented Dec 4, 2016 at 17:47
  • Why are you inserting an empty string in the ID ? Commented Dec 4, 2016 at 17:48
  • Look at this answer on how to do your task stackoverflow.com/questions/12785780/… Commented Dec 4, 2016 at 17:48

1 Answer 1

3

If id is autoincremented, you can insert data with this query

stmt = string.Format("INSERT INTO quotes (user, quote, date) VALUES ('{0}', '{1}', '{2}')", person, quote, date);
Sign up to request clarification or add additional context in comments.

1 Comment

I will as soon as it lets me

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.