미분류
SQLite C example
webnautes
2013. 4. 26. 04:57
반응형
출처
http://nano-chicken.blogspot.kr/2012_08_01_archive.html
http://snortbit.blog.163.com/blog/static/189573172201331593547914/
#include <iostream> #include <sqlite3.h> #include <cstdlib> #include <assert.h> using namespace std; int main(int argc, char** argv) { sqlite3 *conn; sqlite3_stmt *statement; ⁄⁄ SQL Statement Object int ret = 0; int cols; ⁄⁄ This routine opens a connection to an SQLite database file ⁄⁄ and returns a database connection object. ret = sqlite3_open_v2("hello.db", &conn, SQLITE_OPEN_READONLY, NULL); if (ret) { cout << "can not open database\n"; exit(0); } ret = sqlite3_prepare_v2(conn, "select * from hello", -1, &statement, NULL); if (ret != SQLITE_OK) { cout << "We did not get any data\n"; exit(0); } cols = sqlite3_column_count(statement); for (int col = 0; col < cols; col++) { cout << " " << sqlite3_column_name(statement, col); }; cout << endl; while (true) { ret = sqlite3_step(statement); if (ret == SQLITE_ROW) { for (int col = 0; col < cols; col++) { switch (sqlite3_column_type(statement, col)) { case SQLITE_INTEGER: cout << " " << sqlite3_column_int(statement, col) << " "; break; case SQLITE_FLOAT: cout << " " << sqlite3_column_double(statement, col) << " "; break; case SQLITE_TEXT: cout << " " << sqlite3_column_text(statement, col) << " "; break; case SQLITE_NULL: cout << " " << "NULL" << " "; break; } }; cout << endl; } else if (ret == SQLITE_DONE) { cout << "done" << endl; break; } else { cout << "ret:" << ret << endl; break; } } sqlite3_finalize(statement); sqlite3_close(conn); return 0; }
#include "stdafx.h" #include "sqlite3.h" static int callback(void *NotUsed, int argc, char **argv, char **azColName) { int i; for(i=0; i<argc; i++){ printf("%s = %s⁄n", azColName[i], argv[i] ? argv[i] : "NULL"); } printf("⁄n"); return 0; } #define CHECK_RC(rc,szInfo,szErrMsg,db) if(rc!=SQLITE_OK) ⁄ {printf("%s error!⁄n",szInfo);⁄ printf("%s⁄n",szErrMsg); ⁄ sqlite3_free(szErrMsg); ⁄ sqlite3_close(db); ⁄ return 0;} int _tmain(int argc, _TCHAR* argv[]) { sqlite3 *db; char *dbPath="f:⁄test.db"; char *szErrMsg = 0; int rc= sqlite3_open(dbPath, &db); CHECK_RC(rc,"open database",db); char *szSql="create table UserInfo(ID int primary key , UserName char, PassWord char);"; rc=sqlite3_exec(db,szSql,0,0,&szErrMsg); CHECK_RC(rc,"create table",szErrMsg,db); rc=sqlite3_exec(db,"insert into UserInfo(ID,UserName,PassWord) values(1,'kfqcome','123456')",0,0,&szErrMsg); CHECK_RC(rc,"insert info",szErrMsg,db); rc=sqlite3_exec(db,"insert into UserInfo(ID,UserName,PassWord) values(2,'miss wang','654321')",0,0,&szErrMsg); CHECK_RC(rc,"insert info",szErrMsg,db); szSql="select * from UserInfo"; rc = sqlite3_exec(db,szSql, callback, 0, &szErrMsg); CHECK_RC(rc,"query values",szErrMsg,db); sqlite3_close(db); getchar(); return 0; }
#include <stdio.h> #include <sqlite3.h> int main(int argc, char **argv) { sqlite3 *conn; sqlite3_stmt *res; int err = 0, cnt = 0; const char *err_msg; const char *tail; err = sqlite3_open("ship.sqlite3", &conn); if (err) { printf("Can not open database\n"); return 1; } err = sqlite3_exec(conn, "update ship set name=\'NCC-1701-D\' where id=3", 0, 0, 0); err = sqlite3_prepare_v2(conn, "select id,name from ship order by id", 1000, &res, &tail); if (err != SQLITE_OK) { printf("Select failed"); return 1; } while (sqlite3_step(res) == SQLITE_ROW) { printf("%u ", sqlite3_column_int(res, 0)); printf("%s\n", sqlite3_column_text(res, 1)); cnt++; } sqlite3_finalize(res); sqlite3_close(conn); return 0; }
반응형