Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLite .NET Insert ExecuteNonQuery Returns 1

SQL:

CREATE TABLE main.LogRecord (
Id INTEGER PRIMARY KEY  AUTOINCREMENT  NOT NULL ,
TimeStamp DATETIME NOT NULL ,
Type TEXT NOT NULL ,
Severity TEXT NOT NULL ,
Message TEXT NOT NULL ,
Origin TEXT NOT NULL)

INSERT INTO LogRecord
(TimeStamp, Type, Severity, Message, Origins) VALUES
(@timestamp, @type, @severity, @message, @origin)

C# Pseudo-ish Code:

SQLiteCommand command = new SQLiteCommand(INSERT_COMMAND_TEXT);
command.Parameters.AddWithValue("@paramName", object.value);
command.Connect = connectVar;
command.ExecuteNonQuery();

When ExecuteNonQuery() runs, the value 1 is returned which according to SqliteOrg means:

SQL error or missing database

Except, no exception is thrown AND the data is inserted correctly.

Is there way to get the LAST_ERROR_MESSAGE using System.Data.SQLite? I've only come across ways to get the last error message for non .net APIs.

When I incorrectly insert to a column that does not exists the same error code (1) is returned BUT this time an exception is thrown with the last error message.

So if the insert is working, why is 1 being returned? Any help is greatly appreciated.

Edit: I also tried not sending the TimeStamp DateTime in case it was a date formatting issue but the same situation occurs.

Edit: Turns out 1 is the # of rows affected and it's all in the documentation http://msdn.microsoft.com/en-us/library/system.data.idbcommand.executenonquery%28v=vs.80%29.aspx

like image 576
Chris Avatar asked Feb 21 '13 21:02

Chris


2 Answers

Reread your docs. I believe ExecuteNonQuery returns the number of rows affected or -1 on an error.

like image 107
Dale M Avatar answered Oct 04 '22 16:10

Dale M


Are you sure about the return value?

http://msdn.microsoft.com/en-us/library/system.data.idbcommand.executenonquery.aspx

For UPDATE, INSERT, and DELETE statements, the return value is the number of rows affected by the command. For all other types of statements, the return value is -1.

like image 42
granadaCoder Avatar answered Oct 04 '22 15:10

granadaCoder