Linux下通过C语言操作MySQL数据库
2011-12-17 15:07
513 查看
实验环境:
Fedora 11
组件:
libdbi-dbd-mysql-0.8.3-4.fc11.i586
mysql-5.1.42-2.fc11.i586
mysql-libs-5.1.42-2.fc11.i586
mysql-devel-5.1.42-2.fc11.i586
mysql-connector-java-5.1.8-2.fc11.i586
qt-mysql-4.5.3-9.fc11.i586
php-mysql-5.2.11-2.fc11.i586
mysql-connector-odbc-5.1.5r1144-4.fc11.i586
mysql-server-5.1.42-2.fc11.i586
第一步:
启动一个mysql客户端
[mtd@mutiandong mysql]$ mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 20
Server version: 5.1.42 Source distribution
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
创建一个数据库
mysql> create database cusemysql;
Query OK, 1 row affected (0.00 sec)
并使用这个数据库
mysql> use cusemysql;
Database changed
在给数据库内创建一个表
mysql> create table children(childno int not null unique,fname varchar(20),age int);
Query OK, 0 rows affected (0.00 sec)
在该表内插入一项数据
mysql> insert into children values(5,‘jhon’,10);
Query OK, 1 row affected (0.00 sec)
mysql> select * from children;
+---------+-------+------+
| childno | fname | age |
+---------+-------+------+
| 1 | jhon | 10 |
+---------+-------+------+
1 row in set (0.00 sec)
mysql>
第二部:
编写 insert.c
///////////////////////////////////
/* insert.c */
#include
#include
#include
/*注意哦,上面写的是mysql.h的绝对地址,一般在/usr/include/mysql下,仔细看看你的在哪里?这种方式 #include "mysql/h" 可能也可以*/
int main(int argc, char *argv[])
{
MYSQL my_connection;
int res;
mysql_init(&my_connection);
/*mysql_real_connect(&mysql,主机名,用户名,密码,数据库名,0,NULL,0) == NULL)*/
/*mysql_real_connect(&mysql,host,user,passwd,dbname,0,NULL,0) == NULL)*/
if (mysql_real_connect(&my_connection, "localhost", "root", "","cusemysql",0,NULL,CLIENT_FOUND_ROWS))
{
printf("Connection success\n");
res = mysql_query(&my_connection, "insert into children values(11,'Anny',5)");
if (!res)
{
printf("Inserted %lu rows\n",(unsigned long)mysql_affected_rows(&my_connection));
/*里头的函数返回受表中影响的行数*/
}
else
{
//分别打印出错误代码及详细信息
fprintf(stderr, "Insert error %d: %s\n",mysql_errno(&my_connection),mysql_error(&my_connection));
}
mysql_close(&my_connection);
}
else
{
fprintf(stderr, "Connection failed\n");
if (mysql_errno(&my_connection))
{
fprintf(stderr, "Connection error %d: %s\n",mysql_errno(&my_connection),mysql_error(&my_connection));
}
}
return EXIT_SUCCESS;
}
/////////////////////////////////////////////
代码写完了,要编译哦
[mtd@mutiandong mysql]# gcc -o insert insert.c
/tmp/ccyHfsX2.o(.text+0x1e): In function `main':
: undefined reference to `mysql_init'
/tmp/ccyHfsX2.o(.text+0x47): In function `main':
: undefined reference to `mysql_real_connect'
/tmp/ccyHfsX2.o(.text+0x76): In function `main':
: undefined reference to `mysql_query'
/tmp/ccyHfsX2.o(.text+0x9a): In function `main':
: undefined reference to `mysql_affected_rows'
/tmp/ccyHfsX2.o(.text+0xbc): In function `main':
: undefined reference to `mysql_error'
/tmp/ccyHfsX2.o(.text+0xcf): In function `main':
: undefined reference to `mysql_errno'
/tmp/ccyHfsX2.o(.text+0xf5): In function `main':
: undefined reference to `mysql_close'
/tmp/ccyHfsX2.o(.text+0x11f): In function `main':
: undefined reference to `mysql_errno'
/tmp/ccyHfsX2.o(.text+0x135): In function `main':
: undefined reference to `mysql_error'
/tmp/ccyHfsX2.o(.text+0x148): In function `main':
: undefined reference to `mysql_errno'
collect2: ld returned 1 exit status
[mtd@mutiandong mysql]#
头文件和库文件位置没有指定
[mtd@mutiandong mysql]# gcc -o insert insert.c -I/usr/include/mysql/ -L/usr/lib/mysql/
/tmp/cc4gdmlp.o(.text+0x1e): In function `main':
: undefined reference to `mysql_init'
/tmp/cc4gdmlp.o(.text+0x47): In function `main':
: undefined reference to `mysql_real_connect'
/tmp/cc4gdmlp.o(.text+0x76): In function `main':
: undefined reference to `mysql_query'
/tmp/cc4gdmlp.o(.text+0x9a): In function `main':
: undefined reference to `mysql_affected_rows'
/tmp/cc4gdmlp.o(.text+0xbc): In function `main':
: undefined reference to `mysql_error'
/tmp/cc4gdmlp.o(.text+0xcf): In function `main':
: undefined reference to `mysql_errno'
/tmp/cc4gdmlp.o(.text+0xf5): In function `main':
: undefined reference to `mysql_close'
/tmp/cc4gdmlp.o(.text+0x11f): In function `main':
: undefined reference to `mysql_errno'
/tmp/cc4gdmlp.o(.text+0x135): In function `main':
: undefined reference to `mysql_error'
/tmp/cc4gdmlp.o(.text+0x148): In function `main':
: undefined reference to `mysql_errno'
collect2: ld returned 1 exit status
[mtd@mutiandong mysql]#
gcc还是找不到头文件,下面我们可以这样,指定gcc专门找 mysqlclient 之类的库
[root@localhost testmysql]# gcc -o insert insert.c -lmysqlclient -I/usr/include/mysql/ -L/usrlib/mysql
我用 -lmysqlclient 选项就可以了。
ok,现在我们执行看看
[mtd@mutiandong mysql]# ./insert
Connection Success
Inserted 1 rows
呵呵,真的成功了!
不信到mysql下看看表children中是否多了刚才插入的那一行数据
mysql> select * from children;
+---------+-------+------+
| childno | fname | age |
+---------+-------+------+
| 1 | jhon | 10 |
| 11 | Anny | 5 |
+---------+-------+------+
2 rows in set (0.00 sec)
Fedora 11
组件:
libdbi-dbd-mysql-0.8.3-4.fc11.i586
mysql-5.1.42-2.fc11.i586
mysql-libs-5.1.42-2.fc11.i586
mysql-devel-5.1.42-2.fc11.i586
mysql-connector-java-5.1.8-2.fc11.i586
qt-mysql-4.5.3-9.fc11.i586
php-mysql-5.2.11-2.fc11.i586
mysql-connector-odbc-5.1.5r1144-4.fc11.i586
mysql-server-5.1.42-2.fc11.i586
第一步:
启动一个mysql客户端
[mtd@mutiandong mysql]$ mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 20
Server version: 5.1.42 Source distribution
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
创建一个数据库
mysql> create database cusemysql;
Query OK, 1 row affected (0.00 sec)
并使用这个数据库
mysql> use cusemysql;
Database changed
在给数据库内创建一个表
mysql> create table children(childno int not null unique,fname varchar(20),age int);
Query OK, 0 rows affected (0.00 sec)
在该表内插入一项数据
mysql> insert into children values(5,‘jhon’,10);
Query OK, 1 row affected (0.00 sec)
mysql> select * from children;
+---------+-------+------+
| childno | fname | age |
+---------+-------+------+
| 1 | jhon | 10 |
+---------+-------+------+
1 row in set (0.00 sec)
mysql>
第二部:
编写 insert.c
///////////////////////////////////
/* insert.c */
#include
#include
#include
/*注意哦,上面写的是mysql.h的绝对地址,一般在/usr/include/mysql下,仔细看看你的在哪里?这种方式 #include "mysql/h" 可能也可以*/
int main(int argc, char *argv[])
{
MYSQL my_connection;
int res;
mysql_init(&my_connection);
/*mysql_real_connect(&mysql,主机名,用户名,密码,数据库名,0,NULL,0) == NULL)*/
/*mysql_real_connect(&mysql,host,user,passwd,dbname,0,NULL,0) == NULL)*/
if (mysql_real_connect(&my_connection, "localhost", "root", "","cusemysql",0,NULL,CLIENT_FOUND_ROWS))
{
printf("Connection success\n");
res = mysql_query(&my_connection, "insert into children values(11,'Anny',5)");
if (!res)
{
printf("Inserted %lu rows\n",(unsigned long)mysql_affected_rows(&my_connection));
/*里头的函数返回受表中影响的行数*/
}
else
{
//分别打印出错误代码及详细信息
fprintf(stderr, "Insert error %d: %s\n",mysql_errno(&my_connection),mysql_error(&my_connection));
}
mysql_close(&my_connection);
}
else
{
fprintf(stderr, "Connection failed\n");
if (mysql_errno(&my_connection))
{
fprintf(stderr, "Connection error %d: %s\n",mysql_errno(&my_connection),mysql_error(&my_connection));
}
}
return EXIT_SUCCESS;
}
/////////////////////////////////////////////
代码写完了,要编译哦
[mtd@mutiandong mysql]# gcc -o insert insert.c
/tmp/ccyHfsX2.o(.text+0x1e): In function `main':
: undefined reference to `mysql_init'
/tmp/ccyHfsX2.o(.text+0x47): In function `main':
: undefined reference to `mysql_real_connect'
/tmp/ccyHfsX2.o(.text+0x76): In function `main':
: undefined reference to `mysql_query'
/tmp/ccyHfsX2.o(.text+0x9a): In function `main':
: undefined reference to `mysql_affected_rows'
/tmp/ccyHfsX2.o(.text+0xbc): In function `main':
: undefined reference to `mysql_error'
/tmp/ccyHfsX2.o(.text+0xcf): In function `main':
: undefined reference to `mysql_errno'
/tmp/ccyHfsX2.o(.text+0xf5): In function `main':
: undefined reference to `mysql_close'
/tmp/ccyHfsX2.o(.text+0x11f): In function `main':
: undefined reference to `mysql_errno'
/tmp/ccyHfsX2.o(.text+0x135): In function `main':
: undefined reference to `mysql_error'
/tmp/ccyHfsX2.o(.text+0x148): In function `main':
: undefined reference to `mysql_errno'
collect2: ld returned 1 exit status
[mtd@mutiandong mysql]#
头文件和库文件位置没有指定
[mtd@mutiandong mysql]# gcc -o insert insert.c -I/usr/include/mysql/ -L/usr/lib/mysql/
/tmp/cc4gdmlp.o(.text+0x1e): In function `main':
: undefined reference to `mysql_init'
/tmp/cc4gdmlp.o(.text+0x47): In function `main':
: undefined reference to `mysql_real_connect'
/tmp/cc4gdmlp.o(.text+0x76): In function `main':
: undefined reference to `mysql_query'
/tmp/cc4gdmlp.o(.text+0x9a): In function `main':
: undefined reference to `mysql_affected_rows'
/tmp/cc4gdmlp.o(.text+0xbc): In function `main':
: undefined reference to `mysql_error'
/tmp/cc4gdmlp.o(.text+0xcf): In function `main':
: undefined reference to `mysql_errno'
/tmp/cc4gdmlp.o(.text+0xf5): In function `main':
: undefined reference to `mysql_close'
/tmp/cc4gdmlp.o(.text+0x11f): In function `main':
: undefined reference to `mysql_errno'
/tmp/cc4gdmlp.o(.text+0x135): In function `main':
: undefined reference to `mysql_error'
/tmp/cc4gdmlp.o(.text+0x148): In function `main':
: undefined reference to `mysql_errno'
collect2: ld returned 1 exit status
[mtd@mutiandong mysql]#
gcc还是找不到头文件,下面我们可以这样,指定gcc专门找 mysqlclient 之类的库
[root@localhost testmysql]# gcc -o insert insert.c -lmysqlclient -I/usr/include/mysql/ -L/usrlib/mysql
我用 -lmysqlclient 选项就可以了。
ok,现在我们执行看看
[mtd@mutiandong mysql]# ./insert
Connection Success
Inserted 1 rows
呵呵,真的成功了!
不信到mysql下看看表children中是否多了刚才插入的那一行数据
mysql> select * from children;
+---------+-------+------+
| childno | fname | age |
+---------+-------+------+
| 1 | jhon | 10 |
| 11 | Anny | 5 |
+---------+-------+------+
2 rows in set (0.00 sec)
相关文章推荐
- 在Linux下编译安装MySQL并通过C语言操作MySQL数据库
- 在Linux下通过C语言操作MySQL数据库,及部分数据库源代码
- 转]在Linux下编译安装MySQL并通过C语言操作MySQL数据库
- Linux下通过C++码来操作MySQL数据库
- linux通过远程访问MySQL数据库,并执行相关操作
- Linux下的C语言编程——进程间通过signal函数通信简单操作
- Linux下通过C++码来操作MySQL数据库
- (一个代码学会c语言操作数据库)linux上通过c语言操作数据库实现基本的学生信息管理系统(增、删、查、改‘显示)
- linux下c语言进行mysql数据库操作---笔记
- linux下C语言编程操作MySQL数据库
- linux下C语言编程操作MySQL数据库
- Linux系统中MySQL数据库操作命令
- linux 环境下 c语言实现mysql数据库图片的存储以及多数据库直接的转存
- MYSQL数据库 - 学习笔记2 - C语言操作MYSQL
- 在Linux下用C语言操作数据库sqlite3(建立数据库)
- 通过登入IP记录Linux所有用户登录所操作的日志
- Linux下通过mysqldump备份MySQL数据库成sql文件
- 编写Linux下用C语言访问MySQL数据库的程序
- linux下对应mysql数据库的常用操作
- 通过unregister_filesystem()函数学习linux单链表操作