Reputation: 921
I'm compiling a example application which uses the sqlite3 using the source code on my windows x64 machine.
#include "sqlite3.h"
#include <stdio.h>
#include <string.h>
#include <windows.h>
int main(int argc,char **argv)
{
sqlite3 *db;
sqlite3_stmt * pSqllite3_stmt=NULL;
char *zErrMsg=0;
int rc;
// create a new database //
rc =sqlite3_open( ".\\sql_lite3.db" ,&db);
if(rc!=SQLITE_OK|| db == NULL)
{
MessageBox(NULL,TEXT("can't open the database") , TEXT("sql_lite example"), MB_OK);
exit(0);
}
// create a table called string_index //
char * create_query = "create table string_table (index INT ,string TEXT);" ;
rc = sqlite3_prepare_v2( db , create_query, -1, &pSqllite3_stmt,NULL);
if (rc != SQLITE_OK ) {
// if an error occurred //
char error_code[128];
sprintf(error_code," Error Code is %d" ,rc);
MessageBox( NULL, error_code,TEXT("sql_lite example"), MB_OK);
exit(0);
}
// step the create query //
rc = sqlite3_step(pSqllite3_stmt);
if(SQLITE_OK != rc)
{
MessageBox(NULL,TEXT("Create step have failed "),TEXT("sql lite example"),MB_OK);
exit(0);
}
// insert records to the database //
char *insert_query = new char [ 1024];
int i;
sqlite3_stmt * insert_stmt ;
char * insert_string ="This is the insert String";
for (i=1; i<= 10;i++){
sprintf( insert_query,"INSERT INTO string_table(index,string) VALUES(%d,?1);" , \
i);
// execute the insert query //
rc = sqlite3_prepare_v2(db , insert_query,strlen(insert_query), &insert_stmt,NULL);
if(rc!= SQLITE_OK)
{
MessageBox(NULL,TEXT("INSERT INTO have been failed"), TEXT("SQLITE EXAMPLE"),MB_OK);
exit(0);
}
// bind the string //
rc =sqlite3_bind_text(insert_stmt,1,insert_string,sizeof(insert_string),SQLITE_TRANSIENT);
if(rc != SQLITE_OK && rc != SQLITE_DONE)
{
MessageBox(NULL,TEXT("bind failed"),TEXT("SQLITE EXAMPLE"), MB_OK);
exit(0);
}
// call step //
rc = sqlite3_step(insert_stmt);
if( rc != SQLITE_OK)
{
MessageBox(NULL,TEXT("Insertion step failed "),TEXT("SQLITE EXAMPLE"),MB_OK);
exit(0);
}
// finalize the insert query //
sqlite3_finalize(insert_stmt);
}
// now we are going to execute the search query //
char * search_query = "SELECT * FROM string_table WHERE index=10";
sqlite3_stmt * search_stmt ;
rc = sqlite3_prepare_v2(db,search_query,sizeof(search_query),&search_stmt,NULL);
if( SQLITE_OK== rc)
{
while( SQLITE_ROW == sqlite3_step(search_stmt ) )
{
// print the record //
const int index_value= sqlite3_column_int(search_stmt,1);
const unsigned char * string_value = sqlite3_column_text(search_stmt,2);
printf( "index :%d and string:%s ",index_value,string_value );
}
}else{
MessageBox(NULL,TEXT("Searching query sqlite3 have been failed"),TEXT("SQLITE example"), MB_OK);
exit(0);
}
// fnalize the search statement //
sqlite3_finalize(search_stmt);
// don't forget to close the connection //
sqlite3_close(db);
return 0;
}
But when I trying to create a table it always gives me the error code 1. Which means , the bellow code fails.
// create a table called string_index //
char * create_query = "create table string_table (index INT ,string TEXT);" ;
rc = sqlite3_prepare_v2( db , create_query, -1, &pSqllite3_stmt,NULL);
if (rc != SQLITE_OK ) {
// if an error occurred //
char error_code[128];
sprintf(error_code," Error Code is %d" ,rc);
MessageBox( NULL, error_code,TEXT("sql_lite example"), MB_OK);
exit(0);
}
Why is that ? How could I overcome this issue?
Any suggestions, or improvements?
--Thanks in advance--
Upvotes: 1
Views: 234
Reputation: 180060
To show a useful error message, use sqlite3_errmsg
:
MessageBox(0, sqlite3_errmsg(db), NULL, 0);
As for your SQL command: index
is a keyword, so you have to quote it whenever you use it:
CREATE TABLE string_table("index" INT, string TEXT);
It might be easier to use another column name.
Upvotes: 1