sqlite3_prepare_V2 doesn't seem to work well

I posted this question earlier, but as it is pointed to one person there is no response.
I’m trying to get the sqlite3 database working but I get stuck on the sqlite3_prepare_V2 statement.
This the relevant code:
Code: [Select]

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

int main(int argc, const char * argv[])
{
  sqlite3 *db;
  if (sqlite3_open("the_insert.db", &db))
  {
    printf("Could not open the.db\n");
    exit(-1);
  }
  printf("Database %s is open\n", "the_insert.db");
  //
  // Prepare a statement for multiple use:
  // =====================================
  //
  char sqlQuery[] = "INSERT INTO someTable (second, third) VALUES (?,?)";
  sqlite3_stmt *stmt;
  if (sqlite3_prepare_v2(db, sqlQuery, -1, &stmt, NULL))
  {
    printf("Error executing prepare statement\n");
    sqlite3_close(db);
    exit(-1);
  }

The message is:
Database the_insert.db is open
Error executing prepare statement.

Rest asure that someTable is in place and with the expected structure.

What is going on with that statement: “sqlite3_prepare_v2”?
I’ve searched all I can with mr G. but no luck.
Any help will be welcome.

What error code does the prepare return?

I found 3 different GitHub repositories that contain a file named "sqlite3.h". Perhaps you'd be kind enough to tell us which one YOU'RE using. Provide a clickable link to the GitHub.

@wildbill,
When I use “sqlite3_errmsg(db)” I only get the table name, which is of no use in this case.
Are there more specific codes in sqlite3_errmsg(…)?

@gfvalvo
I use the library <sqlite3.h> that comes with Xcode. Oh, Oh, do I have to distrust Xcode in this way?

pagemaker:
When I use "sqlite3_errmsg(db)" I only get the table name, which is of no use in this case.
Are there more specific codes in sqlite3_errmsg(...)?

I don't know, but I do note that the call to prepare returns a numeric error which might give a clue as to what's failing.

@wildbill,
Yes, it reports a 1; and that seems to be just the boolean value true.
Anyway it was a good try.

pagemaker:
@gfvalvo
I use the library <sqlite3.h> that comes with Xcode.

That means absolutely nothing to me. So, without being able to see the source code of the function you’re trying to call, I can offer no advice.

After a lot of research I found a solution; here is the working code part:

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

int main(int argc, const char * argv[])
{
  // Specify the complete path seems necessary! The error code raises
  // in sqlite3_prepare_v2()
  const char path[] = "/Users/janhkila/SQL-Lite/the_insert.db";
    
//  char *pErrMsg = 0;
  sqlite3 *db;
  if (sqlite3_open_v2(path, &db, SQLITE_OPEN_READWRITE, NULL))
  {
    printf("Could not open the_insert.db\n");
    exit(-1);
  }
  printf("Database %s is open\n", "the_insert.db");
  //
  // Prepare a statement for multiple use:
  // =====================================
  //
  const char *query = "INSERT INTO someTable (sHost,sPath,sPort,sName) VALUES (?,?,?,?)";
  int sqlSize = (int)strlen(query) + 1;
  sqlite3_stmt *stmt;
  if (sqlite3_prepare_v2(db, query, sqlSize, &stmt, NULL))
  {
    printf("%s: %s\n", sqlite3_errstr(sqlite3_extended_errcode(db)), sqlite3_errmsg(db));
    sqlite3_close(db);
    exit(-1);
  }
  printf("prepared\n");

The output is:
Database the_insert.db is open
prepared
Program ended with exit code: 0

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.