C ++ SQLite3查询问题尚未解决

时间:2018-04-03 03:36:20

标签: c++ sql sqlite

我对SQL和C ++有些新意。我相信我提出的问题会产生一个简单的解决方案。

我必须创建一个sql查询,将一些值插入到现有表(const char * sql_query)中,并使用混合字符串和浮点值。我的问题是将字符串汇编操作固定下来,而不会出现编译器错误。

这是我的尝试:

v1.10.16

特别是这是我的问题:

int main(int argc, char* argv[])
{
    create_entry("mahut", "topa", "suma", 5.55, 6.66, 7.77);

    return 0;
}

void create_entry(char col1[], char col2[], char col3[], float col4, float col5, float col6)
{
    cout << ("create_entry") << endl;

    sqlite3 *db;
    char *zErrMsg = 0;
    int rc;
    char *sql;
    char db_name[] = "db_test.db";
    stringstream sql_query;

    // assemble string
    sql_query << "INSERT INTO Department1 (Date,Time,Accept,Factor1,Factor2,Factor3) VALUES ('" << col1 << "','" << col2 << "','" << col3 << "','" << col4 << "','" << col5 << "','" << col6 << "')";
    string str = sql_query.str();

    rc = sqlite3_open(db_name, &db);

    if( rc )
    {
        fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
    }
    else
    {
        fprintf(stderr, "Opened database successfully\n");
    }

    // execute SQL statement
    rc = sqlite3_exec(db, str, callback, 0, &zErrMsg);

    if( rc != SQLITE_OK )
    {
        fprintf(stderr, "SQL error: %s\n", zErrMsg);
        sqlite3_free(zErrMsg);
    }
    else
    {
        fprintf(stdout, "Records created successfully\n");
    }
    sqlite3_close(db);
}

static int callback(void *db, int argc, char **argv, char **azColName)
{
    cout << "Debug <in callback>" << endl;
    for(int i=0; i<argc; ++i)
    {
        //printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
        cout << ("%s", argv[i] ? argv[i] : "NULL") << endl;
    }
    tbl_exists = true;

    return 0;
}

我不明白是否有正确的方法将float,integer和string混合成const char * 我不确定是什么问题,但就像我说我已经尝试了一段时间来正确组装该字符串但失败了,因为我只是没有掌握C ++中的指针和字符串。由于跟随错误

,编译失败
sql_query << "INSERT INTO Department1 (Date,Time,Accept,Factor1,Factor2,Factor3) VALUES ('" << col1 << "','" << col2 << "','" << col3 << "','" << col4 << "','" << col5 << "','" << col6 << "')";

我也收到以下警告

error: cannot convert ‘std::__cxx11::string {aka std::__cxx11::basic_string<char>}’ to ‘const char*’ for argument ‘2’ to ‘int sqlite3_exec(sqlite3*, const char*, int (*)(void*, int, char**, char**), void*, char**)’   rc = sqlite3_exec(db, str, callback, 0, &zErrMsg);

2 个答案:

答案 0 :(得分:0)

对于您的错误,您错过了将{std :: string转换为C样式字符串的.c_str()转换。 sqlite3_exec采用C风格的const char *字符串,而不是C ++字符串。

要获得警告,您需要将参数声明为create_entry() const char *(或const char []) - 因为它们是文字(不应修改的常量)。将它们声明为非常量会让您意外地修改它们。

答案 1 :(得分:0)

如果您根本不这样做,那么组装字符串会变得更容易:

void create_entry(const char col1[], const char col2[], const char col3[],
                  float col4, float col5, float col6)
{
    sqlite3 *db;
    int rc;
    const char *sql;
    static const char db_name[] = "db_test.db";
    sqlite3_stmt *stmt;

    sql = "INSERT INTO Department1 (Date,Time,Accept,Factor1,Factor2,Factor3)"
          " VALUES (?,?,?,?,?,?)";

    rc = sqlite3_open(db_name, &db);
    if( rc != SQLITE_OK )
    {
        fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
        return;
    }

    rc = sqlite3_prepare_v2(db, sql, -1, &stmt, NULL);
    if( rc != SQLITE_OK )
    {
        fprintf(stderr, "Can't prepare query: %s\n", sqlite3_errmsg(db));
        sqlite3_close(db);
        return;
    }

    sqlite3_bind_text(stmt, 1, col1, -1, SQLITE_STATIC);
    sqlite3_bind_text(stmt, 2, col2, -1, SQLITE_STATIC);
    sqlite3_bind_text(stmt, 3, col3, -1, SQLITE_STATIC);
    sqlite3_bind_double(stmt, 4, col4);
    sqlite3_bind_double(stmt, 5, col5);
    sqlite3_bind_double(stmt, 6, col6);

    rc = sqlite3_step(stmt);
    if( rc != SQLITE_DONE )
    {
        fprintf(stderr, "Can't execute query: %s\n", sqlite3_errmsg(db));
        sqlite3_finalize(stmt);
        sqlite3_close(db);
        return;
    }

    sqlite3_finalize(stmt);
    sqlite3_close(db);
}