[ C ] – How to C lang Connette Mysql C語言 如何連線 Mysql

評等結果
點擊便能為這篇文章進行評等!
[評等總次數: 0,平均評等: 0]

小編因工作需要…<=到底是需要學多少程式?!,沒錯資訊這行就是這樣;無止儘的學習,至少我的肝還沒爆掉,這次要學的是如何用c 語言來連線(操作) Mysql。

1.安裝
指令:apt install mysql-devel
預設的安裝路徑為 /usr/include/mysql
(請先確認你已經幫你的主機安裝了 Mysql Server ,你可以使用 netstat -ntpl 來看目前有沒有跑 mysql :3306)

2.來段連結的範例程式吧!

#include </usr/include/mysql/mysql.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
    MYSQL *conn;
    MYSQL_RES *res;
    MYSQL_ROW row;
    char server[] = "localhost";
    char user[] = "root";
    char password[] = "12345";
    char database[] = "mysql";
    conn = mysql_init(NULL);
    if (!mysql_real_connect(conn, server,user, password, database, 0, NULL, 0))
    {
        fprintf(stderr, "%s\n", mysql_error(conn));
        exit(1);
    }
    if (mysql_query(conn, "show tables"))
    {
        fprintf(stderr, "%s\n", mysql_error(conn));
        exit(1);
    }
    res = mysql_use_result(conn);
    printf("MySQL Tables in mysql database:\n");
    while ((row = mysql_fetch_row(res)) != NULL)
    {
        printf("%s \n", row[0]);
    }
    mysql_free_result(res);
    mysql_close(conn);
    printf("finish! \n");
    return 0;
}
3.編譯程式
指令:gcc -o show_table show_table.c -Wall `mysql_config --cflags --libs`

4.執行程式
指令:./show_table

顯示結果:

MySQL Tables in mysql database:
columns_priv
db
engine_cost
event
func
general_log
gtid_executed
help_category
help_keyword
help_relation
help_topic
innodb_index_stats
innodb_table_stats
ndb_binlog_index
plugin
proc
procs_priv
proxies_priv
server_cost
servers
slave_master_info
slave_relay_log_info
slave_worker_info
slow_log
tables_priv
time_zone
time_zone_leap_second
time_zone_name
time_zone_transition
time_zone_transition_type
user
finish!

參考連結:
https://www.cnblogs.com/liudw-0215/p/9593414.html
https://www.ubuntu-tw.org/modules/newbb/viewtopic.php?post_id=277530

評等結果
點擊便能為這篇文章進行評等!
[評等總次數: 0,平均評等: 0]
!去下營經續永站本助幫能就下一點輕輕,話的您到助幫有章文的我果如 如果我的文章有幫助到您的話,輕輕點一下就能幫助本站永續經營下去!

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *