2

My goal is to insert a column into a SQLite Table and then return the entry so that I can get the primary key value. I have the following code that executes generic insert statements for me:

 int SQLiteHelper::ExecuteWriteStatement(Platform::String^ statement)
 {
    char* zErrMsg;
int rc;

LPCWSTR input = statement->Data();
char sql[MAX_PATH + 1];
memset(sql, 0, sizeof(sql));
WideCharToMultiByte(CP_UTF8, 0, input, -1, sql, sizeof(sql), NULL, NULL);

const char* data = "Callback function called";

rc = sqlite3_exec(m_db, sql, WriteCallback, (void*)data, &zErrMsg);

if(rc != SQLITE_OK)
{
    OutputDebugString(L"Failed to Write to database");
    sqlite3_free(zErrMsg);
}
else
{
    OutputDebugString(L"Succesfully wrote to database");
}
 }

My callback is implemented as follows (note that it is a static private class method):

int SQLiteHelper::WriteCallback(void *NotUsed, int argc, char** argv, char** azColName)
{
   ////do some stuff
}

I am aware that I will probably use the void* NotUsed callback parameter to return data and I'm pretty sure it maps to the (void*)data from the sqlite3_exec() function call. The problem I have is that when I run an insert statement, the code does not reach the callback function.

If you run an insert command using sqlite3_exec shouldn't it call the callback method?

Is there a different sqlite command I should be using?

Thanks!

1 Answer 1

5

The sqlite3_exec callback gets called if the statement returns some data. This can happen only for SELECT and some PRAGMA statements.

To get the rowid of the inserted record, call sqlite3_last_insert_rowid.

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

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.