使用linux下的C操作SQLLITE

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数据库连接。有更详细的说明解释    


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值