最近要做一个小工具,用到数据库,sql的数据库太麻烦,只是一个简单的数据库太多的功能也用不到,因此决定用sqlite3,以前在linux下玩过,但是不太深入,这次老大把活派下来了,没办法硬着头皮往前冲吧,于是开始在网上找相关的资料,找到了两个历程但是不是很理想,封装的水平感觉有限,最后在外国的网站上发现了一点干货
Kompex sqlite Wrapper for C++
http://sqlitewrapper.kompex-online.com/index.PHP?content=home
非常完美
void CsqliteMFCDlg::OnBnClickedButtonRead() { // TODO: 在此添加控件通知处理程序代码 Kompex::sqliteDatabase *pDatabase = new Kompex::sqliteDatabase("world.db3",sqlITE_OPEN_READWRITE | sqlITE_OPEN_CREATE,0); // move database to memory,so that we are work on the memory database hence pDatabase->MoveDatabaseToMemory(); // create statement instance for sql queries/statements Kompex::sqliteStatement *pStmt = new Kompex::sqliteStatement(pDatabase); CString res,name; int id; #if 0 //res = pStmt->GetsqlResultString("SELECT NAME FROM sheet1 WHERE ID = 1;"); pStmt->sql("SELECT * FROM sheet1;"); // process all results while(pStmt->FetchRow()) { id = pStmt->GetColumnInt("ID"); name = pStmt->GetColumnCString("NAME"); } // do not forget to clean-up pStmt->FreeQuery(); #endif //pStmt->sql(_T("SELECT * FROM sheet1 WHERE NAME LIKE '黄进';")); pStmt->sql(_T("SELECT * FROM Country;")); m_List.DeleteAllItems(); while(m_List.DeleteColumn(0)); CRect rect; m_List.GetWindowRect(&rect); int colCnt = pStmt->GetColumnCount(); int width = rect.Width() / colCnt; int col = 0; int row = 0; CString szText; if(width < 160) width = 160; for(col = 0; col < colCnt; col++) { szText = pStmt->GetColumnName16(col); m_List.InsertColumn(col,szText,LVCFMT_LEFT,width); } while(pStmt->FetchRow()) { szText = pStmt->GetColumnString16(0); m_List.InsertItem(row,szText); for(col = 1; col < colCnt; col++) { szText = pStmt->GetColumnString16(col); m_List.SetItem(row,col,LVIF_TEXT,0); } ++row; } pStmt->FreeQuery(); }
每天进步一点点,进步来源于痛苦~P