sqlite3 c ++中的主键错误

时间:2015-11-19 06:29:45

标签: c++ sqlite

编译和执行时的代码显示了这一点:

  
      
  1. 成功打开数据库
  2.   
  3. SQL错误:靠近“;”:语法错误
  4.   
  #include <iostream>
  #include <fstream>
  #include <string>
  #include <vector>
  #include <stdlib.h>
  #include <cstring>
  #include <sstream>
  #include <sqlite3.h>
  #include <stdio.h>

using namespace std;

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

static void createTrackTable(){
 sqlite3 *db;
 char *zErrMsg = 0;
 int  rc;
 char *sql;

 rc = sqlite3_open("idk.db", &db);
 if( rc ){
  fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
  exit(0);
}else{
  fprintf(stdout, "Opened database successfully\n");
}

sql = "CREATE TABLE TRACK (" \
  "CD_ID INTEGER NOT NULL," \
  "TRACK_ID INTEGER NOT NULL," \
  "TITLE VARCHAR(70) NOT NULL," \
  "PRIMARY KEY(CD_ID, TRACK_ID);";

  rc = sqlite3_exec(db, sql, callback, 0, &zErrMsg);
  if( rc != SQLITE_OK ){
   fprintf(stderr, "SQL error: %s\n", zErrMsg);
   sqlite3_free(zErrMsg);
 }else{
  fprintf(stdout, "Track table created successfully\n");
}
sqlite3_close(db);
}

int main(){
  createTrackTable();

  return 0;

}

我相信当我声明PRIMARY KEY(CD_ID,TRACK_ID)

时会发生错误

请非常感谢任何帮助。谢谢!

1 个答案:

答案 0 :(得分:2)

查询中

缺少最后一个。)