mardi 20 janvier 2015

How to determine the cause of an SQLITE_MISUSE error in a C++ program?

I am just trying to get an example working to create a table in an SQLite database, but the sqlite3_exec() call is returning an SQLITE_MISUSE code (21). I have been unable to determine the cause of the error or find an explanation online. Does anyone have an idea?


The output of the code is:


Opened database successfully 21 SQL error:


The code is:



#include <iostream>
#include <sqlite3.h>
#include <cstdlib>

static int callback(void *NotUsed, int argc, char **argv, char **azColName){
int i;
for(i=0; i<argc; i++){
std::cout << azColName[i] << " = " << (argv[i] ? argv[i] : "NULL") << std::endl;
}
return 0;
}

int main()
{
sqlite3* db;
char* zErrMsg = 0;
int rc;

/* Open database */
rc = sqlite3_open("test.db", &db);
if( rc ){
std::cout << "Can't open database: " << sqlite3_errmsg(db) << std::endl;
exit(0);
}else{
std::cout << "Opened database successfully" << std::endl;
}
sqlite3_close(db);

/* Create SQL statement */
const char* sql = "CREATE TABLE COMPANY(ID INT PRIMARY KEY NOT NULL, NAME TEXT NOT NULL, AGE INT NOT NULL, ADDRESS CHAR(50), SALARY REAL)";

/* Execute SQL statement */
rc = sqlite3_exec(db, sql, callback, 0, &zErrMsg);
if( rc != SQLITE_OK ){
std::cout << rc << std::endl;
std::cout << "SQL error: " << zErrMsg << std::endl;
sqlite3_free(zErrMsg);
}else{
std::cout << "Table created successfully" << std::endl;
}
sqlite3_close(db);

return 0;
}

Aucun commentaire:

Enregistrer un commentaire