Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4250109
  • 博文数量: 776
  • 博客积分: 13014
  • 博客等级: 上将
  • 技术积分: 10391
  • 用 户 组: 普通用户
  • 注册时间: 2010-02-22 17:00
文章分类

全部博文(776)

文章存档

2015年(55)

2014年(43)

2013年(147)

2012年(20)

2011年(82)

2010年(429)

分类: 数据库开发技术

2010-11-15 23:34:33

This is a simple example of selecting a column from an SQLite database using the C interface, with a bind statement.

The database has one table, called "t", with one column, called "xyz". See for creating the table and inserting data, as well as the macros in "mysqlite.h" given there.

The main program selects whatever is in the table "xyz" and then prints out each row:

#include 
#include 
#include 
#include 
#include "mysqlite.h"

int main (int argc, char ** argv)
{
    sqlite3 * db;
    char * sql;
    sqlite3_stmt * stmt;
    int nrecs;
    char * errmsg;
    int i;
    int row = 0;
    int arg;

    CALL_SQLITE (open ("/home/ben/test.db", & db));
    sql = "SELECT * FROM t WHERE xyz = ?";
    CALL_SQLITE (prepare_v2 (db, sql, strlen (sql) + 1, & stmt, NULL));

    for (arg = 1; arg < argc; arg++) {
        CALL_SQLITE (bind_text (stmt,
                                1, /* The number of the argument. */
                                argv[arg],
                                strlen (argv[arg]),
                                0 /* The callback. */
                                ));
        while (1) {
            int s;

            s = sqlite3_step (stmt);
            if (s == SQLITE_ROW) {
                int bytes;
                const unsigned char * text;
                bytes = sqlite3_column_bytes(stmt, 0);
                text  = sqlite3_column_text (stmt, 0);
                printf ("%d: %s\n", row, text);
                row++;
            }
            else if (s == SQLITE_DONE) {
                break;
            }
            else {
                fprintf (stderr, "Failed.\n");
                exit (1);
            }
        }
        CALL_SQLITE (reset (stmt));
        CALL_SQLITE (clear_bindings (stmt));
    }
    return 0;
}
阅读(1642) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~