本文提到的這兩個(gè)問(wèn)題是我在編譯時(shí)出現(xiàn)的問(wèn)題,然后就是用本文提到的方法解決的,特意記下來(lái)方便以后查看柔纵。
使用linux下的C操作MYSQL
編譯時(shí)提示頭文件include <mysql/mysql.h>不存在的解決辦法:
先找到mysql.h文件在哪里,gcc test.c -o test -I /usr/lib/mysql/include -L /usr/lib/mysql/ -lmysqlclient 加‘-I’導(dǎo)向頭文件mysql.h所在的文件目錄
解決可以嘗試安裝mysql-devel:
$ sudo yum install mysql-devel -y //RHEL,Centos,Fedora
$ sudo apt-get install libmysqlclient-dev -y //Ubuntu
如果已經(jīng)安裝成功了蔫敲,找到mysql.h的文件路徑柬姚,-I 編譯即可
$ sudo find /usr/ -name 'mysql.h'
$ gcc -I/usr/include/mysql ...
使用linux下的C操作SQLLITE
由于linux下側(cè)重使用命令郁轻,沒(méi)有win的操作容易上手,所以在測(cè)試C操作SQLITE時(shí)會(huì)比較容易出現(xiàn)錯(cuò)誤笑旺,給大家做一個(gè)簡(jiǎn)單的程序進(jìn)行測(cè)試昼浦,演示怎么應(yīng)用。
#include <stdio.h>
#include <sqlite3.h>
int main( void )
{
sqlite3 *db=NULL;
char *zErrMsg = 0;
int rc;
//打開(kāi)指定的數(shù)據(jù)庫(kù)文件,如果不存在將創(chuàng)建一個(gè)同名的數(shù)據(jù)庫(kù)文件
rc = sqlite3_open("zieckey.db", &db);
if( rc )
{
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
sqlite3_close(db);
exit(1);
}
else printf("You have opened a sqlite3 database named zieckey.db successfully!\nCongratulations! Have fun ! ^-^ \n");
sqlite3_close(db); //關(guān)閉數(shù)據(jù)庫(kù)
return 0;
}
退出筒主,保存关噪。(代碼輸入完成后,按下 Esc 鍵乌妙,然后輸入: :wq ,回車(chē)就好拉)
編譯:[root@localhost temp]# gcc opendbsqlite.c -o db.out
或者遇到這樣的問(wèn)題:
[root@localhost temp]# gcc opendbsqlite.c -o db.out
opendbsqlite.c:11:21: sqlite3.h: 沒(méi)有那個(gè)文件或目錄
opendbsqlite.c: In function `main':
opendbsqlite.c:19: `sqlite3' undeclared (first use in this function)
opendbsqlite.c:19: (Each undeclared identifier is reported only once
opendbsqlite.c:19: for each function it appears in.)
opendbsqlite.c:19: `db' undeclared (first use in this function)
這是由于沒(méi)有找到頭文件的原因使兔。
也許會(huì)碰到類(lèi)似這樣的問(wèn)題:
[root@localhost temp]# gcc opendbsqlite.c -o db.out
/tmp/ccTkItnN.o(.text+0x2b): In function `main':
: undefined reference to `sqlite3_open'
/tmp/ccTkItnN.o(.text+0x45): In function `main':
: undefined reference to `sqlite3_errmsg'
/tmp/ccTkItnN.o(.text+0x67): In function `main':
: undefined reference to `sqlite3_close'
/tmp/ccTkItnN.o(.text+0x8f): In function `main':
: undefined reference to `sqlite3_close'
collect2: ld returned 1 exit status
這是個(gè)沒(méi)有找到庫(kù)文件的問(wèn)題。
[root@localhost temp]# gcc opendbsqlite.c -o db.out -lsqlite3 -L/usr/local/sqlite3/lib -I/usr/local/sqlite3/include
這樣編譯應(yīng)該就可以了藤韵。最后運(yùn)行'./db.out'可執(zhí)行文件火诸。