这是我的(非常简单的代码)。 我要执行的语句是: 创建表“T_COMPANY”(“ID”整数不为空,“名称”文本不为空,主键(“ID”自动增量))
正如我生成的日志所证实的那样,上面的代码正确地组成了查询:
- SQLITE prepare statement error.
- Error code: 21
- Statement:
CREATE TABLE 'T_COMPANY' ('ID' INTEGER NOT NULL,'NAME' TEXT NOT NULL, PRIMARY KEY('ID' AUTOINCREMENT))
- Address: (nil)
static int sqlInitDatabase(sqlite3 *pDb, const char *dbfile)
{
int rc;
if(SQLITE_OK != (rc = sqlite3_initialize())) {
fprintf(stderr, "- Error initializing SQLITE engine. Error code: %d\n Aborting.\n", rc);
return rc;
}
if(SQLITE_OK != (rc = sqlite3_open_v2(dbfile, &pDb, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL))) {
fprintf(stderr, "- Fail to load or create DB file %s.\n- Error code: %d\n Aborting.\n", dbfile, rc);
return rc;
}
return rc;
}
static void sqlCloseDatabase(sqlite3 *pDb)
{
if(pDb != NULL) {
sqlite3_close(pDb);
sqlite3_shutdown();
}
}
int sqlDeployDatabase(char *name)
{
sqlite3 *pDb;
sqlite3_stmt *stmt;
char *query;
int rc;
if(name == NULL || strcmp(name, "") == 0) {
fprintf(stderr, "- Database name cannot be empty.\n- Aborting\n");
return -1;
}
if(SQLITE_OK != (rc = sqlInitDatabase(pDb, name))) {
return rc;
}
for(int i = 0; i <= NUM_TABLES; i++) {
if(!(query = (char *)malloc(sizeof(char) * SQL_STMT_BUF))) {
sqlCloseDatabase(pDb);
fprintf(stderr, "Unable to allocate memory for building the query. Aborting.\n");
return -1;
}
memset(query, 0, SQL_STMT_BUF);
buildCreateStmt(tables, i, query, 0);
rc = sqlite3_prepare_v2(pDb, query, -1, &stmt, NULL);
if (rc != SQLITE_OK) {
fprintf(stderr, "- SQLITE prepare statement error.\n- Error code: %d\n- Statement:\n %s\n- Address: %p\n", rc, query, stmt);
free(query);
sqlCloseDatabase(pDb);
return rc;
}
rc = sqlite3_step(stmt);
if (rc != SQLITE_DONE) {
fprintf(stderr, "- SQLITE step error.\n- Error code: %d\n- Statement:\n %\ns", rc, query);
free(query);
sqlite3_finalize(stmt);
sqlCloseDatabase(pDb);
return rc;
}
free(query);
sqlite3_finalize(stmt);
}
sqlCloseDatabase(pDb);
}
通过互联网搜索没有帮助。 对此有什么建议吗?TY
我发现错误: static int sqlInitDatabase(sqlite3 *pDb, const char *dbfile) 必须有一个双指针作为第一个参数,否则 sqlite3_open_v2() 不会更新调用者。现在解决了。查询很完美。
static int sqlInitDatabase(sqlite3 **pDb, const char *dbfile)
{
int rc;
if(SQLITE_OK != (rc = sqlite3_initialize())) {
fprintf(stderr, "- Error initializing SQLITE engine. Error code: %d\n Aborting.\n", rc);
return rc;
}
if(SQLITE_OK != (rc = sqlite3_open_v2(dbfile, pDb, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL))) {
fprintf(stderr, "- Fail to load or create DB file %s.\n- Error code: %d\n Aborting.\n", dbfile, rc);
return rc;
}
return SQLITE_OK;
}
static void sqlCloseDatabase(sqlite3 *pDb)
{
if(pDb != NULL) {
sqlite3_close(pDb);
sqlite3_shutdown();
}
}
int sqlDeployDatabase(char *name)
{
sqlite3 *pDb;
sqlite3_stmt *stmt;
char *query;
int rc;
if(name == NULL || strcmp(name, "") == 0) {
fprintf(stderr, "- Database name cannot be empty.\n- Aborting\n");
return -1;
}
if(SQLITE_OK != (rc = sqlInitDatabase(&pDb, name))) {
return rc;
}
for(int i = 0; i < NUM_TABLES; i++) {
if(!(query = (char *)malloc(sizeof(char) * SQL_STMT_BUF))) {
sqlCloseDatabase(pDb);
fprintf(stderr, "Unable to allocate memory for building the query. Aborting.\n");
return -1;
}
memset(query, 0, SQL_STMT_BUF);
buildCreateStmt(tables, i, query, 1);
rc = sqlite3_prepare_v2(pDb, query, strlen(query) + 1, &stmt, NULL);
if (rc != SQLITE_OK) {
fprintf(stderr, "- SQLITE prepare statement error.\n- Error code: %d\n- Statement:\n %s\n- Address: %p\n", rc, query, stmt);
free(query);
sqlCloseDatabase(pDb);
return rc;
}
fprintf(stderr, "Query num %d: %s\n\n", i, query );
rc = sqlite3_step(stmt);
if (rc != SQLITE_DONE) {
fprintf(stderr, "- SQLITE step error.\n- Error code: %d\n- Statement:\n %s\n", rc, query);
free(query);
sqlite3_finalize(stmt);
sqlCloseDatabase(pDb);
return rc;
}
fprintf(stderr, "Return code: %d\n\n", rc);
free(query);
sqlite3_finalize(stmt);
}
sqlCloseDatabase(pDb);
}