sqlLite 接口的使用 包含MFC控件CListCtrl代码示例
SqlLite是一款轻型的数据库,是遵守ACID的关系型数据库管理系统,它包含在一个相对小的C库中。它是D.RichardHipp建立的公有领域项目。它的设计目标是嵌入式的,而且目前已经在很多嵌入式产品中使用了它,它占用资源非常的低,在嵌入式设备中,可能只需要几百K的内存就够了。它能够支持Windows/Linux/Unix等等主流的操作系统,同时能够跟很多程序语言相结合,比如 Tcl、C#、PHP、Java等,还有ODBC接口,同样比起Mysql、PostgreSQL这两款开源的世界著名数据库管理系统来讲,它的处理速度比他们都快。SQLite第一个Alpha版本诞生于2000年5月。 至今已经有14个年头,SQLite也迎来了一个版本 SQLite 3已经发布。
下面利用SqlLite3 实现了简单的增、删、查、改,并将查询结果显示在MFC 的CListCtrl控件中:
1 //Sqlite.h 2 class _declspec (dllexport)Sqlite 3 { 4 public: 5 Sqlite(){} 6 ~Sqlite(){} 7 void Open(string name); 8 void Close(); 9 bool ExecDML(string sql); 10 int ExecScalar(string sql); 11 bool ExecQuery(string sql,vector<string> &fieldName,vector<string> &fieldValue); 12 bool CreateTable(string tableName,vector<string> &fieldName); 13 bool DeleteTable(string tableName); 14 bool InsertRow(string tableName,vector<string> &fieldName,vector<string> &fieldValue); 15 bool UpdateRow(string tableName,string key,string keyValue,vector<string> &fieldName,vector<string> &fieldValue); 16 bool DeleteRow(string tableName,string key,string keyValue); 17 private: 18 CppSQLite3DB m_db; 19 };
删表:
1 bool Sqlite::DeleteTable(string tableName) 2 { 3 string sql; 4 sql=string("drop table ")+tableName+";"; 5 //删除表table1 { "drop table table1"} 6 if(!m_db.tableExists(tableName.data()))return true; 7 int ret= m_db.execDML(sql.data()); 8 if(ret<0)return false; 9 return true; 10 }
建表:
1 bool Sqlite::CreateTable(string tableName,vector<string> &fieldName) 2 { 3 string sql; 4 if(m_db.tableExists(tableName.data()))return true; 5 sql=string("create table ")+tableName+" ("; 6 if(fieldName.size()<=0)return false; 7 vector <string>::iterator it; 8 for(it=fieldName.begin();it!=fieldName.end();it++) 9 { 10 sql+=(*it+" string,"); 11 } 12 sql[sql.size()-1]=')'; 13 sql+=";"; 14 //sql=" create table table1 (ID string,name string)" 15 int ret= m_db.execDML(sql.data()); 16 if(ret<0)return false; 17 return true; 18 }
向量查询,取得表行数等 :
1 //sql="select count(*) from table1" 2 int Sqlite::ExecScalar(string sql) 3 { 4 int ret=-1; 5 try{ 6 ret=m_db.execScalar(sql.data()); 7 }catch(string ex) { 8 return -1; 9 } 10 return ret; 11 }
插入数据:
1 //sql="insert into table1 ('ID','name') values('001','zhang');" 2 bool Sqlite::InsertRow(string tableName,vector<string> &fieldName,vector<string> &fieldValue) 3 { 4 // sql.Format("insert into UserTable ('domain','ip','port','time') values('%s','%s',%d,'%s');",domain,ip,port,t1); 5 string sql; 6 sql=string("insert into ")+tableName+" ("; 7 if(fieldName.size()<=0)return false; 8 vector <string>::iterator it; 9 for(it=fieldName.begin();it!=fieldName.end();it++) 10 { 11 sql+=("'"+*it+"',"); 12 } 13 sql[sql.size()-1]=')'; 14 sql+=" values("; 15 16 for(it=fieldValue.begin();it!=fieldValue.end();it++) 17 { 18 sql+=("'"+*it+"',"); 19 } 20 sql[sql.size()-1]=')'; 21 sql+=";"; 22 int ret= m_db.execDML(sql.data()); 23 if(ret<0)return false; 24 return true; 25 }
更改数据:
1 //sql="update table1 set ID = '004',name = 'zhao1' where ID = '004';" 2 bool Sqlite::UpdateRow(string tableName,string key,string keyValue,vector<string> &fieldName,vector<string> &fieldValue) 3 { 4 string sql; 5 if(fieldName.size()<=0)return false; 6 if(fieldName.size()!=fieldValue.size())return false; 7 sql=string("update ")+tableName+" set "; 8 vector <string>::iterator it; 9 vector <string>::iterator it1; 10 it1=fieldValue.begin(); 11 for(it=fieldName.begin();it!=fieldName.end();it++) 12 { 13 sql+=(*it+" = '"+*it1+"',"); 14 it1++; 15 } 16 sql[sql.size()-1]=' '; 17 if(key.data()&&keyValue.data()) 18 { 19 sql+=(string("where ")+key+" = '"+keyValue+"'"); 20 } 21 sql+=";"; 22 int ret= m_db.execDML(sql.data()); 23 if(ret<0)return false; 24 return true; 25 }
删除一行数据:
1 //sql="delete from table1 where ID = '004';"; 2 bool Sqlite::DeleteRow(string tableName,string key,string keyValue) 3 { 4 string sql; 5 sql=string("delete from ")+tableName+" "; 6 if(key.data()&&keyValue.data()) 7 { 8 sql+=(string("where ")+key+" = '"+keyValue+"'"); 9 } 10 sql+=";"; 11 int ret= m_db.execDML(sql.data()); 12 if(ret<0)return false; 13 return true; 14 }
查询,后文有更详细的使用代码:
1 //sql="select * from table1"; 2 bool Sqlite::ExecQuery(string sql,vector<string> &fieldName,vector<string> &fieldValue) 3 { 4 CppSQLite3Query q=m_db.execQuery(sql.data()); 5 fieldName.clear(); 6 fieldValue.clear(); 7 for(int i=0;i<q.numFields();i++) 8 { 9 fieldName.push_back(q.fieldName(i)); 10 } 11 while(!q.eof()) 12 { 13 for(int i=0;i<q.numFields();i++) 14 { 15 fieldValue.push_back(q.fieldValue(i)); 16 } 17 q.nextRow(); 18 } 19 return true; 20 }
demo code,how to use:
1 //插入了4条数据,删除一条,最后查询出所有数据 2 void Cmy_SqlLite_tDlg::OnBnClickedBnTest() 3 { 4 5 // TODO: 在此添加控件通知处理程序代码 6 int ret = 0; 7 Sqlite m; 8 m.Open("test.db"); 9 10 ret = m.DeleteTable("table1"); 11 TRACE("%d\n", ret); 12 vector<string> fields; 13 fields.push_back("ID"); 14 fields.push_back("name"); 15 ret = m.CreateTable("table1", fields); 16 TRACE("%d\n", ret); 17 ret = m.ExecScalar("select count(*) from table1"); 18 TRACE("%d\n", ret); 19 vector<string> Values; 20 Values.clear(); 21 Values.push_back("001"); 22 Values.push_back("zhang"); 23 ret = m.InsertRow("table1", fields, Values); 24 TRACE("%d\n", ret); 25 ret = m.ExecScalar("select count(*) from table1"); 26 TRACE("%d\n", ret); 27 Values.clear(); 28 Values.push_back("002"); 29 Values.push_back("wang"); 30 ret = m.InsertRow("table1", fields, Values); 31 TRACE("%d\n", ret); 32 ret = m.ExecScalar("select count(*) from table1"); 33 TRACE("%d\n", ret); 34 Values.clear(); 35 Values.push_back("003"); 36 Values.push_back("li"); 37 ret = m.InsertRow("table1", fields, Values); 38 TRACE("%d\n", ret); 39 ret = m.ExecScalar("select count(*) from table1"); 40 TRACE("%d\n", ret); 41 Values.clear(); 42 Values.push_back("004"); 43 Values.push_back("zhao"); 44 ret = m.InsertRow("table1", fields, Values); 45 TRACE("%d\n", ret); 46 ret = m.ExecScalar("select count(*) from table1"); 47 TRACE("%d\n", ret); 48 Values.clear(); 49 Values.push_back("004"); 50 Values.push_back("zhao1"); 51 ret = m.UpdateRow("table1", (char *)fields[0].data(), "004", fields, Values); 52 TRACE("%d\n", ret); 53 ret = m.DeleteRow("table1", (char *)fields[0].data(), "004"); 54 TRACE("%d\n", ret); 55 ret = m.ExecScalar("select count(*) from table1"); 56 TRACE("%d\n", ret); 57 58 59 vector<string> fieldName; 60 vector<string> fieldValue; 61 ret = m.ExecQuery("select * from table1", fieldName, fieldValue); 62 vector <string>::iterator it; 63 for (it = fieldValue.begin(); it != fieldValue.end(); it++) 64 { 65 MessageBox(*it); 66 67 } 68 69 m.Close(); 70 71 }
下面讨论下MFC CList控件显示查询结果:
先看看效果图:
代码:
1 void Cmy_SqlLite_tDlg::OnBnClickedBnselect() 2 { 3 // TODO: 在此添加控件通知处理程序代码 4 // CStringArray fieldName;CStringArray rowValue; 5 CppSQLite3DB m_db; 6 m_db.open("test.db"); 7 8 9 std::string sql = "select * from table1;"; 10 CppSQLite3Query q = m_db.execQuery(sql.data()); 11 12 13 //b 14 //删除所用行和列,reset CListCtrl 15 while (m_LsData.DeleteColumn(0)) // 因为你删除了第一列后,后面的列会依次向上移动。 16 m_LsData.DeleteAllItems(); 17 18 19 m_LsData.ModifyStyle(0L, LVS_REPORT); 20 m_LsData.ModifyStyle(0L, LVS_SINGLESEL); 21 m_LsData.ModifyStyle(0L, LVS_SHOWSELALWAYS); 22 m_LsData.ModifyStyle(0L, LVS_NOSORTHEADER); 23 m_LsData.SetExtendedStyle(LVS_EX_GRIDLINES); 24 //e 25 26 27 for (int i = 0; i<q.numFields(); i++) 28 { 29 // fieldName.push_back(q.fieldName(i)); 30 m_LsData.InsertColumn(i, q.fieldName(i), LVCFMT_LEFT); 31 m_LsData.SetColumnWidth(i, strlen(q.fieldName(i)) + 100); 32 } 33 int nRow = 0;//数据太大分页,或做限制 34 while (!q.eof()) 35 { 36 int nColumn = q.numFields(); 37 m_LsData.InsertItem(nRow, ""); 38 for (int i = 0; i<nColumn; i++) 39 { 40 //fieldValue()返回值好像一定是字符类型 41 m_LsData.SetItemText(nRow, i, q.fieldValue(i)); 42 43 } 44 nRow++; 45 q.nextRow(); 46 } 47 m_db.close(); 48 49 }
下面是使用SQLite Expert Professional 3查看数据看”test.db“:(注:不免费,我下载的这个只有30天试用期)
整个工程,我分享在百度网盘,感谢支持,boyang987 ,all copyright reserved.
下载链接: Sqlite interface down load