C中mysql_query(INSERT INTO table() values(ch,……)

johndiyang 2006-05-12 03:15:05
C语言中mysql_query(INSERT INTO table() values(ch,……)
ch 的定义是char ch=‘a’;
上边的执行为什么会出错?怎么样通过参数变量插入记录?

mysql_query(INSERT INTO table() values('ch',……) 这一句不会出错!
...全文
621 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
johndiyang 2006-05-17
  • 打赏
  • 举报
回复
谢谢各位的指点!
dreamXren 2006-05-15
  • 打赏
  • 举报
回复
这样组装一下:
char buffer[256];
memset(buffer, 0, sizeof(buffer));
sprintf(buffer, "INSERT INTO table() values('%c')", ch);
mysql_query(buffer);
mao_kern 2006-05-14
  • 打赏
  • 举报
回复
22.2.3.50. mysql_query()
int mysql_query(MYSQL *mysql, const char *query)

Description

Executes the SQL query pointed to by the null-terminated string query. Normally, the string must consist of a single SQL statement and you should not add a terminating semicolon (‘;’) or \g to the statement. If multiple-statement execution has been enabled, the string can contain several statements separated by semicolons. See Section 22.2.9, “C API Handling of Multiple Query Execution”.

mysql_query() cannot be used for queries that contain binary data; you should use mysql_real_query() instead. (Binary data may contain the ‘\0’ character, which mysql_query() interprets as the end of the query string.)

If you want to know whether the query should return a result set, you can use mysql_field_count() to check for this. See Section 22.2.3.22, “mysql_field_count()”.

Return Values

Zero if the query was successful. Non-zero if an error occurred.

mao_kern 2006-05-12
  • 打赏
  • 举报
回复
mysql_query(mysql_handler,"insert into table_1(..,..) values('a',..)");
mysql_query第2个参数要求字符串,看看API呀
johndiyang 2006-05-12
  • 打赏
  • 举报
回复
只要把字符串修改成变量他就会报错。
比如:you have errror in your sql syntax "&ch,now())"
或者 unkonw column 'ch' in(field list)
johndiyang 2006-05-12
  • 打赏
  • 举报
回复
恩,我是用'ch'测试数据库连接是不是正确。
但是怎么通过变量来插入?
johndiyang 2006-05-12
  • 打赏
  • 举报
回复
mao_kern()
谢谢指点,我看了API,可是还没有看明白。
书上的API例子都是插入值,没有调用变量的!
itblog 2006-05-12
  • 打赏
  • 举报
回复
反正是char型的变量都要加上'',这是约定。
mysql_query(INSERT INTO table() values('ch',……)
-----
你这样写插入的不应该是ch变量的值,应该是ch值吧~

56,685

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧