linux c 连接mysql数据库
作者:zieckey(zieckey@yahoo.com.cn)
All Right Reserved!
1. mysql在linux下的编译和安装
[root@localhost zieckey]# mkdir /usr/local/mysql[root@localhost zieckey]# cp mysql-4.0.12.tar.gz /home/[root@localhost root]# cd /home/
[root@localhost home]# tar zxvf mysql-4.0.12.tar.gz[root@localhost home]# cd mysql-4.0.12
[root@localhost mysql-4.0.12]# ./configure --prefix=/usr/local/mysql --without-debug --with-extra-charsets=gb2312 --enable-assembler --without-isam --without-innodb --with-pthread --enable-thread-safe-client
[root@localhost mysql-4.0.12]# make
[root@localhost mysql-4.0.12]# make install[root@localhost mysql-4.0.12]# scripts/mysql_install_db
[root@localhost mysql-4.0.12]# groupadd mysql[root@localhost mysql-4.0.12]# useradd -g mysql mysql
[root@localhost mysql]# chown -R root /usr/local/mysql[root@localhost mysql]# chown -R mysql /usr/local/mysql/var[root@localhost mysql]# chgrp -R mysql /usr/local/mysql
[root@localhost mysql-4.0.12]# cd /usr/local/mysql/bin/[root@localhost bin]# export PATH=$PATH:/usr/local/mysql/bin/[root@localhost bin]# export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/mysql/lib/mysql/[root@localhost bin]# env
启动mysql服务器[root@localhost root]# cd /usr/local/mysql/bin/[root@localhost bin]# ./mysqld_safe -u mysql&Starting mysqld daemon with databases from /usr/local/mysql/var
[root@localhost root]# pgrep mysql159311595015951
[root@localhost root]# /usr/local/mysql/bin/mysqlWelcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 1 to server version: 4.0.12Type 'help;' or '\h' for help. Type '\c' to clear the buffer.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,"花儿",10);Query OK, 1 row affected (0.00 sec)mysql> select * from children;+---------+-------+------+| childno | fname | age |+---------+-------+------+| 5 | 花儿 | 10 |+---------+-------+------+1 row in set (0.03 sec)mysql>
////////////////////////////////////* insert.c */#include <stdio.h>#include <stdlib.h>#include "mysql.h"/*注意哦,上面也可以是mysql.h的绝对地址,一般在mysql下的include目录下,仔细看看你的在哪里?*/int main(int argc, char *argv[]){ MYSQL my_connection; int res; mysql_init(&my_connection); /*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;}[root@zieckey 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[root@zieckey mysql]#
[root@zieckey 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[root@zieckey mysql]#
[root@localhost testmysql]# gcc -o insert insert.c -lmysqlclient -I/usr/local/mysql/include/mysql/ -L/usr/local/mysql/lib/mysql
[root@zieckey mysql]# ./insertConnection SuccessInserted 1 rows
ln -s /usr/local/mysql/lib/mysql/libmysqlclient.so.15.0.0 /usr/lib/libmysqlclient.soln -s /usr/local/mysql/lib/mysql/libmysqlclient.so.15.0.0 /usr/lib/libmysqlclient.so.15ln -s /usr/local/mysql/lib/mysql/libmysqlclient.so.15.0.0 /usr/lib/libmysqlclient.so.6.0.0
mysql> select * from children;+---------+-------+------+| childno | fname | age |+---------+-------+------+| 5 | 花儿 | 10 || 11 | Anny | 5 |+---------+-------+------+2 rows in set (0.00 sec)