使用linux下的C操作SQLLITE

来源:互联网 发布:眼镜厂景品手办淘宝店 编辑:程序博客网 时间:2024/05/16 15:34

from: http://baike.so.com/doc/1529694.html

由于linux下侧重使用命令,没有win的操作容易上手,所以在测试C操作SQLITE时会比较容易出现错误,给大家做一个简单的程序进行测试,演示怎么应用。打开vi编辑器,输入如下代码:

   /*c代码*/   #include <stdio.h>  #include <sqlite3.h>  int main( void )  {  sqlite3 *db=NULL;  char *zErrMsg = 0;  int rc;  //打开指定的数据库文件,如果不存在将创建一个同名的数据库文件  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); //关闭数据库  return 0;  }


  退出,保存。(代码输入完成后,按下 Esc 键,然后输入: :wq ,回车就好拉)

  好拉,现在编译:[root@localhost temp]# gcc opendbsqlite.c -o db.out

  或者遇到这样的问题:

  [root@localhost temp]# gcc opendbsqlite.c -o db.out

  opendbsqlite.c:11:21: sqlite3.h: 没有那个文件或目录

  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)

  这是由于没有找到头文件的原因。

  也许会碰到类似这样的问题:

  [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

  这是个没有找到库文件的问题。

  [root@localhost temp]# gcc opendbsqlite.c -o db.out -lsqlite3 -L/usr/local/sqlite3/lib -I/usr/local/sqlite3/include

  这样编译应该就可以了

  如果还是有错误的话请搜索:(一)Sqlite数据库连接。有更详细的说明解释    


原创粉丝点击