[c/c++操作MySQL] mysql_query()的delete一直返回0

MySql 码拜 7年前 (2017-05-02) 2803次浏览
本人用c++去执行删除(delete)数据的语句:

	int ret = 0;
	// sql_delete_user = "delete from userinfo where username="username"";
	if (!(ret = mysql_query(mysql, sql_delete_user))){
		cout << "DELETE User Success!" << endl;
	}
	else{
		cout << " DELETE User Error!  " << endl;
	}
	cout << "******* ret = " << ret << endl;

* 无论username存不存在, mysql_query都返回0, 这让本人无法判断delete执行的情况.
* 是不是delete不能返回其他结果, 还是本人哪里出错了? 能否有其他办法判断呢?
* 帮帮本人

解决方案

20

有个 mysql_affected_rows 方法,专门获取影响的行数

20

mysql_query 返回是错误代码,假如没有执行错误,则返回0

引用

20.9.3.51. mysql_query()
int mysql_query(MYSQL *mysql, const char *stmt_str)
Description
Executes the SQL statement pointed to by the null-terminated string stmt_str. 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 20.9.12, “C API Support for Multiple Statement Execution”.
mysql_query() cannot be used for statements that contain binary data; you must use mysql_real_query() instead. (Binary data may contain the “\0” character, which mysql_query() interprets as the end of the statement string.)
If you want to know whether the statement should return a result set, you can use mysql_field_count() to check for this. See Section 20.9.3.22, “mysql_field_count()”.
Return Values
Zero if the statement was successful. Nonzero if an error occurred.

如楼上所说,使用mysql_affected_rows得到语句影响到多少条记录。

引用

20.9.3.1. mysql_affected_rows()
my_ulonglong mysql_affected_rows(MYSQL *mysql)
Description
After executing a statement with mysql_query() or mysql_real_query(), returns the number of rows changed (for UPDATE), deleted (for DELETE), or inserted (for INSERT). For SELECT statements, mysql_affected_rows() works like mysql_num_rows().
Return Values
An integer greater than zero indicates the number of rows affected or retrieved. Zero indicates that no records were updated for an UPDATE statement, no rows matched the WHERE clause in the query or that no query has yet been executed. -1 indicates that the query returned an error or that, for a SELECT query, mysql_affected_rows() was called prior to calling mysql_store_result(). Because mysql_affected_rows() returns an unsigned value, you can check for -1 by comparing the return value to (my_ulonglong)-1 (or to (my_ulonglong)~0, which is equivalent).
Errors
None.
Example
char *stmt = “UPDATE products SET cost=cost*1.25 WHERE group=10”;
mysql_query(&mysql,stmt);
printf(“%ld products updated”,
(long) mysql_affected_rows(&mysql));
For UPDATE statements, if you specify the CLIENT_FOUND_ROWS flag when connecting to mysqld, mysql_affected_rows() returns the number of rows matched by the WHERE clause. Otherwise, the default behavior is to return the number of rows actually changed.
Note that when you use a REPLACE command, mysql


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明[c/c++操作MySQL] mysql_query()的delete一直返回0
喜欢 (0)
[1034331897@qq.com]
分享 (0)