MFC SQlite3封装工程

https://blog.csdn.net/fz835304205/article/details/46639715

最近要做一个小工具,用到数据库,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, szText, 0, 0, 0, 0);

}

++row;

}

pStmt->FreeQuery();

}

完整工程的下载地址:http://download.csdn.net/detail/fz835304205/8839049

每天进步一点点,进步来源于痛苦

(0)

相关推荐