【问题标题】:SQLite invalid conversion from ‘char’ to ‘const char*’ [-fpermissive] in CPPSQLite 在 CPP 中从 ‘char’ 到 ‘const char*’ [-fpermissive] 的无效转换
【发布时间】:2017-03-20 17:45:26
【问题描述】:

需要通过用户提供的新值(姓名和地址)来更新数据库。查询中的错误是:

  1. 错误:从‘char’到‘const char*’的无效转换 [-fpermissive] sqlite3_bind_text(res, 2, *c2)

    1. 错误:函数“int sqlite3_bind_text(sqlite3_stmt*, int, const char*, int, void ()(void))”的参数太少 sqlite3_bind_text(res, 2, *c2)

我的代码是:

        const char *c1 = updatedName.c_str();
        const char *c2 = updatedAdd.c_str();

char *sql = ("UPDATE RECORDS SET NAME = ? AND ADDRESS = ? WHERE ACCOUNT_No = ?");
        rc = sqlite3_prepare_v2(db, sql, -1, &res, 0);
        sqlite3_bind_text(res, 1, *c1);
        sqlite3_bind_text(res, 2, *c2);
        sqlite3_bind_int(res, 3, acc);
        rc = sqlite3_step(res);
        sqlite3_finalize(res);

【问题讨论】:

  • 2.编译器告诉您sqlite3_bind_text 接受 5 个参数,但您只提供 3 个,因此缺少 2 个参数。
  • @Jean-FrançoisFabre 它向函数 'int sqlite3_bind_text(sqlite3_stmt*, int, const char*, int, void ()(void))' 抛出的参数太少sqlite3_bind_text(res, 1, c1, 0);这个错误
  • 抱歉,函数必须作为最后一个参数传递。检查文档...一些示例:stackoverflow.com/questions/19927188/…
  • 1.这可能是因为参数不匹配,当没有提供足够的参数时。正如 Jean-François 所说,请阅读手册页。
  • 另外,*c1*c2 的类型为const char,函数要求const char*

标签: c sqlite


【解决方案1】:

sqlite3_bind_text() 想要一个指向整个字符串的指针,而不仅仅是第一个字符。 (你需要了解 C 指针和字符串(字符数组)是如何工作的。)

sqlite3_bind_text() documentation 告诉你使用五个参数:

sqlite3_bind_text(res, 1, updatedName.c_str(), -1, SQLITE_TRANSIENT);

【讨论】:

    猜你喜欢
    • 2016-01-20
    • 2014-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-30
    • 2014-07-30
    相关资源
    最近更新 更多