我在写C程序来访问数据库。 我最近从sqlite切换到mysql。 我不熟悉mysql c api,所以我需要帮助转换一些代码。最近从sqlite切换到mysql。需要转换一些代码c
下面的例子是用参数执行sql语句。
源码:
char *zSQL = sqlite3_mprintf("SELECT price FROM warehouse WHERE p_ID='%q'", input_value);
sqlite3_prepare_v2(handle,zSQL,-1,&stmt,0);
我尝试在MySQL:
char zSQL[60] = {'\0'};
int n = 0;
n = sprintf(zSQL, "SELECT price FROM warehouse WHERE p_ID='%s'", input_value);
mysql_real_query(conn, zSQL, n);
另一个例子是解析SQL语句的结果变量
源码:
double price_value = 0;
if (sqlite3_step (stmt) == SQLITE_ROW) {
price_value = sqlite3_column_double (stmt, 0);
}
的mysql:
MYSQL_ROW row;
while ((row = mysql_fetch_row(result)))
{
price_value = atof(row[0]);
}
虽然在MySQL的代码对我的作品,但我觉得我没有利用API就够了。 mysql c api中是否有与sqlite3_mprintf()
和sqlite3_column_double()
具有相同功能的函数?
编辑: 我对mysql_real_escape_string()
尝试:
ulong in_length = strlen(input_value);
char input_esc[(2 * in_length)+1];
mysql_real_escape_string(conn, input_esc, input_value, in_length);
char sql_stmnt[56] = {'\0'};
n = sprintf(zSQL, "SELECT price FROM warehouse WHERE p_ID='%s'", input_esc);
mysql_real_query(conn, sql_stmnt, n);
我发现这个[示例](http://mysqlresources.com/api/c/690)。这和我的很相似。我可以只'mysql_real_escape_string()''input_value'吗? – 2012-03-20 04:42:50
看看我的帖子,在'mysql_real_escape_string()' – 2012-03-20 05:23:06
是我的尝试,这看起来是对的 – hroptatyr 2012-03-20 07:36:14