On Sun, 11 Jan 2015 20:00:03 +0000
Paul via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com> wrote:

> Can someone please tell me what I'm doing wrong here, the sql 
> INSERT statement fails for some reason. I don't fully understand 
> the callback function yet (I borrowed this from a C tutorial on 
> the subject), maybe that is the source of the problem?
> 
> 
> import etc.c.sqlite3;
> import std.stdio;
> 
> //stub
> extern(C) int aCallback(void *n, int c, char **v, char **col)
> {
>        return 0;
> }
> 
> void main(){
>       
>       sqlite3 *db;
>       int result = sqlite3_open("myDatabase.db", &db);
>       
>       if (result) {   
>               writeln("Failed to open database");
>               return; 
>       }
>       
>       //create table  
>       char *msg = null;
>       result = sqlite3_exec(db, "CREATE TABLE people('id INT PRIMARY 
> KEY NOT NULL, surname TEXT NOT NULL');", &aCallback, null, &msg);
>       if (result) {   
>               writeln("Failed to create table");
>               
>               //tidy up on exit
>               sqlite3_close(db);
>               return;         
>       }
>       
>       //insert record
>       msg = null;
>       result = sqlite3_exec(db, "INSERT INTO people (id, surname) 
> VALUES (1, 'Smith');", &aCallback, null, &msg);
>       if (result) {   
>               writeln("Failed to insert record");
>               
>               //tidy up on exit
>               sqlite3_close(db);              
>               return; 
>               
>       }       
> 
>       sqlite3_free(msg);
>       sqlite3_close(db);      
> 
> }
> 
> Many thanks
> 
> Paul
if you'll output the error message, you'll see something unusual here:

  if (result) {
    import std.conv : to;
    writeln("Failed to insert record: ", to!string(msg));
    //tidy up on exit
    sqlite3_close(db);          
    return;     
  }     

"Failed to insert record: table people has no column named id"

wow! but it has! or isn't it? yep, it hasn't. the error is here:
>       result = sqlite3_exec(db, "CREATE TABLE people('id INT PRIMARY 
> KEY NOT NULL, surname TEXT NOT NULL');", &aCallback, null, &msg);

`CREATE TABLE people('...')` is not the syntax you want. i don't know
why sqlite is not rejecting it, but the correct one is this:

  result = sqlite3_exec(db, "CREATE TABLE people(id INT PRIMARY "~
    "KEY NOT NULL, surname TEXT NOT NULL);", &aCallback, null, &msg);

note the single quotes in your code: that is where it all goes wrong. i
don't know where you got that quotes from, but this is not a valid SQL
syntax for `CREATE TABLE`. ;-)

Attachment: signature.asc
Description: PGP signature

  • Sqlite Paul via Digitalmars-d-learn
    • Re: Sqlite ketmar via Digitalmars-d-learn
    • Re: Sqlite Paul via Digitalmars-d-learn

Reply via email to