【问题标题】:Execute SQLite statement with a parameter执行带参数的 SQLite 语句
【发布时间】:2016-03-02 16:35:20
【问题描述】:

我在 C 中有这个方法,我从表中删除一行。我想传递一个作为主键的参数,然后删除该行。

这是我的方法:

void eliminarAlumno(char *mat) {
    abrirBD();
    error = sqlite3_exec(conexion, "delete from alumnos WHERE matricula='<mat>'", 0, 0, &msjError);
    comprobarError(error, msjError);
    cerrarBD();
}

其中&lt;mat&gt; 是参数。我该怎么做?

【问题讨论】:

  • C 不支持方法,只支持函数

标签: c string sqlite printf


【解决方案1】:

您不能直接执行此操作。您需要首先获取一个数组,使用sprintf()/snprintf() 填充所需的字符串并将其打印到数组中,然后将该数组用作sqlite3_exec() 的参数。

类似

char argument[128] = {0};   //make sure the final array fits in here
sprintf(argument, "delete from alumnos WHERE matricula='%s'", mat);
error = sqlite3_exec(conexion, argument, 0, 0, &msjError);

应该做的工作。

【讨论】:

  • Sourav,因为mat 是一个字符串,而数据库字段也是一个字符串,所以您必须在SQL 查询中使用单引号将该字段括起来。我将其作为编辑添加到您的解决方案中。
  • 谢谢,它成功了,但是sqlite3_exec 的第二个参数应该是argument 而不是mat
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-01
  • 1970-01-01
  • 2015-05-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多