在x64 TDM下sqlite3_prepare错误c ++ cygwin

时间:2013-01-27 00:54:20

标签: gcc sqlite

我正在编译一个使用sqlite3的示例应用程序 我的Windows x64机器上的代码。

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

int main(int argc,char **argv)
{
  sqlite3 *db;
  sqlite3_stmt * pSqllite3_stmt=NULL;
  char *zErrMsg=0;
  int rc;

  // create a new database //
  rc =sqlite3_open( ".\\sql_lite3.db" ,&db);
  if(rc!=SQLITE_OK|| db == NULL)
  {
      MessageBox(NULL,TEXT("can't open the database") , TEXT("sql_lite example"), MB_OK);
      exit(0);
  }

  // create a table called string_index //
  char * create_query = "create table string_table (index INT ,string TEXT);" ;
  rc = sqlite3_prepare_v2( db , create_query, -1, &pSqllite3_stmt,NULL);
  if (rc != SQLITE_OK ) { 
    // if an error occurred //
    char error_code[128];
    sprintf(error_code," Error Code is %d" ,rc);

    MessageBox( NULL, error_code,TEXT("sql_lite example"), MB_OK);
    exit(0);
  }

  // step the create query //
  rc = sqlite3_step(pSqllite3_stmt);
  if(SQLITE_OK != rc)
  {
    MessageBox(NULL,TEXT("Create step have failed "),TEXT("sql lite example"),MB_OK);
    exit(0);  
  }


  // insert records to the database //
  char *insert_query = new char [ 1024];
  int i;
  sqlite3_stmt * insert_stmt ;
  char * insert_string ="This is the insert String";
  for (i=1; i<= 10;i++){
    sprintf( insert_query,"INSERT INTO string_table(index,string) VALUES(%d,?1);" , \
                   i);
    // execute the insert query //
    rc = sqlite3_prepare_v2(db , insert_query,strlen(insert_query), &insert_stmt,NULL);
    if(rc!= SQLITE_OK)
    {
      MessageBox(NULL,TEXT("INSERT INTO have been failed"), TEXT("SQLITE EXAMPLE"),MB_OK);
      exit(0);
    }

    // bind the string //
    rc =sqlite3_bind_text(insert_stmt,1,insert_string,sizeof(insert_string),SQLITE_TRANSIENT);
    if(rc != SQLITE_OK && rc != SQLITE_DONE)
    {
      MessageBox(NULL,TEXT("bind failed"),TEXT("SQLITE EXAMPLE"), MB_OK);
      exit(0);
    }
    // call step //
    rc = sqlite3_step(insert_stmt);
    if( rc != SQLITE_OK)
    {
      MessageBox(NULL,TEXT("Insertion step failed "),TEXT("SQLITE EXAMPLE"),MB_OK);
      exit(0);
    }

    // finalize the insert query //
    sqlite3_finalize(insert_stmt);
  } 

  // now we are going to execute the search query //
  char * search_query = "SELECT * FROM string_table WHERE index=10";
  sqlite3_stmt * search_stmt ;
  rc = sqlite3_prepare_v2(db,search_query,sizeof(search_query),&search_stmt,NULL); 
  if( SQLITE_OK== rc)
  {
    while( SQLITE_ROW == sqlite3_step(search_stmt ) )
    {
      // print the record //
      const int  index_value= sqlite3_column_int(search_stmt,1);
      const unsigned char * string_value = sqlite3_column_text(search_stmt,2);
      printf( "index :%d and string:%s ",index_value,string_value );

    }
  }else{
    MessageBox(NULL,TEXT("Searching query sqlite3 have been failed"),TEXT("SQLITE example"), MB_OK);
    exit(0);
  }

  // fnalize the search statement //
  sqlite3_finalize(search_stmt);

  // don't forget to close the connection //
  sqlite3_close(db);

  return 0;
 }

但是当我尝试创建一个表时,它总是给我错误代码1。 这意味着,波纹管代码失败。

 // create a table called string_index //
  char * create_query = "create table string_table (index INT ,string TEXT);" ;
  rc = sqlite3_prepare_v2( db , create_query, -1, &pSqllite3_stmt,NULL);
  if (rc != SQLITE_OK ) { 
    // if an error occurred //
    char error_code[128];
    sprintf(error_code," Error Code is %d" ,rc);

    MessageBox( NULL, error_code,TEXT("sql_lite example"), MB_OK);
    exit(0);
  }

为什么?我怎么能克服这个问题?

有任何建议或改进吗?

- 提前致谢 -

1 个答案:

答案 0 :(得分:1)

要显示有用的错误消息,请使用sqlite3_errmsg

MessageBox(0, sqlite3_errmsg(db), NULL, 0);

至于你的SQL命令:indexkeyword,因此你必须在使用它时引用它:

CREATE TABLE string_table("index" INT, string TEXT);

使用其他列名称可能更容易。