使用linux下的C操作SQLLITE

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

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

 

  1.  /*c代码*/  
  2.  #include <stdio.h>  
  3. #include <sqlite3.h>  
  4.   
  5. int main( void )  
  6.   
  7. {  
  8. sqlite3 *db=NULL;  
  9. char *zErrMsg = 0;  
  10. int rc;  
  11.   
  12. //打开指定的数据库文件,如果不存在将创建一个同名的数据库文件  
  13. rc = sqlite3_open("zieckey.db", &db);  
  14. if( rc )  
  15. {  
  16. fprintf(stderr, "Can't open database: %s ", sqlite3_errmsg(db));  
  17. sqlite3_close(db);  
  18. exit(1);  
  19. }  
  20. else printf("You have opened a sqlite3 database named zieckey.db successfully! Congratulations! Have fun ! ^-^  ");  
  21. sqlite3_close(db); //关闭数据库  
  22. return 0;  
  23. }  
   /*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
", sqlite3_errmsg(db));
  sqlite3_close(db);
  exit(1);
  }
  else printf("You have opened a sqlite3 database named zieckey.db successfully!
Congratulations! Have fun ! ^-^ 
");
  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数据库连接。有更详细的说明解释    

原文地址:https://www.cnblogs.com/zxc2man/p/7462120.html