嵌入式数据库SqLite3使用入门

来源:互联网 发布:1元域名 编辑:程序博客网 时间:2024/05/01 22:37
    最近一直在开发一个基于net-snmp的网络设备管理程序,由于该项目规模比较庞大,需要在潜入式系统里移植数据库,考虑该系统的高效和硬件资源,最后选择了SqLite3作为数据引擎,下面简要阐述一下该数据库的安装和使用方法:
    一、下载及安装
    可以到它的官方网站www.sqlite.org或者直接到www.chinaunix.com去down。

    按照linux一般的安装方法,参照开发包提供的README文档
    tar -zxvf sqlite-3.3.5.tar.gz
    mkdir bld
    cd bld
    ../sqlite-3.3.5/configure
    make
    make install

    安装之后的应用程序在/usr/bin/sqlite3
    开发所用的库文件在  /usr/local/lib/libsqlite3.so
    开发所需要的头文件  /usr/local/include/sqlite3.h
    二、数据库的创建及使用
    sqlite3 test.db   //Create the database
      sqlite3> create table tb1(str varchar(20), intVar smallint, indexVar smallint);  //Create the table
      sqlite3> insert into tb1 values('Hello World!', 100, 1);
      sqlite3> select * from tb1;
        Hello World!|100|1
      sqlite3> .quit              //quit
     在使用上还是比较简单的,遵循基本的SQL语句,注意每条语句后面的;
     三、编程实现数据库操作的方法
    
    #include <stdio.h>
    #include <sqlite3.h>
    #include <stdlib.h>

    int main(int argc, char * argv[])
    {
        sqlite3 * test_db;
        int rc = 0;

        rc = sqlite3_open("test.db", &test_db);
   
        if (rc)
        {
            fprintf(stderr, "Can't open database:%s/n", sqlite3_errmsg(test_db));
            sqlite3_close(test_db);
            exit(1);
        }
        else
        {
            fprintf(stdout, "Open database OK./n");
        }

        sqlite3_close(test_db);


        return 0;
    }
    这样就可以建好一个数据库了,关于更深层次的使用我也还在摸索。