mysql基礎 mysql與C結合例項

2021-06-22 15:48:49 字數 3493 閱讀 7653

乙個簡單的mysql與c的互動,使用了一些mysql的c api!

老鳥掠過,新手可以看看!

[cpp]view plain

copy

/******************************************

本檔案學習mysql的database使用

學習mysql的c介面

包括初始化db,檢查資料庫是否存在,不存在就建立。

檢查表是否存在,不存在則建立

*****************************************/

#include 

#include 

#include 

//需要包含mysql的標頭檔案,/usr/include/mysql/

//編譯的時候需要加上-l/usr/lib/ -lmysqlclient -i/usr/include/mysql選項

#include //基本的標頭檔案,一些重要結構體的宣告

#include //錯誤資訊的存放

#include //包含當前mysql的版本資訊

//全部變數,巨集定義//

#define debug

#define server_host "localhost"  //mysql的遠端位址

#define server_user "root"      //資料庫登入名

#define server_pwd  "123"  //資料庫登入密碼

#define db_name     "tmp_db"    //新建資料庫的名字

#define table_name  "mytables"  //庫中的表

int check_tbl(mysql* mysql,char *name);  

int check_db(mysql *mysql,char *db_name);  

int init_db()  

if(!mysql_real_connect(&mysql,server_host,server_user,server_pwd,null,0,null,0))  

printf("connected.....\n");  

err = check_db(&mysql,db_name);  

if(err != 0)  

//select which db

if(mysql_select_db(&mysql,db_name)) //return 0 is success ,!0 is err

//chuangjianbiao

if((err=check_tbl(&mysql,table_name))!=0)  

mysql_close(&mysql);  

return 0;  

}  int check_db(mysql *mysql,char *db_name)  

}  //mysql_list_dbs會分配記憶體,需要使用mysql_free_result釋放

mysql_free_result(res);  

}  if(!row)  //沒有這個資料庫,則建立

;  strcpy(buf,"create database ");  

strcat(buf,db_name);  

#ifdef debug

printf("%s\n",buf);  

#endif

if(mysql_query(mysql,buf))  

}  return 0;  

}  int check_tbl(mysql* mysql,char *name)  

}  mysql_free_result(res);  

}  if(!row) //create table

;  char qbuf[128]=;  

snprintf(buf,sizeof(buf),"%s (name varchar(20),*** char(1),score int(3));",table_name);  

strcpy(qbuf,"create table ");  

strcat(qbuf,buf);  

//#ifdef debug

printf("%s\n",qbuf);  

//#endif

if(mysql_query(mysql,qbuf))  

}  return 0;  

}  int main()    

編譯的方法:最後的-i一定要注意檢查這個目錄是不是存在,否則會報錯

[sql]view plain

copy

gcc -o mysql mysql.c -l/usr/lib/ -lmysqlclient -i/usr/include/mysql  

報錯:[python]view plain

copy

root@jack-desktop:~# gcc -o mysql mysql.c -l/usr/lib/ -lmysqlclient

gcc: mysql.c: 沒有那個檔案或目錄  

root@jack-desktop:~# 

root@jack-desktop:~# 

root@jack-desktop:~# cd myc/

root@jack-desktop:myc# gcc -o mysql mysql.c -l/usr/lib/ -lmysqlclient

mysql.c:15:73: error: mysql.h: 沒有那個檔案或目錄  

mysql.c:16:43: error: errmsg.h: 沒有那個檔案或目錄  

mysql.c:17:63: error: mysql_version.h: 沒有那個檔案或目錄  

mysql.c:27: error: expected 『)』 before 『*』 token  

mysql.c:28: error: expected 『)』 before 『*』 token  

mysql.c: in function 『init_db』:  

mysql.c:34: error: 『mysql』 undeclared (first use in this function)  

mysql.c:34: error: (each undeclared identifier is reported only once  

mysql.c:34: error: expected 『;』 before 『mysql』  

mysql.c:36: error: 『mysql』 undeclared (first use in this function)  

mysql.c: at top level:  

mysql.c:73: error: expected 『)』 before 『*』 token  

mysql.c:109: error: expected 『)』 before 『*』 token 

mysql基礎 mysql與C結合例項

乙個簡單的mysql與c的互動,使用了一些mysql的c api!老鳥掠過,新手能夠看看!本檔案學習mysql的database使用 學習mysql的c介面 包括初始化db,檢查資料庫是否存在,不存在就建立。檢查表是否存在,不存在則建立 include include include 須要包括mys...

mysql 釋放結果結 mysql free

其它 實驗三 函式的呼叫和返回 單選題 ysql free result result 函式中的引數,可以是 資料。單選題 lte的物理小區標識 pci 有多少個 單選題 php的各種擴充套件中,操作mysql資料庫的pdo擴充套件的檔名是 其它 實驗一 php 環境搭建 單選題 採購作業的作業動因...

mysql 與操作 MySql基礎概念與操作

include include include mysql.h include using std string using std vector pragma comment lib,libmysql void dealsql const char pmysqlc,mysql pmysql if ...