#include <stdlib.h>
#include <stdio.h>
#include "mysql.h"
MYSQL my_connection;
MYSQL_RES *res_ptr;
MYSQL_ROW sqlrow;
void display_row();
int main(int argc, char *argv[])
{
int res;
mysql_init(&my_connection);
if (mysql_real_connect(&my_connection, "localhost", "rick",
"secret", "foo", 0, NULL, 0)) {
/* 设置数据库默认字符集 */
if (mysql_set_character_set(&my_connection, "utf8")) {
fprintf(stderr, "错误, %s\n", mysql_error(&my_connection));
}
res = mysql_query(&my_connection,
"SELECT childno, fname, age FROM children WHERE age > 5");
if (res) {
fprintf(stderr, "SELECT error: %s\n",
mysql_error(&my_connection));
} else {
res_ptr = mysql_use_result(&my_connection);
if (res_ptr) {
while ((sqlrow = mysql_fetch_row(res_ptr))) {
display_row();
}
if (mysql_errno(&my_connection)) {
fprintf(stderr, "Retrive error: %s\n",
mysql_error(&my_connection));
}
mysql_free_result(res_ptr);
}
}
mysql_close(&my_connection);
} else {
fprintf(stderr, "Connection failed\n");
if (mysql_errno(&my_connection)) {
fprintf(stderr, "Connection error %d: %s\n",
mysql_errno(&my_connection),
mysql_error(&my_connection));
}
}
return EXIT_SUCCESS;
}
void display_row()
{
unsigned int field_count;
field_count = 0;
while (field_count < mysql_field_count(&my_connection)) {
if (sqlrow[field_count])
printf("%s ", sqlrow[field_count]);
else
printf("NULL");
field_count++;
}
printf("\n");
}
|