版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明
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