Re: [sqlite] execute or prepare+step+finalize

2009-11-11 Thread TTTTT

i`ve done it, and i think i understand it :)

thank you..


few more question to be sure that i`m not missing something important..

if i use prepared statements only (dont use exec function in program at all)
i dont need callback function, do i?
 somehow after i replaced exec (i.e. select_statement function) and i dont
need select_statement function, neither callback function, evetything seems
much more simple (which is why i`m bit suspicious if i`ve done everything
correct)..

are there any cases when it is better to use exec ?


does using prepared statement slow down my program since prepare function is
used every time when i want to execute some command ? here is how my
function looks like now:





bool create2 (char * command)
{

sqlite3_stmt * statement2;

if ( sqlite3_prepare (db, command, -1, , 0) != SQLITE_OK )
{
int err = sqlite3_prepare (db, command, -1, , 0);
const char * pErr = sqlite3_errmsg (db);
printf ("\nError %d occured! \n %s", err, pErr  );
return false;
}

int iCol = sqlite3_column_count (statement2);

int smth, i;
//int rows=1;
smth=sqlite3_step(statement2);

while (smth == SQLITE_ROW)
{
printf ("\n");
//printf ("\n Row %d:\t", rows);   // doesnt give right number of column
for other select statements except select all
for (i=0; i

Re: [sqlite] sqlite3_free()

2009-11-11 Thread TTTTT



is this right way how to do it:

sqlite_free (errmsg); 

???


or this:


sqlite3_free (NULL);

or how should i do it?
-- 
View this message in context: 
http://old.nabble.com/sqlite3_free%28%29-tp5188068p26302585.html
Sent from the SQLite mailing list archive at Nabble.com.

___
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users


Re: [sqlite] sqlite3_free()

2009-11-11 Thread TTTTT



jalburger wrote:
> 
> 
> 
> 
> By "the same" I mean the same sequence of bytes.  The error message
> from sqlite3_exec() is always obtained from a malloc-like memory
> allocator and must be freed using sqlite3_free().  The error message
> returned by sqlite3_errmsg() is always a constant, static string.
> The error messages might say the same thing, but they are distinct
> strings.
> --
> D. Richard Hipp   
> 
> 
> 


is this right way how to do it:

sqlite_free (errmsg); 

???

thank you
-- 
View this message in context: 
http://old.nabble.com/sqlite3_free%28%29-tp5188068p26302583.html
Sent from the SQLite mailing list archive at Nabble.com.

___
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users


Re: [sqlite] execute or prepare+step+finalize

2009-11-11 Thread TTTTT


why not:
int smth = sqlite3_step (statement2);
while( smth == SQLITE_ROW )
{
printf( "\n command= %s result code = %d \n", command, smth );
smth = sqlite3_step (statement2);
}
so that it will work no matter how many rows



because I dont have much experience :)
that works great, thank you



> and finaly i get SQLITE_DONE but it still doesnt show me table i have
> selected...

See http://www.sqlite.org/c3ref/column_blob.html


i dont understand what exactly do you mean..

combination of these:

sqlite3_value *sqlite3_column_value(sqlite3_stmt*, int iCol);
typedef struct Mem sqlite3_value;
void sqlite3_result_value(sqlite3_context*, sqlite3_value*);

maybe?




___
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users



-- 
View this message in context: 
http://old.nabble.com/execute-or-prepare%2Bstep%2Bfinalize-tp26299247p26300548.html
Sent from the SQLite mailing list archive at Nabble.com.

___
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users


Re: [sqlite] execute or prepare+step+finalize

2009-11-11 Thread TTTTT

i think i understand why it doesnt work for select *..

because sqlite_step executes one row at time.. so after i prepare SELECT * ,
i need to use sqlite_step as many times as table i`m selecting from has rows
(in this case 3 times)..
so i made another function that looks like this:



bool create2 (char * command)

{
sqlite3_stmt * statement2;

if ( sqlite3_prepare (db, command, -1, , 0) != SQLITE_OK )
{
int err = sqlite3_prepare (db, command, -1, , 0);
const char * pErr = sqlite3_errmsg (db);
printf ("\nError %d occured! \n %s", err, pErr  );
return 1;
}
int i;
for (i=0; i<=3; i++)
{
int smth= sqlite3_step (statement2);
printf ("\n command= %s result code = %d \n",command, smth);
}
sqlite3_reset (statement2);
sqlite3_finalize (statement2);

return 0;
}



and finaly i get SQLITE_DONE but it still doesnt show me table i have
selected...


do i need to use prepare function for each command? if so, isnt then
function select_statement better to use? 


-- 
View this message in context: 
http://old.nabble.com/execute-or-prepare%2Bstep%2Bfinalize-tp26299247p26299743.html
Sent from the SQLite mailing list archive at Nabble.com.

___
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users


[sqlite] execute or prepare+step+finalize

2009-11-11 Thread TTTTT

Hello everyone!

i have been using function select_statement to execute SQL statements as
SELECT, CREATE, DROP, INSERT etc.. 
then i`ve replaced insert by bind function because you ve told me about
various advantages it has over insert.

now i have realized that in official documentation is also said that its
better to use prepared statements instead of execute function..

I wonder does it apply for all cases? 


here is select function that i`ve used:


int select_stmt(const char* stmt) {  
  char *errmsg;
  int   ret;
  int   nrecs = 0;

  first_row = 1;

  ret = sqlite3_exec(db, stmt, select_callback, , );

  if(ret!=SQLITE_OK) {
printf("Error in select statement %s [%s].\n", stmt, errmsg);
getchar ();
  }
  else {
printf("\n   %d records returned.\n", nrecs);
}
return 0;
}



and new function which uses prepared statements:


bool create (char * command)

{
sqlite3_stmt * statement2;


if ( sqlite3_prepare (db, command, -1, , 0) != SQLITE_OK )
{
int err = sqlite3_prepare (db, command, -1, , 0);
const char * pErr = sqlite3_errmsg (db);
printf ("\nError %d occured! \n %s", err, pErr  );
return 1;
}

int smth= sqlite3_step (statement2);
printf ("\n result code = %d \n", smth);


//sqlite3_reset (statement2);
sqlite3_finalize (statement2);

return 0;

}




main :


create ("CREATE TABLE two (a,b,c)");
create ("INSERT INTO two (a,b) VALUES (3, 1)");
create ("INSERT INTO two (a,b,c) VALUES (2, 8, 9)");
create ("INSERT INTO two (a,c) VALUES (4, 1)");
create ("SELECT * FROM two");




The thing is that create function does not execute last instruction:  create
("SELECT * FROM two");
it returns sqlite_row instead of sqlite_done which i would expect.. 

I would appreciate if someone could explain me this..

Thanky in advance..

T
-- 
View this message in context: 
http://old.nabble.com/execute-or-prepare%2Bstep%2Bfinalize-tp26299247p26299247.html
Sent from the SQLite mailing list archive at Nabble.com.

___
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users


Re: [sqlite] problem with inserting non NULL values into sqlite table

2009-10-30 Thread TTTTT


hey! Thank you very much.. 
i wanted to insert string and i have forgotten about a fact i need to use
these quotes  ' '.. 
that has solved the problem...
Thank you..



Nataraj, 
 
i am using sqlite3_exec function to get error... and yes, call back is one
of arguments passed to the function..
check this:   http://www.sqlite.org/c3ref/exec.html  



-- 
View this message in context: 
http://old.nabble.com/problem-with-inserting-non-NULL-values-into-sqlite-table-tp26114852p26127196.html
Sent from the SQLite mailing list archive at Nabble.com.

___
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users


[sqlite] problem with inserting non NULL values into sqlite table

2009-10-29 Thread TTTTT

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

open_db ("db_name"); 

cout<<"Insert table name:"<>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.. 
here it is:




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 auggestions very appreciated..

T








-- 
View this message in context: 
http://www.nabble.com/problem-with-inserting-non-NULL-values-into-sqlite-table-tp26114852p26114852.html
Sent from the SQLite mailing list archive at Nabble.com.

___
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users


Re: [sqlite] Importing data into SQLite

2009-09-24 Thread TTTTT



You can use the sqlite binary to import data from a CSV file - if you do it
that way you have to make sure that your data fields in the SQLite database
match exactly in order the data in the CSV file. That's been my experience.
The other way is to do it programmatically (Java, C++, etc). The advantage
of that is you have control over the data import. Though the sqlite binary
does an excellent job of importing data, it might be better to use the
latter option if you want more control over the data. It's not a difficult
job to do.


Could you please explain how does it go by doing other way-programmatically
in c++?
I have installed sqlite and then i have found somewhere information that i
also need a sqlite++ wrapper to be able to use sqlite trought c++. so, i
have installed both, but i dont dont know how to connect sqlite and sqlite++
to each other, if i have to do it, at all.
I'm really new to all this, but i have an asignment to copy datas from c++
into sqlite and i dont know even how to start..
If someone could just tell me in short steps what do i need, then i will
find somehow solutions for  each step..

Thank you very much!
-- 
View this message in context: 
http://www.nabble.com/Importing-data-into-SQLite-tp25224025p25538942.html
Sent from the SQLite mailing list archive at Nabble.com.

___
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users