I am trying to use and understand SQLite syntax to use in my C program. I need to figure how to insert a pre-existing variable into my database. For example "insert into MyTable values (MyVariable)". However I do not believe I can do this without doing something special because I have not found the answer yet. I believe I have to do some sort of binding?? so I have been reading and experimenting on how to do this but I have not been successful thus far. If anybody knows how to do this any help is appreciated, thanks.
SQLite help. Inserting a variable into database
Page 1 of 15 Replies - 3636 Views - Last Post: 01 February 2013 - 12:41 PM
Replies To: SQLite help. Inserting a variable into database
#2
Re: SQLite help. Inserting a variable into database
Posted 29 January 2013 - 03:05 AM
from the SQLite-side you can only check whether the SQL syntax is right. for making a connection and to query SQLite you would need the appropriate C-library.
#3
Re: SQLite help. Inserting a variable into database
Posted 29 January 2013 - 10:34 AM
Quote
from the SQLite-side you can only check whether the SQL syntax is right. for making a connection and to query SQLite you would need the appropriate C-library.
Can you explain this more. This is the correct C-library I would use to bind values to prepared statements. C library
prepare statement
Im not sure how to bind my variable into an insert statement or if binding is the correct way to go.
#4
Re: SQLite help. Inserting a variable into database
Posted 29 January 2013 - 10:06 PM
int sqlite3_prepare( sqlite3 *db, /* Database handle */ const char *zSql, /* SQL statement, UTF-8 encoded */ int nByte, /* Maximum length of zSql in bytes. */ sqlite3_stmt **ppStmt, /* OUT: Statement handle */ const char **pzTail /* OUT: Pointer to unused portion of zSql */ );
I see this looks like the most basic prepare statement can someone explain where my statement would go and how it should look, would it look like "insert into table (fieldname) values (?)" and how would I get my variable in place of the question mark
This post has been edited by ry110891: 29 January 2013 - 10:07 PM
#5
Re: SQLite help. Inserting a variable into database
Posted 30 January 2013 - 07:37 AM
I can’t help with specific C code (not my language) but you don’t replace the ? yourself explicitly. this is the responsibility of the library. the only thing you have to do yourself is telling the library, which value/variable is bound to which ?. the normal procedure of a Prepared Statement is
1) prepare SQL
2) bind values
3) execute Statement
1) prepare SQL
2) bind values
3) execute Statement
#6
Re: SQLite help. Inserting a variable into database
Posted 01 February 2013 - 12:41 PM
so this is what I came up with but it still did not read the value of my variable into my database. Anybody have an idea what I am doing wrong or how to fix this?
sqlite3 *db; sqlite3_stmt *stmt; const char *zSql; sqlite3_open("UserDatabase.db", &db); zSql = "insert into meal_1 (Description) values (?)"; sqlite3_prepare( db, zSql, // stmt strlen (zSql) + 1, &stmt, NULL // Pointer to unused portion of stmt ); sqlite3_bind_text ( stmt, 1, myVariable, strlen (value), // length of text 0 ); sqlite3_step(stmt); int sqlite3_close(sqlite3*);
Page 1 of 1