hello !
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];
table_name=tab_name;

open_db ("db_name"); 

cout<<"Insert table name:"<<endl;
cin>>tab_name;

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");

sqlite3_close(db);

getchar ();

return 0;
}

function create_table is working ok, if i`m not wrong..

int create_table (int no_col, char *fmt, ...) // CREATE TABLE (4, a,b,c,d)
{
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 apriciated

Tajna

Problem solved...
What i did wrong was that i have forgotten that values i want to insert into a table have to be inside ' ', so I have just changed the loop withing insert function to look like this:

for (i=0; i<(no_col-1); i++)
{
    strcat (fmt2, ", [COLOR="Red"]' [/COLOR]");
    strcat (fmt2, (va_arg(ap, char*)));
    strcat (fmt2, "[COLOR="red"]'[/COLOR]");
}

and in main function I did the same thing here:

char aa[100]=" ' whatever ' ";
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.