這一切似乎都有效,但如果有一個帶NULL的MySQL字段,我會得到一個分段錯誤。C程序:從包含NULL的字段中選擇MySQL時的分段錯誤
下面的代碼:
int
selectDB(char * cmd)
{
printf("Cmd: %s\n", cmd);
MYSQL *conn;
MYSQL_RES *result;
MYSQL_ROW row;
int i;
conn = mysql_init(NULL);
if (conn == NULL) {
printf("Error %u: %s\n", mysql_errno(conn), mysql_error(conn));
exit(1);
}
if (mysql_real_connect(conn, "localhost", "root",
"myPassword", "myDB", 0, NULL, 0) == NULL)
{
printf("Error %u: %s\n", mysql_errno(conn), mysql_error(conn));
exit(1);
}
if (mysql_query(conn, cmd))
{
printf("Error %u: %s\n", mysql_errno(conn), mysql_error(conn));
exit(1);
}
if (!(result = mysql_store_result(conn)))
{
printf("Error %u: %s\n", mysql_errno(conn), mysql_error(conn));
exit(1);
}
while((row = mysql_fetch_row(result))) {
for (i=0 ; i < mysql_num_fields(result); i++)
printf("%s\n", row[i]);
}
if (!mysql_eof(result))
{
printf("Error %u: %s\n", mysql_errno(conn), mysql_error(conn));
exit(1);
}
mysql_free_result(result);
mysql_close(conn);
return 0;
}
我與
char cmd[1000];
snprintf(cmd, 999, "SELECT * FROM users WHERE id = %s", valueptr);
selectDB(cmd);
哪條線(S),你得到一個賽格故障? – Chad 2011-05-03 18:46:11
我不會說一個笨蛋,必然...... - 看來OP不知道mysql可以在行中放置NULL指針。 – Claudiu 2011-05-03 18:46:13
@ Claudio-你是對的。這可能會幫助他,但不是一個人。抱歉。 – MByD 2011-05-03 18:47:55