Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1810108
  • 博文数量: 496
  • 博客积分: 12043
  • 博客等级: 上将
  • 技术积分: 4778
  • 用 户 组: 普通用户
  • 注册时间: 2010-11-27 14:26
文章分类

全部博文(496)

文章存档

2014年(8)

2013年(4)

2012年(181)

2011年(303)

2010年(3)

分类: SQLite/嵌入式数据库

2011-03-22 17:40:35

最近因为学校那个项目,也在搞数据库方面的东西,以前也用过SQLITE的东西,拿来复习下
    Sqlite是一个面向系统的数据库,编译完成只有200K,同时支持2T的数据记录。对于设备是一个 很好的数据库引擎。本文通过一个小例子说明如何在C与C++调用Sqlite API完成数据库的创建、插入数据与查询数据。本文的开发环境为(Redhat9.0 + Qtopia2.1.2 + Sqlite3)
    安装Sqlite3:
    从上Sqlite3.2.2运源代码,依照Readme中的步骤:
    tar xzf sqlite3.2.2.tar.gz
    mkdir bld
    cd bld
    ../sqlite3.2.2/configure
    make
    make install
    然后在shell下运行 sqlite3 test.db命令可以检验是否已经安装成功。
    创建数据库:
     sqlite3 *pDB = NULL;
     char * errMsg = NULL;

     //打开一个数据库,如果改数据库不存在,则创建一个名字为databaseName的数据库文件
     int rc = sqlite3_open(databaseName, &pDB);

     if(rc)
     {
        cout << " Open the database " << databaseName << " failed" << endl;
     }

     //如果创建成功,添加表
     else
     {
        cout << "create the database successful!" << endl;

        //creat the table
        int i;
        for(i=1; i        {

        }
        //插入一个表,返回值为SQLITE_OK为成功,否则输出出错信息
        //函数参数:第一个为操作数据库的指针,第二句为SQL命令字符串
        //第三个参数为callback函数,这里没有用,第四个参数为callback函数
        //中的第一个参数,第五个为出错信息
        rc = sqlite3_exec(pDB, "CREATE TABLE chn_to_eng(chinese QString, english QString)", 0, 0, &errMsg);

        if(rc == SQLITE_OK)
           cout << "create the chn_to_eng table successful!" << endl;
        else
           cout << errMsg << endl;

        //同上,插入另一个表
        rc = sqlite3_exec(pDB, "CREATE TABLE eng_to_chn(english QString, chinese QString)", 0, 0, &errMsg);

        if(rc == SQLITE_OK)
            cout << "create the eng_to_chn table successful!" << endl;
        else
           cout << errMsg << endl;

     }
      、、、、、、
      //往表中添加数据
      char chn[]="...";
      char eng[]="...";
      char value[500];
      //定义一条参数SQL命令,其中chn,eng为需要插入的数据
      sprintf(value, "INSERT INTO chn_to_eng(chinese, english) VALUES('%s', '%s')", chn, eng);

      //use the SQLITE C/ API to create and adjust a database.
      rc = sqlite3_exec(pDB,
                           value,
                          0, 0, &errMsg);
     //查询一条记录
     char value[500];
     //定义一条查询语句,其中条件为当english为target时的中文记录
     //print_result_cb为callback函数,在其中可以得到查询的结果,具体见下文
     sprintf(value, "SELECT chinese FROM eng_to_chn where english='%s' ", target);
     rc = sqlite3_exec(pDB,
                          value,
                          print_result_cb, 0, &errMsg);

      if(rc == SQLITE_OK)
          {
    //        #ifdef_debug
               cout << "select the record successful!" << endl;
    //        #endif
          }
          else
          {
    //        #ifdef_debug
              cout << errMsg << endl;
    //        #endif
              return false;
          }
    .......
    }

//callback回调函数print_result_cb的编写,其中data为sqlite3_exec中的第四个参数,第二个参数是栏的数目,第三个是栏的名字,第四个为查询得到的值得。这两个函数输出所有查询到的结果
    int print_result_cb(void* data, int n_columns, char** column_values,
                        char** column_names)
    {
        static int column_names_printed = 0;
        int i;
        if (!column_names_printed) {
            print_row(n_columns, column_names);
            column_names_printed = 1;
        }

        print_row(n_columns, column_values);
        return 0;
    }

    void print_row(int n_values, char** values)
    {
        int i;
        for (i = 0; i < n_values; ++i) {
            if (i > 0) {
                printf("\t");
            }

            printf("%s", values[i]);

        }
        printf("\n");
    }

    大致过程就是如此,具体可以参考SQLITE的API函数说明,见


    void print_row(int n_values, char** values)
    {
        int i;
        for (i = 0; i < n_values; ++i) {
            if (i > 0) {
                printf("\t");
            }

            printf("%s", values[i]);

        }
        printf("\n");
    }

    大致过程就是如此,具体可以参考SQLITE的API函数说明,见


    void print_row(int n_values, char** values)
    {
        int i;
        for (i = 0; i < n_values; ++i) {
            if (i > 0) {
                printf("\t");
            }

            printf("%s", values[i]);

        }
        printf("\n");
    }

    大致过程就是如此,具体可以参考SQLITE的API函数说明,见


      char value[500];
      //定义一条参数SQL命令,其中chn,eng为需要插入的数据
      sprintf(value, "INSERT INTO chn_to_eng(chinese, english) VALUES('%s', '%s')", chn, eng);

      //use the SQLITE C/ API to create and adjust a database.
      rc = sqlite3_exec(pDB,
                           value,
                          0, 0, &errMsg);
     //查询一条记录
     char value[500];
     //定义一条查询语句,其中条件为当english为target时的中文记录
     //print_result_cb为callback函数,在其中可以得到查询的结果,具体见下文
     sprintf(value, "SELECT chinese FROM eng_to_chn where english='%s' ", target);
     rc = sqlite3_exec(pDB,
                          value,
                          print_result_cb, 0, &errMsg);

      if(rc == SQLITE_OK)
          {
    //        #ifdef_debug
               cout << "select the record successful!" << endl;
    //        #endif
          }
          else
          {
    //        #ifdef_debug
              cout << errMsg << endl;
    //        #endif
              return false;
          }
    .......
    }
    //callback回调函数print_result_cb的编写,其中data为sqlite3_exec中的第四个参数,第二个参数是栏的数目,第三个是栏的名字,第四个为查询得到的值得。这两个函数输出所有查询到的结果
    int print_result_cb(void* data, int n_columns, char** column_values,
                        char** column_names)
    {
        static int column_names_printed = 0;
        int i;
        if (!column_names_printed) {
            print_row(n_columns, column_names);
            column_names_printed = 1;
        }

        print_row(n_columns, column_values);
        return 0;
    }

    void print_row(int n_values, char** values)
    {
        int i;
        for (i = 0; i < n_values; ++i) {
            if (i > 0) {
                printf("\t");
            }

            printf("%s", values[i]);

        }
        printf("\n");
    }

 

阅读(1207) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~