首页mysql › mysql 编程笔记

mysql 编程笔记

要进行linux下的mysql的C编程,需要安装mysql及mysql的开发包,ubuntu下直接apt-get install libmysql++安装开发包。
#include <mysql.h>
相关函数:

例子:

创建测试数据库
mysql> create database test;
创建用户
mysql> grant all on test.* to test@’localhost’ identified by ‘test’;

sql文件:

导入sql文件:

mysql -u test –password=test test<mysqlchildren.sql

导入后的情况:

mysql> show tables;
+—————-+
| Tables_in_test |
+—————-+
| children |
+—————-+
1 row in set (0.00 sec)

mysql> select * from children;
+———+——–+——+
| childno | fname | age |
+———+——–+——+
| 1 | Jenny | 21 |
| 2 | Andrew | 17 |
| 3 | Gavin | 8 |
| 4 | Duncan | 6 |
| 5 | Emma | 4 |
| 6 | Alex | 15 |
| 7 | Adrian | 9 |
+———+——–+——+
7 rows in set (0.00 sec)

C代码:

gcc -I/usr/include/mysql -L/usr/lib/mysql -lmysqlclient -o mysqltest mysqltest.c

./test

结果如下:

Connection success:
Column details:
Name: childno
Type: Numeric filed
MAX width 11
Auto increments

Name: fname
Type: VARCHAR
MAX width 30

Name: age
Type: Numeric filed
MAX width 11

1 Jenny 21
2 Andrew 17
3 Gavin 8
4 Duncan 6
6 Alex 15
7 Adrian 9

发表评论