C++ 如何从 SQLite 数据库中读取数据?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3957343/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 14:08:27  来源:igfitidea点击:

How to read data from SQLite database?

c++cdatabasesqlite

提问by zaplec

I decided to use SQLite as it allows to store database into a single file. I think I have managed to do a database with SQLite Database Browser.

我决定使用 SQLite,因为它允许将数据库存储到单个文件中。我想我已经设法用SQLite Database Browser 创建了一个数据库。

How can I read that data in a C/C++ program?

如何在 C/C++ 程序中读取该数据?

采纳答案by Preet Sangha

How about the 'An Introduction to Sqlite C/C++ Interface', and there is a whole C++ example here on CodeProject.

' Sqlite C/C++ 接口简介'怎么样,CodeProject 上有一个完整的 C++ 示例。

This is bits of the more full sample,

这是更完整样本的一部分,

#include "CppSQLite.h"
#include <ctime>
#include <iostream>
using namespace std;
const char* gszFile = "C:\test.db";

int main(int argc, char** argv)
{
    try
    {
        int i, fld;
        time_t tmStart, tmEnd;
        CppSQLiteDB db;

        cout << "SQLite Version: " << db.SQLiteVersion() << endl;

        db.open(gszFile);
        cout << db.execScalar("select count(*) from emp;") 
               << " rows in emp table in ";
        db.Close();
    }
    catch (CppSQLiteException& e)
    {
        cerr << e.errorCode() << ":" << e.errorMessage() << endl;
    }
}

回答by Satyam

A example using sqlite read:

使用 sqlite 读取的示例:

#include <stdio.h>
#include <sqlite3.h>
#include <string.h>


int main(int argc, char** argv)
{
    const char*          username = "satyam";
    char                 q[999];
    sqlite3*             db;
    sqlite3_stmt*        stmt;
    int                  row = 0;
    int                  bytes;
    const unsigned char* text;

    if (2 == argc) {
        username = argv[1];
    }

    q[sizeof q - 1] = '
#include <stdio.h>
#include <string>
using std::string;
#include <sstream>
using std::stringstream;

#include "sqlite3.h"

bool find_employee(int _id)
{
    bool found = false;
    sqlite3* db;
    sqlite3_stmt* stmt;
    stringstream ss;

    // create sql statement string
    // if _id is not 0, search for id, otherwise print all IDs
    // this can also be achieved with the default sqlite3_bind* utilities
    if(_id) { ss << "select * from employees where id = " << _id << ";"; }
    else { ss << "select * from employees;"; }
    string sql(ss.str());

    //the resulting sql statement
    printf("sql: %s\n", sql.c_str());

    //get link to database object
    if(sqlite3_open("data/test.db", &db) != SQLITE_OK) {
        printf("ERROR: can't open database: %s\n", sqlite3_errmsg(db));
        sqlite3_close(db);
        return found;
    }

    // compile sql statement to binary
    if(sqlite3_prepare_v2(db, sql.c_str(), -1, &stmt, NULL) != SQLITE_OK) {
        printf("ERROR: while compiling sql: %s\n", sqlite3_errmsg(db));
        sqlite3_close(db);
        sqlite3_finalize(stmt);
        return found;
    }

    // execute sql statement, and while there are rows returned, print ID
    int ret_code = 0;
    while((ret_code = sqlite3_step(stmt)) == SQLITE_ROW) {
        printf("TEST: ID = %d\n", sqlite3_column_int(stmt, 0));
        found = true;
    }
    if(ret_code != SQLITE_DONE) {
        //this error handling could be done better, but it works
        printf("ERROR: while performing sql: %s\n", sqlite3_errmsg(db));
        printf("ret_code = %d\n", ret_code);
    }

    printf("entry %s\n", found ? "found" : "not found");

    //release resources
    sqlite3_finalize(stmt);
    sqlite3_close(db);

    return found;
}
'; snprintf( q, sizeof q - 1, "SELECT ipaddr FROM items WHERE username = '%s'", username ); if (sqlite3_open ("test.db", &db) != SQLITE_OK) { fprintf(stderr, "Error opening database.\n"); return 2; } printf("Query: %s\n", q); sqlite3_prepare(db, q, sizeof q, &stmt, NULL); bool done = false; while (!done) { printf("In select while\n"); switch (sqlite3_step (stmt)) { case SQLITE_ROW: bytes = sqlite3_column_bytes(stmt, 0); text = sqlite3_column_text(stmt, 1); printf ("count %d: %s (%d bytes)\n", row, text, bytes); row++; break; case SQLITE_DONE: done = true; break; default: fprintf(stderr, "Failed.\n"); return 1; } } sqlite3_finalize(stmt); return 0; }

回答by Giles

One way to do it without additional wrappers

一种无需额外包装的方法

##代码##