Hi, I have just started out with sqlite in c but I can't seem to return the results of the select statements. There are so many functions that I don't know what to use as none of them make any sense. So can anybody give me an example of how to select from a sqlite database using the sqlite c api. Thanks.

Recommended Answers

All 7 Replies

Yes it is c++. Just ignore the c++ stuff and concentrate on how to do the same in C.

I translated it into C and posted it in that same thread. Scroll to the last post in that thread to view it.

Hi Ancient Dragan. I have just checked your script and can't see how it displays the data from the table. Is there meant to be more code after the sqlite3_exec function? Also I though I would ask is it possible to replace the callback function with just the integer 0 as I have been doing or is there something special with the callback function?

Thanks.
cwarn23

int callback(void *NotUsed, int argc, char **argv, char **azColName){
  int i;
  for(i=0; i<argc; i++){
    printf("%s = ",azColName[i]);
    if( argv[i] )
        printf(argv[i]);
    else
        printf("NULL");
     printf("\n");
  }
  return 0;
}

void display(sqlite3* db, char* tbname)
{
    char* db_err = 0;
    char select[255] = {0};
    if( strlen(tbname) == 0)
    {
        GetTablename(tbname);
        if( tbname[0] == '\0' )
            return;
    }
    if( strlen(tbname) > 0)
    {
        sprintf(select, "select * from %s;", tbname);
        sqlite3_exec(db, select, callback, 0, &db_err);
        dsperr(&db_err);
    }

}

>>Is there meant to be more code after the sqlite3_exec function

No. The code I posted is a complete working example.

>>is it possible to replace the callback function with just the integer 0
I'm not sure what you are asking. The callback function is called for each row in the resultset.

Hi

>>> Also I though I would ask is it possible to replace the callback function with just the integer 0 as I have been doing or is there something special with the callback function?

The link given by gerard4143 shows an example where callback function isn't further necessary (modern version of SQLite in 5 Minutes)

-- tesu

commented: right +33
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.