1

I am using http://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki version 1.0.82.0

When I insert a row like so:

"INSERT INTO [testtable] (col1,col2) values ('','')"

I always get a result of 1 from SQLiteCommand.ExecuteNonQuery(); where it should be returning 0 (OK) or 101 (DONE). I know the row is getting inserted just fine because the auto increment value increases each time I run the method.

The class

readonly object _threadlock = new object();
readonly string _file;

public CSQLite(string file)
{
    _file = file;
}

private SQLiteConnection _sqlCon;
private SQLiteCommand _sqlCmd;
private SQLiteDataAdapter _db;

private void SetConnection()
{
    lock (_threadlock)
    {
        _sqlCon = new SQLiteConnection(String.Format("Data Source={0};Version=3;New=False;Compress=True;", _file));
    }
}

public int SimpleInsertAndGetlastID(out int id)
{
    lock (_threadlock)
    {
        SetConnection();
        _sqlCon.Open();

        //Execute
        _sqlCmd = _sqlCon.CreateCommand();
        _sqlCmd.CommandText = "INSERT INTO [testtable] (col1,col2) values ('','')";
        var res = _sqlCmd.ExecuteNonQuery();

        //Get id
        _db = new SQLiteDataAdapter("select last_insert_rowid();", _sqlCon);
        var ds = new DataSet();
        _db.Fill(ds);
        DataTable dt = ds.Tables[0];
        var val = dt.Rows[0][0].ToString();

        Int32.TryParse(val, out id);

        _sqlCon.Close();
        return res;
    }
}

The Test:

/// <summary>
///A test for SimpleInsertAndGetlastID
///</summary>
[TestMethod()]
public void SimpleInsertAndGetlastIDTest()
{
    var file = "dbs\\test.db";
    var target = new CSQLite(file);
    var id = -1;
    var res = -1;

    try
    {
        res = target.SimpleInsertAndGetlastID(out id);
    }
    catch (Exception ex){/*Breakpoint*/}

    Assert.IsTrue(id > 0); //id gets +1 every time the test is run so the row *is* getting inserted
    Assert.IsTrue(res==0||res==101); //Res is always 1 for some reason
}

Table creation (in case that's the problem):

public List<string> Columns { get; set; }

if (!File.Exists(_dbFile))
    SQLiteConnection.CreateFile(_dbFile);

var fieldqry = "";
var count = 0;
Columns.ForEach((field) =>
    {
        count++;

        fieldqry += String.Format("[{0}] TEXT NULL", field);

        if (count < Columns.Count)
            fieldqry += ",";
    });

var qry = String.Format("CREATE TABLE IF NOT EXISTS [{0}](" +
                        "[ID] INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT," +
                        "{1}" +
                        ");", TableName, fieldqry);

var sql = new CSQLite(_dbFile);
var res = sql.Execute(qry);
if(res!=SqLiteErrorCodes.SQLITE_OK)
    throw new SqLiteImplementationException("Query failed.");

Where columns is new List<string> { "col1", "col2" } };

Can anyone tell me what I did wrong?

2 Answers 2

3

ExecuteNonQuery() does not return a SQLite error code, it returns the number of rows affected by the query. If you are inserting a row, 1 sounds like the expected result if the operation was successful.

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

1 Comment

Oh, I see! 1 would be the perfect result then. But then what's the method called that returns all of these pretty useful codes? -- Never mind: stackoverflow.com/questions/11223044/…
3

The result from ExecuteNonQuery is concidered as "number of rows affected" and not an error code :-)

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.