数据库之Sqlite3

来源:互联网 发布:工程进度计划软件 编辑:程序博客网 时间:2024/05/24 23:14

 

#include "sqlite3.h"sqlite3 *db;//1 打开数据库char info[1024] = {0};int rc=sqlite3_open( "mydb_v1.db", &db );if( rc ){    sprintf( info, "can not open database :%s", sqlite3_errmsg(db) );    sqlite3_close( db );    db = NULL;}//open database success//2 关闭数据库if( db ){    sqlite3_close( db );    db = NULL;}//close database success//3 select operationif(!db){    //db is not open    return ;}sqlite3_stmt *stmt;const char *tail;string str;string sql = "select * from CodeCode where BarCode='2014003680'";int rc = sqlite3_prepare(db, sql.c_str(), sql.size(), &stmt, &tail);if(rc!=SQLITE_OK){    string error(sqlite3_errmsg(db));    return;}rc=sqlite3_step(stmt);int ncols=sqlite3_column_count(stmt);vector<string> oColumnNames;for (int i = 0; i < ncols; ++i){    string sColumnName = sqlite3_column_name(stmt, i);    oColumnNames.push_back(sColumnName);}while(rc==SQLITE_ROW){    vector<string> row;    for (int i = 0; i < ncols; ++i)    {        string sValue((char *)sqlite3_column_text(stmt,i));        row.push_back(sValue);    }    rc=sqlite3_step(stmt);}sqlite3_finalize(stmt); //4 insert, update, delete operationssqlite3_stmt *stmt;char *error;string str;string sql = "insert into CoalSample values('2014003681', 'tt1', '2014-11-21')";int rc = sqlite3_exec(db, sql.c_str(), NULL, NULL, &error);if(rc != SQLITE_OK){    if(error != NULL)    {        str = string(error);        sqlite3_free(error);    }    return;}//insert into database success


 

0 0
原创粉丝点击