Onelong

分享知识,与你一起进步......
RSS icon Home icon
  • c语言操作sqlite3的入门教程

    post by onelong / 2010-7-9 20:13 Friday [数据库]

    linux下c函数库放在/usr/include里面,先去看看那里有没有sqlite3的函数库,如果有既不用装了,没有的就按装一下吧!ubuntu下安装软件是那么的简单,不多说了!
    测试一下先:sqlite.c的源码如下.....
    #include <stdlib.h>
    #include <stdio.h>
    #include "sqlite3.h"

    int main(void)
    {
        sqlite3 *db=NULL;
        char *zErrMsg =0;       
        int rc;
        rc=sqlite3_open("test1.db",&db);
        if(rc)
        {
            fprintf(stderr,"Can't open database: %s\n",sqlite3_errmsg(db));
            sqlite3_close(db);
            exit(1);
        }
        else
            {
                printf("open mydata successfully!\n");
                rc = sqlite3_exec(db, "CREATE TABLE chn_to_eng(chinese QString, english QString)", 0, 0, &zErrMsg);
                char *sql="INSERT INTO [chn_to_eng] ([chinese], [english]) VALUES ('ddd','fgg')";
                rc = sqlite3_exec(db, sql, 0, 0, &zErrMsg);
            }
        sqlite3_close(db);
        return 0;
    }
    编译使用的命令:gcc sqlite.c -o sqlite -L/full/path/to/directory/with/libsqlite3/ -lsqlite3
            或gcc sqlite.c -o sqlite -lsqlite3
    运行:./sqlite
    这个时候你查看当前目录,会发现多了一个文件test1.db
    编译时要注意的问题 如果只是用:gcc sqlite.c -o sqlite
    会提示错误
    /tmp/ccqqKvmp.o: In function `main':
    sqlite.c:(.text+0x2e): undefined reference to `sqlite3_open'
    sqlite.c:(.text+0x42): undefined reference to `sqlite3_errmsg'
    sqlite.c:(.text+0x67): undefined reference to `sqlite3_close'
    sqlite.c:(.text+0xa9): undefined reference to `sqlite3_exec'
    sqlite.c:(.text+0xdc): undefined reference to `sqlite3_exec'
    sqlite.c:(.text+0xea): undefined reference to `sqlite3_close'
    collect2: ld returned 1 exit status
     

    引用地址:
     

    我要评论