LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   can not create sqlite table with C API (https://www.linuxquestions.org/questions/programming-9/can-not-create-sqlite-table-with-c-api-907078/)

golden_boy615 10-08-2011 04:35 AM

can not create sqlite table with C API
 
hello
I am new in sqlite and this is my C code :


Code:

#include <stdio.h>
#include <sqlite3.h>
#include <string.h>


int main()
{
 sqlite3 *db;
 sqlite3_stmt *stmt;
 char *sqlitequery="create table ee (row INTEGER PRIMARY KEY,a INTEGER);";
 int rc = 0;

  rc = sqlite3_open("ehsan.db", &db);
  if( rc )
  {
    fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
  }
  else
  {
    rc = sqlite3_prepare_v2(db, sqlitequery, strlen(sqlitequery), &stmt, 0);
    printf("return value %d:",rc);
    if( rc )
    {
      fprintf(stderr, "SQL error: %d : %s\n", rc, sqlite3_errmsg(db));
    }
 }
 return 0;
}

this code makes database file for me but it does not create its table named "ee" into it.
what is wrong with it?

paulsm4 10-08-2011 10:48 AM

Two things I see off the bat:

1. You should pass a length of "strlen (sqlitequery)+1"
<= Don't forget space for the "null" terminator ;)

2. I don't see where you're executing the statement you've prepared (e.g. "sqlite3_step()")

Q: Have you considered substituting "sqlite3_exec()"?

Q: What was the sample output? Any errors or warnings?

Here's a nice, short tutorial:

http://www.yolinux.com/TUTORIALS/SQLite.html

Proud 10-08-2011 04:08 PM

Quote:

Originally Posted by paulsm4 (Post 4493181)
1. You should pass a length of "strlen (sqlitequery)+1"
<= Don't forget space for the "null" terminator ;)

You're not wrong, but it's not essential in this case ;)
Quote:

When nByte is non-negative, the zSql string ends at either the first '\000' or '\u0000' character or the nByte-th byte, whichever comes first. If the caller knows that the supplied string is nul-terminated, then there is a small performance advantage to be gained by passing an nByte parameter that is equal to the number of bytes in the input string including the nul-terminator bytes.


All times are GMT -5. The time now is 06:20 AM.