tags:

views:

537

answers:

4

Hi,

What does statement "insert" in SQLite return in case of success?

I always believed that it should be SQLITE_DONE, but recently in my logs I found out the following string:

sqlite3_step error: 'not an error'

And here is the code that logs the mentioned string:

prepareStatement(addTranslationStmt2, "INSERT INTO translations(lang1_wordid, lang2_wordid) VALUES(?, ?)");
if (!addTranslationStmt2) return -2;

sqlite3_bind_int(addTranslationStmt2, 1, word_id);
sqlite3_bind_int(addTranslationStmt2, 2, translation_id);

if(sqlite3_step(addTranslationStmt2) != SQLITE_DONE)
{
    NSLog(@"sqlite3_step error: '%s'", sqlite3_errmsg(database));
    sqlite3_reset(addTranslationStmt2);
    return -1;
}

sqlite3_reset(addTranslationStmt2);

I am wondering, why does it work in most cases. Should I change SQLITE_DONE in my code to SQLITE_OK?

Thanks.

+1  A: 

SQLITE_DONE

http://www.sqlite.org/c3ref/step.html

You could also try printing out the error code to find out what the problem is.

Uqqeli
A: 

The SQLite Result Codes Reference lists SQLITE_OK as indicating a successful result. It is also the first error code, having an error code of 0, making it the canonical result (i.e. the result I would expect on a successful operation).

You should put a breakpoint or print statement in your code to find out if it really is returning zero, and check your data to make sure you're getting the result you expect. If that all checks out, I would change your condition to check for SQLITE_OK.

Robert Harvey
I just cannot understand, why does the following code executes, but new data sometimes is not being added(but sometimes it does!)
Ilya
A: 

In cases like these, I like to look at code samples. Here are some good ones:

http://sqlite.phxsoftware.com/forums/p/76/6659.aspx

Robert Harvey
A: 

The details of the behavior of the sqlite3_step() interface depend on whether the statement was prepared using the newer "v2" interface sqlite3_prepare_v2() and sqlite3_prepare16_v2() or the older legacy interface sqlite3_prepare() and sqlite3_prepare16().

In the legacy interface, the return value will be either SQLITE_BUSY, SQLITE_DONE, SQLITE_ROW, SQLITE_ERROR, or SQLITE_MISUSE. With the "v2" interface, any of the other result codes or extended result codes might be returned as well.

Robert Harvey