標籤:connect cte gcc mes cti 數組 字元 進入 oca
首先確定系統上安裝了GCC和MYSQL了沒有,
如果沒有先安裝.CentOS用
yum -y install gcc
yum -y install mysql-server
此外還必須安裝mysql-devel
安裝成功檢測:
[[email protected] mysql]# rpm -qa | grep ‘gcc‘ libgcc-4.4.7-4.el6.x86_64gcc-4.4.7-4.el6.x86_64[[email protected] mysql]# rpm -qa | grep ‘mysql‘mysql-5.1.73-3.el6_5.x86_64mysql-devel-5.1.73-3.el6_5.x86_64mysql-libs-5.1.73-3.el6_5.x86_64mysql-server-5.1.73-3.el6_5.x86_64
然後啟動mysql
service mysqld start
進入Mysql建立資料庫和表
mysql> create database c_test;mysql> use c_testCREATE TABLE `user` (`id` int(11) NOT NULL AUTO_INCREMENT ,`name` varchar(30) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT ‘‘ ,PRIMARY KEY (`id`))ENGINE=InnoDBDEFAULT CHARACTER SET=utf8 COLLATE=utf8_general_ci;mysql> insert into user(name) values(‘張三‘),(‘李四‘),(‘王五‘);mysql> select * from user;+----+--------+| id | name |+----+--------+| 1 | 張三 || 2 | 李四 || 3 | 王五 |+----+--------+3 rows in set (0.00 sec)
建立mysql.c
/* ============================================================================ Name : connect.c Author : 林澤歸來 Version : Copyright : Your copyright notice Description : Connect Mysql ============================================================================ */#include <stdio.h>#include <stdlib.h>#include <string.h>#include <mysql/mysql.h>MYSQL *g_conn; // mysql 串連MYSQL_RES *g_res; // mysql 記錄集MYSQL_ROW g_row; // 字串數組,mysql 記錄行#define MAX_BUF_SIZE 1024 // 緩衝區最大位元組數const char *g_host_name = "localhost";const char *g_user_name = "root";const char *g_password = "123456";const char *g_db_name = "c_test";const unsigned int g_db_port = 3306;void print_mysql_error(const char *msg) { // 列印最後一次錯誤 if (msg) printf("%s: %s\n", msg, mysql_error(g_conn)); else puts(mysql_error(g_conn));}int executesql(const char * sql) { /*query the database according the sql*/ if (mysql_real_query(g_conn, sql, strlen(sql))) // 如果失敗 return -1; // 表示失敗 return 0; // 成功執行}int init_mysql() { // 初始化串連 // init the database connection g_conn = mysql_init(NULL); /*設定字元編碼,可能會亂碼*/
mysql_query(g_conn,"set nemas utf-8"); /* connect the database */ if(!mysql_real_connect(g_conn, g_host_name, g_user_name, g_password, g_db_name, g_db_port, NULL, 0)) // 如果失敗 return -1; // 是否串連已經可用 //if (executesql("set names utf8")) // 如果失敗 // return -1; return 0; // 返回成功}int main(void) { if (init_mysql()); print_mysql_error(NULL); char sql[MAX_BUF_SIZE]; if (executesql(sql)) print_mysql_error(NULL); if (executesql("select * from user")) // 句末沒有分號 print_mysql_error(NULL); 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); // 得到記錄的列數 printf("共%d個記錄,每個記錄%d欄位\n", iNum_rows, iNum_fields); puts("id\tname\n"); 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); // 關閉連結 return EXIT_SUCCESS;}
編譯
gcc -g -o mysql -I/usr/include/mysql/ connect.c -L/usr/lib64/mysql/ -lmysqlclient -lz
編譯的時候要注意用到2個路徑,mysql.h和libmysqlclient.so的路徑
尋找mysql.h路徑
[[email protected] mysql]# find / -name ‘mysql.h‘ /usr/include/mysql/mysql.h
[[email protected] mysql]# find / -name ‘*mysqlclient*‘ /usr/lib64/mysql/libmysqlclient_r.so.16.0.0/usr/lib64/mysql/libmysqlclient.so.16/usr/lib64/mysql/libmysqlclient_r.so.16/usr/lib64/mysql/libmysqlclient_r.so/usr/lib64/mysql/libmysqlclient.so/usr/lib64/mysql/libmysqlclient.so.16.0.0
運行:
[[email protected] mysql]# ./mysql
運行結果:
共3個記錄,每個記錄2欄位id name1 張三2 李四3 王五
一定要保持資料表編碼,C檔案編碼的一致性,否則會出現亂碼
Linux C串連Mysql