简体   繁体   中英

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM