sqlite3_exec()!= SQLITE3_OK. 尝试在 OOP 中转换不起作用

sqlite3_exec()! = SQLITE3_OK. Trying to tranfer in OOP won't work

本文关键字:OOP 转换 不起作用 SQLITE3 exec sqlite3 OK      更新时间:2023-10-16

我正在尝试使用C++中的数据库。我制作了一个程序来打开数据库,然后在过程编程中创建表。尝试在 OOP 中制作时,sqlite3_exec() != SQLITE_OK

我是新手,所以要温柔。

这是主要.cpp:

#include <iostream>
#include "sqlite3.h"
#include "Table.h"
using namespace std;
int openDatabase(sqlite3 *db);
int main() {
    sqlite3 *db;
    string columnValues, rowValues;   // these are for query
    Table Personal;
    Personal.SettableName("PERSONAL");
    columnValues = "NUME TEXT, ID TEXT";   // this is just an example
    openDatabase(db);
    Personal.createTable(db, columnValues);
    sqlite3_close(db);
    return 0;
}

表.cpp

#include <string>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "Table.h"
#include "sqlite3.h"
using namespace std;
static int callback(void *data, int argc, char **argv, char **azColName) {
    int i;
    fprintf(stderr, "%s: ", (const char*)data);
    for(i = 0; i < argc; i++) {
        printf("%s = %sn", azColName[i], argv[i] ? argv[i] : "NULL");
    }
    printf("n");
    return 0;
}
void executeSqlStatement(sqlite3 *db,const char* sql) {
    int rc = 0 ;
    char *zErrMsg = 0;
    rc = sqlite3_exec(db, sql, callback, 0, &zErrMsg);  // Here it doesn't work, rc=21;
    if( rc != SQLITE_OK ) {
        fprintf(stderr, "SQL error: %sn", zErrMsg);
        sqlite3_free(zErrMsg);
    } 
    else {
        fprintf(stdout, "Operation done successfullyn");
    }
}
void Table::createTable(sqlite3 *db, string columnDetails) {
    this->sqlCommand =  "CREATE TABLE ";
    this->sqlCommand += (this->tableName + " (" + columnDetails + ");");
    executeSqlStatement(db, this->sqlCommand.c_str());
    printf(sqlCommand.c_str());
}

和表.h

#include "sqlite3.h"
using namespace std;
void executeSqlStatement(sqlite3 *db);
static int callback(void *data, int argc, char **argv, char **azColName);
class Table
{
public:
    void SettableName(string val){tableName = val;}
    void createTable(sqlite3 *db, string columnDetails);    
    string tableName;
    string sqlCommand;
};
不需要在所有

三个文件中包含sqlite3.h文件,因为两个 cpp 文件都包含Table.h,因此您只需将其包含在该文件中。

sqlite3.h 文件也是一个系统文件,因此使用 include <sqlite3.h> 而不是 include "sqlite3.h" 可以更清楚地了解文件的来源。

我建议使用 -Wall-Wextra 标志进行编译 - 起初它们似乎提出了大量投诉,但值得关注报告的问题并研究如何解决它们。

表.h

#include <sqlite3.h>
using namespace std;
void executeSqlStatement(sqlite3 *db);
int callback(void *data, int argc, char **argv, char **azColName);
class Table
{
public:
    void SettableName(string val){tableName = val;}
    void createTable(sqlite3 *db, string columnDetails);    
    string tableName;
    string sqlCommand;
};

表.cpp

#include <string>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "Table.h"
using namespace std;
 int callback(void *data, int argc, char **argv, char **azColName) {
    int i;
    fprintf(stderr, "%s: ", (const char*)data);
    for(i = 0; i < argc; i++) {
        printf("%s = %sn", azColName[i], argv[i] ? argv[i] : "NULL");
    }
    printf("n");
    return 0;
}
void executeSqlStatement(sqlite3 *db,const char* sql) {
    int rc = 0 ;
    char *zErrMsg = 0;
    rc = sqlite3_exec(db, sql, callback, 0, &zErrMsg);  // Here it doesn't work, rc=21;
    if( rc != SQLITE_OK ) {
        fprintf(stderr, "SQL error: %sn", zErrMsg);
        sqlite3_free(zErrMsg);
    } 
    else {
        fprintf(stdout, "Operation done successfullyn");
    }
}
void Table::createTable(sqlite3 *db, string columnDetails) {
    this->sqlCommand =  "CREATE TABLE ";
    this->sqlCommand += (this->tableName + " (" + columnDetails + ");");
    executeSqlStatement(db, this->sqlCommand.c_str());
    printf(sqlCommand.c_str());
}

主.cpp

#include <iostream>
#include "Table.h"
using namespace std;
int main() {
    sqlite3 *db;
    sqlite3_open_v2("test.db", &db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
    string columnValues, rowValues;   // these are for query
    Table Personal;
    Personal.SettableName("PERSONAL");
    columnValues = "NUME TEXT, ID TEXT";   // this is just an example

    Personal.createTable(db, columnValues);
    sqlite3_close(db);
    return 0;
}

在我的 github 上,我有一个代码的工作版本,有一个(借来的)makefile 将文件拉到一起,所以我只需要键入 make debug 来编译代码。

make debug && bin/debug/hello

将生成文件,如果编译成功,则运行可执行文件。