时间:2021-07-01 10:21:17 帮助过:28人阅读
#include "/usr/include/mysql/mysql.h"#include#include #include int main(void){ printf("Hello MYSQL./n"); char sql[4000]; sprintf(sql, "INSERT INTO `km_login_info`(`KmNumber`,`UserPwd`,`IfOnline`,`LoginIP`,`LoginTime`,`FriendsCount`,`IfMsg`) VALUES('95215710','123','0','192.168.8.123','2012-12-25',100,'1')"); const char *g_host_name = "192.168.8.5"; const char *g_user_name = "zangzy"; const char *g_password = "123"; const char *g_db_name = "im"; const unsigned int g_db_port = 3306; MYSQL *g_conn; //mysql连接 MYSQL_RES *g_res; //mysql记录集 MYSQL_ROW g_row; //mysql记录行 g_conn = mysql_init(NULL); if(!mysql_real_connect(g_conn, g_host_name, g_user_name, g_password, g_db_name, g_db_port, NULL, 0)) { puts(mysql_error(g_conn)); exit(-1); } //if (mysql_real_query(g_conn, sql, strlen(sql))) // puts(mysql_error(g_conn)); memset(sql, 0x00, sizeof(sql)); strcpy(sql, "select * from `km_login_info`"); if (mysql_real_query(g_conn, sql, strlen(sql))) puts(mysql_error(g_conn)); g_res = mysql_store_result(g_conn); //从服务器传送结果集至本地,mysql_use_result直接使用服务器上的记录 //int iNum_rows = mysql_num_rows(g_res); //得到记录的行数 //int iNum_fields = mysql_num_fields(g_res); //得到记录的列数 while ((g_row=mysql_fetch_row(g_res))) //打印结果集 printf("%s/t%s/n", g_row[0], g_row[1]); //第一,第二字段 mysql_free_result(g_res); // 释放结果集 mysql_close(g_conn); //关闭链接 exit(0);}