5 Replies - 2028 Views - Last Post: 01 February 2013 - 12:41 PM Rate Topic: -----

#1 ry110891  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 25
  • Joined: 24-May 12

SQLite help. Inserting a variable into database

Posted 29 January 2013 - 12:44 AM

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.

Is This A Good Question/Topic? 0
  • +

Replies To: SQLite help. Inserting a variable into database

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3572
  • View blog
  • Posts: 10,414
  • Joined: 08-June 10

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.
Was This Post Helpful? 0
  • +
  • -

#3 ry110891  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 25
  • Joined: 24-May 12

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.
Was This Post Helpful? 0
  • +
  • -

#4 ry110891  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 25
  • Joined: 24-May 12

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

Was This Post Helpful? 0
  • +
  • -

#5 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3572
  • View blog
  • Posts: 10,414
  • Joined: 08-June 10

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
Was This Post Helpful? 0
  • +
  • -

#6 ry110891  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 25
  • Joined: 24-May 12

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

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1