Linux C語言連線MySQL 增刪改查操作

2021-07-25 12:02:10 字數 1748 閱讀 1481

linux下想要測試mysql和memcached的效能,因為是伺服器只能通過終端連線,所以考慮用c語言寫測試**。於是研究了把c怎麼連線mysql以及增刪改查的**。安裝mysql-client或者編譯原始碼安裝mysql後,會有支援c語言寫客戶端的標頭檔案和庫檔案,但是目錄可能不一樣,mysql原始碼安裝見 

從網上找了類似的**改改後如下

連線資料庫

#include #include #include int main() 

conn_ptr = mysql_real_connect(conn_ptr, "localhost", "root", "123456", "test", 0, null, 0);

//root 為使用者名稱 123456為密碼 test為要連線的database

if (conn_ptr) else

mysql_close(conn_ptr);

return exit_success;

}

通過gcc編譯(我的mysql原始碼安裝目錄為 /usr/local/mysql)

gcc -o connect -g connect.c  -i /usr/local/mysql/include/ -l /usr/local/mysql/lib/ -l mysqlclient

引數說明:-i(大寫的i) 表示要連線的頭檔案目錄,因為使用了,-l表示要連線的庫檔案目錄 -l(小寫的l) 表示要連線的具體的庫名稱,在/usr/local/mysql/lib/ 下,有叫做libmysqlclient.so的庫,指定具體的庫的名字時,預設去掉頭尾的lib和.so直接使用中間的mysqlclient

如果不用-i -l ,可能導致找不到標頭檔案 或者 函式未定義的錯誤

如果出現錯誤error while loading shared libraries,那是因為.so沒有被系統載入,解決辦法如下

vi /etc/ld.so.conf // 開啟ld.so.conf檔案,增加.so檔案的路徑,比如/usr/local/mysql/lib/

查詢**  

#include #include #include int main() 

conn_ptr = mysql_real_connect(conn_ptr, "localhost", "root", "123456", "test", 0, null, 0);

if (conn_ptr) else

if (mysql_errno(conn_ptr))

}

mysql_free_result(res_ptr);

} } else

mysql_close(conn_ptr);

return exit_success;

}

插入更新及刪除 

#include #include #include int main() 

conn_ptr = mysql_real_connect(conn_ptr, "localhost", "root", "123456", "test", 0, null, 0);

if (conn_ptr) else

} else

mysql_close(conn_ptr);

return exit_success;

}

原文:

Linux C語言連線MySQL 增刪改查操作

linux下想要測試mysql和memcached的效能,因為是伺服器只能通過終端連線,所以考慮用c語言寫測試 於是研究了把c怎麼連線mysql以及增刪改查的 安裝mysql client或者編譯原始碼安裝mysql後,會有支援c語言寫客戶端的標頭檔案和庫檔案,但是目錄可能不一樣,mysql原始碼安...

Linux C語言連線 sqlserver資料庫

記錄一下linux下使用c語言連線sqlserver的方法。連線前需要安裝freetds。參考 這個是從參考鏈結中的提取 分析一下,寫上注釋。include include include include include includeint main void else 連線資料庫 if dbus...

Linux C語言連線 sqlserver資料庫

記錄一下linux下使用c語言連線sqlserver的方法。連線前需要安裝freetds。參考 這個是從參考鏈結中的提取 分析一下,寫上注釋。include include include include include includeint main void else 連線資料庫 if dbus...