MySQL一次执行多条语句的实现及常见问题(C API Commands out of sync; you can't run this command now)
2011-06-15 21:03
1071 查看
通常情况MySQL出于安全考虑不允许一次执行多条语句(但也不报错,很让人郁闷)。MySQL是支持在单个查询字符串中指定多语句执行的,使用方法是给链接指定参数:
view plaincopy to clipboardprint?
//链接时设定
mysql_real_connect( ..., CLIENT_MULTI_STATEMENTS );
//或者
//中途指定
mysql_set_server_option( mysql, MYSQL_OPTION_MULTI_STATEMENTS_ON ); //mysql是连接的名称
当使用执行多语句功能后,一定要读完整个resault集,否则会出现错误:Commands out of sync; you can't run this command now
官方推荐的执行语句是这样的:
view plaincopy to clipboardprint?
do
{
/* Process all results */
...
printf( "total affected rows: %lld", mysql_affected_rows( mysql ) );
...
if( !( result mysql_store_result( mysql ) ) )
{
printf( stderr, "Got fatal error processing query/n" );
exit(1);
}
process_result_set(result); /* client function */
mysql_free_result(result);
}while( !mysql_next_result( mysql ) );
如果仅仅是插入等不需要返回值的SQL语句,也一样得读完整个resault集并释放,最小化的写法:
view plaincopy to clipboardprint?
do
{
result = mysql_store_result( mysql );
mysql_free_result(result);
}while( !mysql_next_result( mysql ) );
view plaincopy to clipboardprint?
//链接时设定
mysql_real_connect( ..., CLIENT_MULTI_STATEMENTS );
//或者
//中途指定
mysql_set_server_option( mysql, MYSQL_OPTION_MULTI_STATEMENTS_ON ); //mysql是连接的名称
当使用执行多语句功能后,一定要读完整个resault集,否则会出现错误:Commands out of sync; you can't run this command now
官方推荐的执行语句是这样的:
view plaincopy to clipboardprint?
do
{
/* Process all results */
...
printf( "total affected rows: %lld", mysql_affected_rows( mysql ) );
...
if( !( result mysql_store_result( mysql ) ) )
{
printf( stderr, "Got fatal error processing query/n" );
exit(1);
}
process_result_set(result); /* client function */
mysql_free_result(result);
}while( !mysql_next_result( mysql ) );
如果仅仅是插入等不需要返回值的SQL语句,也一样得读完整个resault集并释放,最小化的写法:
view plaincopy to clipboardprint?
do
{
result = mysql_store_result( mysql );
mysql_free_result(result);
}while( !mysql_next_result( mysql ) );
相关文章推荐
- MySQL一次执行多条语句的实现及常见问题(C API Commands out of sync; you can't run this command now)
- PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now 问题的解决
- PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now
- php用mysql_query()调用存储过程后无法再执行sql报错:commands out of sync you can't run this command now
- python mysql 2014 Commands out of sync; you can't run this command now
- LR错误: Commands out of sync; you can't run this command now 解决方法
- 解决error:2014 Commands out of sync; you can't run this command now
- 解决mysql error:2014 Commands out of sync; you can't run this command now
- PHP+MySQL连续插入数据报错:Commands out of sync; you can't run this command now & 读取Excel数据并批量插入到数据中
- 解决error:2014 Commands out of sync; you can't run this command now
- PHP执行MYSQL存储过程报错:Commands out of sync; you can't run this command now
- 解决error:2014 Commands out of sync; you can't run this command now
- error:2014 Commands out of sync; you can't run this command now
- Commands out of sync; you can't run this command now
- mysql 出现 Commands out of sync; you can't run this command now
- Mysql::Error: Commands out of sync; you can't run this command now: SHOW TABLES解决方案
- C++ Mysql调用 错误:2014 :Commands out of sync; you can't run this command now
- mysql: Commands out of sync; you can't run this command now
- Commands out of sync; you can't run this command now SQL
- ERROR (ProgrammingError) (2014, "Commands out of sync; you can't run this command now")