c语言简单操作mysql
代码
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <winsock.h>
#include <mysql.h>
int main() {
printf("Content-type:text/html\n\n");
for (int i = 0; i < 1; i++) {
printf("hello c (%d) <br/>\n", i);
}
MYSQL *mysql = mysql_init(NULL);
if (mysql_real_connect(mysql, "localhost", "root", "root", "xj2014", 3306, NULL, 0)) {
printf("数据库连接成功\n");
if (mysql_set_character_set(mysql, "gbk")) {
printf("编码设置失败:%s\n",mysql_error(mysql));
exit(1);
}
if (mysql_query(mysql, "select num,name,classs from userinfo limit 10")) {
printf("查询出错:%s\n", mysql_error(mysql));
exit(1);
};
MYSQL_RES* res = mysql_store_result(mysql);
MYSQL_ROW row;
while (row = mysql_fetch_row(res)) {
printf("\n学号=%s\t姓名=%s\t班级=%s", row[0], row[1],row[2]);
}
mysql_free_result(res);
mysql_close(mysql);
} else {
printf("数据库连接失败\n");
exit(1);
}
Sleep(500);
return 0;
}
Comments