linux mysql c语言编程,在Linux下通过C语言操作MySQL数据库
2010年1月27日 晚 22:10
作者:longyun(http://www.linuxdiyf.com/mailto:mtd527@gmail.com)
續(xù):小弟最近想學(xué)習(xí)數(shù)據(jù)庫,并想開發(fā)一個簡單的數(shù)據(jù)庫管理系統(tǒng),需要用到 GTK+2.0 , C語言 , Mysql ;所以小弟查看了很多l(xiāng)inux下C操作mysql的資料,發(fā)現(xiàn)寫的都不怎么詳細(xì),所以小弟寫下這篇短文供大家一起交流學(xué)習(xí)!
實驗環(huán)境:
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>
創(chuàng)建一個數(shù)據(jù)庫
mysql> create database cusemysql;
Query OK, 1 row affected (0.00 sec)
并使用這個數(shù)據(jù)庫
mysql> use cusemysql;
Database changed
在給數(shù)據(jù)庫內(nèi)創(chuàng)建一個表
mysql> create table children(childno int not null unique,fname varchar(20),age int);
Query OK, 0 rows affected (0.00 sec)
在該表內(nèi)插入一項數(shù)據(jù)
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下,仔細(xì)看看你的在哪里?這種方式 #include "mysql/h" 可能也可以*/
int main(int argc, char *argv[])
{
MYSQL my_connection;
int res;
mysql_init(&my_connection);
/*mysql_real_connect(&mysql,主機(jī)名,用戶名,密碼,數(shù)據(jù)庫名,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));
/*里頭的函數(shù)返回受表中影響的行數(shù)*/
}
else
{
//分別打印出錯誤代碼及詳細(xì)信息
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,現(xiàn)在我們執(zhí)行看看
[mtd@mutiandong mysql]# ./insert
Connection Success
Inserted 1 rows
呵呵,真的成功了!
不信到mysql下看看表children中是否多了剛才插入的那一行數(shù)據(jù)
mysql> select * from children;
+---------+-------+------+
| childno | fname | age |
+---------+-------+------+
| 1 | jhon | 10 |
| 11 | Anny | 5 |
+---------+-------+------+
2 rows in set (0.00 sec)
學(xué)習(xí)linux的兄弟們都不容易,歡迎大家交流,Skype:mtd527
(因為linux下的QQ很不穩(wěn)定,功能太少,故留下Skype 帳號)
總結(jié)
以上是生活随笔為你收集整理的linux mysql c语言编程,在Linux下通过C语言操作MySQL数据库的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 灾字开头的成语有哪些?
- 下一篇: LINUX进程调度分析源码,Linux