ubuntu 8.04建立mysql C開發環境

來源:互聯網
上載者:User
ubuntu 8.04建立mysql C開發環境 - [編程]Tag:C Linux

著作權聲明:轉載時請以超連結形式標明文章原始出處和作者資訊及本聲明
http://feizf.blogbus.com/logs/30689586.html

安裝mysql

kissgnu@kissgnu-desktop:~$ sudo apt-get install mysql-servel mysql-client
下載安裝過程中,會提示輸入root密碼

重新啟動mysql伺服器:
kissgnu@kissgnu-desktop:~$ sudo /init.d/mysql restart

給普通使用者授權操作資料庫:
kissgnu@kissgnu-desktop:~$ mysql -u root -p      (root登入資料庫)
mysql> create database mydb;                     (建立一個資料庫)
mysql> grant all privileges on mydb.* to kissgnu@localhost identified by 'kissgnu';
mysql> quit;

給特定使用者指派資料操作許可權的格式是:
grant 許可權1,許可權2,...許可權n on 資料庫名稱.表名稱 to 使用者名稱@使用者ip地址 identified by '串連口令';
其中的許可權包括:select,insert,update,delete,create,drop,index,alter,grant,references,reload,shutdown,process,file等14個許可權.

普通使用者操作資料庫:
kissgnu@kissgnu-desktop:~$ mysql -u kissgnu -p   (kissgnu賬戶登入)
mysql> show databases;  (顯示所有的資料庫)
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mydb               |
+--------------------+
2 rows in set (0.00 sec)

接下來的資料庫操作見:
http://feizf.blogbus.com/logs/5072906.html

安裝C語言編程介面:
kissgnu@kissgnu-desktop:~$ sudo apt-get install libmysqlclient15-dev
相關mysql標頭檔和庫檔案安裝在/usr/include/mysql/和/usr/lib/mysql目錄
kissgnu@kissgnu-desktop:~$ gcc xxx.c -I /usr/include/mysql -L /usr/lib/mysql -lmysqlclient  -o xxx

具體的編程介面見sun網站的文檔 http://dev.mysql.com/doc/refman/5.0/en/c.html
和一篇文章 http://hi.baidu.com/kangakang203/blog/item/b1c43ffe6d3188345c600828.html

下面是一個例子,假設資料庫mydb.pet表的內容如下:
+----------+-------+---------+------+------------+-------+
| name     | owner | species | sex  | birth      | death |
+----------+-------+---------+------+------------+-------+
| Puffball | Diane | hamster | f    | 1999-03-30 | NULL  |
| hak      | jack  | euro    | m    | 2001-03-05 | NULL  |
+----------+-------+---------+------+------------+-------+

程式碼:

//demo.c
#include <stdio.h>
#include <mysql.h>

int main(int argc, char **argv)
{
    MYSQL mysql_conn; /* Connection handle */
    MYSQL_RES *mysql_result; /* Result handle */
    MYSQL_ROW mysql_row; /* Row data */
    int f1, f2, num_row, num_col;

    if (mysql_init(&mysql_conn) != NULL)
    {
        if (mysql_real_connect(&mysql_conn, "localhost", "kissgnu",
                    "kissgnu", "mydb", MYSQL_PORT, NULL, 0) != NULL)
        {
            if (mysql_query(&mysql_conn, "select * from pet") == 0)
               {
                mysql_result = mysql_store_result(&mysql_conn);
                num_row = mysql_num_rows(mysql_result);
                /* Get the no. of row */
                num_col = mysql_num_fields(mysql_result);
                /* Get the no. of column */
               
                printf("row=%d, col=%d\n",num_row,num_col);
                 for (f1 = 0; f1 < num_row; f1++)
                 {
                     mysql_row = mysql_fetch_row(mysql_result);
                     for (f2 = 0; f2 < num_col; f2++)
                     {
                        /* Fetch one by one */
                         printf("[Row %d, Col %d] ==> [%s]\n", f1, f2, mysql_row[f2]);
                       }
                 }
             } else
             {
                 (void) printf("Query fails\n");
             }
         } else
         {
             (void) printf("Connection fails\n");
         }
     } else
     {
         (void) printf("Initialization fails\n");
     }

     mysql_free_result(mysql_result);
     mysql_close(&mysql_conn);

     printf("quit\n");
     return 0;
}

kissgnu@kissgnu-desktop:~$ gcc demo.c  -I /usr/include/mysql -L /usr/lib/mysql  -lmysqlclient  -o demo
kissgnu@kissgnu-desktop:~/mymysql$ ./demo
row=2, col=6
[Row 0, Col 0] ==> [Puffball]
[Row 0, Col 1] ==> [Diane]
[Row 0, Col 2] ==> [hamster]
[Row 0, Col 3] ==> [f]
[Row 0, Col 4] ==> [1999-03-30]
[Row 0, Col 5] ==> [(null)]
[Row 1, Col 0] ==> [hak]
[Row 1, Col 1] ==> [jack]
[Row 1, Col 2] ==> [euro]
[Row 1, Col 3] ==> [m]
[Row 1, Col 4] ==> [2001-03-05]
[Row 1, Col 5] ==> [(null)]
quit

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.