MySQL Forums
Forum List  »  SQLite

problem with inserting values into sqlite table
Posted by: kugb kjlhb
Date: October 29, 2009 09:42AM

hello everyone!
I`m trying to use sqlite database with c++, but something isnt working right...

The problem I`m dealing here is that i cant insert other datas into the table i`ve created except NULL, because i get following error message:

Error in select statement : INSERT INTO a (a,b,c,d) VALUES (NULL, NULL, something_else, NULL) [no such column: something_else].

when i write :

insert(4, aa, "NULL", "NULL", "NULL");

then NULL "values" are inserted

I guess i`ve made some mistake with using pointers, but i dont see it.. I`m not very experienced programmer..

here is the main function:

int main ()

char data_base_name [20];
char *db_name;
db_name = data_base_name;

char tab_name [20];

open_db ("db_name");

cout<<"Insert table name:"<<endl;

char a[10]="a";
char aa[100]="NULL";

select_stmt ("DROP TABLE a");

create_table (4, a, "b", "c", "d");

insert(4, aa, "NULL", "something_else", "NULL");


getchar ();

return 0;

create function, which i think works ok, but who knows...

int create_table (int no_col, char *fmt, ...)
int i;
char f[500] = "CREATE TABLE ";

va_list ptr;
va_start (ptr, fmt);
for (i=0; i<(no_col-1); i++)
strcat (fmt, ","); // fmt = a ,b ,c ,d
strcat(fmt, (va_arg (ptr, char*)));
va_end (ptr);

fmt1 = fmt; //fmt1- global pointer

strcat (f, table_name); // "CREATE TABLE x
strcat (f, " ("); // "CREATE TABLE x "(
strcat (f, fmt); // "CREATE TABLE x ( a,b,c,d
strcat (f,")"); // "CREATE TABLE x (a,b,c,d)"

char * stmt = f ;
printf ("\nstmt = %s\n", stmt);
select_stmt (stmt);

return 0;

insert function :

int insert (int no_col, char *fmt2, ... )
int i;
va_list ap;
va_start (ap, fmt2);

for (i=0; i<(no_col-1); i++)
strcat (fmt2, ",");
strcat (fmt2, (va_arg(ap, char*)));
va_end (ap);

char k[500]= "INSERT INTO ";
strcat (k, table_name );
strcat (k, " ( ");
strcat (k, fmt1);
strcat (k, ") ");
strcat (k, "VALUES (");
strcat (k, fmt2);
strcat (k, ")");
printf ("\nk = %s\n\n", k);

char * stmt = k;
select_stmt (stmt);

return 0;

any help and suggestions very appreciated..


Options: ReplyQuote

Written By
problem with inserting values into sqlite table
October 29, 2009 09:42AM

Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.