📄 linux下mysql调用.c
字号:
#include <mysql.h>#include <stdio.h>#include <iostream>using namespace std;int main(int agc,char* argv[]){ MYSQL *conn; char strInfo[250]; conn = mysql_init(NULL); if (conn == NULL) { sprintf(strInfo, "mysql_init() Failed due to : %s\n", mysql_error(conn)); cout<<strInfo<<endl; return -1; } if(mysql_real_connect(conn,"localhost", "root", "", "gkauth", 3306,NULL,0) == NULL) { sprintf(strInfo, "Failed to connect to database: Error: %s\n", mysql_error(conn)); cout<<strInfo<<endl; return -1; } MYSQL_RES *res_set = NULL; MYSQL_ROW row; char strSql[200]; memset(strSql,0,sizeof(char)*200); sprintf(strSql,"SELECT * FROM cdr");//sql 语句 if( mysql_query(conn, strSql) != 0 ) { //根据sql语句执行 sprintf(strInfo, "mysql_query() Failed due to : %s\n", mysql_error(conn)); cout<<strInfo<<endl; return -1; } if( (res_set = mysql_store_result(conn)) == NULL ) {//得到返回记录 sprintf(strInfo, "mysql_store_result() Failed due to : %s\n", mysql_error(conn)); cout<<strInfo<<endl; return -1; } unsigned long uNum=mysql_num_rows(res_set); mysql_free_result(res_set); cout<<"Record number="<<uNum<<endl; //mysql_num_rows() /*if( (row = mysql_fetch_row(res_set)) == NULL ) {//得到记录的一行 servid = 0; } else sscanf(row[0], "%d", &servid); */ return 1;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -