MySQL笔记12:C语言访问MYSQL数据库的完整的代码例子
生活随笔
收集整理的這篇文章主要介紹了
MySQL笔记12:C语言访问MYSQL数据库的完整的代码例子
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
C語言訪問MYSQL數據庫的完整的代碼例子
1、手寫安裝帶mysql sdk 的mysql
2、新建控制臺項目,項目屬性中把
C:\Program Files\MySQL\MySQL Server 5.5\include 加入“包含目錄”
把C:\Program Files\MySQL\MySQL Server 5.5\lib 加入“庫目錄”
3、stdafx.h中加入
#include <winsock.h> //注意順序,要放在mysql.h前
#include <mysql.h>//控制臺項目中要在mysql.h之前include <winsock.h>
//注意lib添加到“庫目錄”中,而不是“引用目錄”中
#pragma comment(lib, "libmysql.lib")
4、把libmysql.dll放到生成的exe目錄下
5、主要的幾個例子:
//執行基本查詢 void test1() {MYSQL *pConn;pConn = mysql_init(NULL);//第2、3、4、5參數的意思分別是:服務器地址、用戶名、密碼、數據庫名,第6個為mysql端口號(0為默認值3306)if(!mysql_real_connect(pConn,"localhost","root","root","test",0,NULL,0)){ printf("無法連接數據庫:%s",mysql_error(pConn));return;}mysql_query(pConn,"set names gbk");//防止亂碼。設置和數據庫的編碼一致就不會亂碼//SET NAMES x 相當于 SET character_set_client = x;SET character_set_results = x;SET character_set_connection = x;//寫set character set gbk;查詢不會亂碼,但是參數化插入會報錯。而set names gbk則都不會亂碼//mysql_real_query比mysql_query多了個參數: 字符串query的長度, 所以適合有二進制數據的query, 而mysql_query的字符串query不能包含二進制,因為它以\0為結尾//mysql_query() 不能傳二進制BLOB字段,因為二進制信息中的\0會被誤判為語句結束。 mysql_real_query() 則可以。if(mysql_query(pConn,"select * from persons")){printf("查詢失敗:%s",mysql_error(pConn));return;}//mysql_store_result是把查詢結果一次性取到客戶端的離線數據集,當結果比較大時耗內存。//mysql_use_result則是查詢結果放在服務器上,客戶端通過指針逐行讀取,節省客戶端內存。但是一個MYSQL*連接同時只能有一個未關閉的mysql_use_result查詢MYSQL_RES *result = mysql_store_result(pConn);MYSQL_ROW row;while(row = mysql_fetch_row(result)){printf("%s %s\n",row[1],row[2]);}mysql_free_result(result);mysql_close(pConn); }//獲得更新行數 void test2() {MYSQL *pConn;pConn = mysql_init(NULL);if(!mysql_real_connect(pConn,"127.0.0.1","root","root","test",0,NULL,0)){printf("無法連接數據庫:%s",mysql_error(pConn));return;}if(mysql_query(pConn,"update persons set Age=Age+1")){printf("執行失敗:%s",mysql_error(pConn));return;}printf("更新成功,共更新完成%d條",mysql_affected_rows(pConn));mysql_close(pConn); }//獲得自增id void test3() {MYSQL *pConn;pConn = mysql_init(NULL);if(!mysql_real_connect(pConn,"127.0.0.1","root","root","test",0,NULL,0)){printf("無法連接數據庫:%s",mysql_error(pConn));return;}mysql_query(pConn,"set names gbk");if(mysql_query(pConn,"insert into persons(Name,Age) values('如鵬網',100)")){printf("執行insert失敗%s",mysql_error(pConn));return;}printf("執行insert成功,新id=%d",mysql_insert_id(pConn));mysql_close(pConn);}//參數化查詢 void test4() {MYSQL* pConn;pConn = mysql_init(NULL);if(!mysql_real_connect(pConn,"127.0.0.1","root","root","test",0,NULL,0)){printf("數據庫連接失敗:%s",mysql_error(pConn));return;}mysql_query(pConn,"set names gbk");MYSQL_STMT *stmt; MYSQL_BIND bind[2]; memset(bind,0,sizeof(bind));//把is_null、length等字段默認值設置為NULL等默認值,否則執行會報錯stmt = mysql_stmt_init(pConn); char* insertSQL="insert into persons(Name,Age) values(?,?)";if (mysql_stmt_prepare(stmt, insertSQL, strlen(insertSQL))) { fprintf(stderr, " mysql_stmt_prepare(), INSERT failed,%s\n",mysql_error(pConn)); return; } bind[0].buffer_type= MYSQL_TYPE_STRING; bind[0].buffer= "黑馬"; bind[0].buffer_length= strlen("黑馬"); //如果設定了buffer_length,則可以不試用lengthint age=3;bind[1].buffer_type= MYSQL_TYPE_LONG; bind[1].buffer= &age; bind[1].buffer_length = sizeof(age);if (mysql_stmt_bind_param(stmt, bind)) { fprintf(stderr, " mysql_stmt_bind_param() failed %s\n", mysql_stmt_error(stmt)); return;} if (mysql_stmt_execute(stmt)) { fprintf(stderr, " mysql_stmt_execute(), failed %s\n", mysql_stmt_error(stmt)); return; } mysql_stmt_close(stmt);mysql_close(pConn); printf("參數化執行SQL結束"); } 《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
以上是生活随笔為你收集整理的MySQL笔记12:C语言访问MYSQL数据库的完整的代码例子的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MySQL笔记13:查询结果集
- 下一篇: Linux线程-互斥锁pthread_