本项目提供了一套基于Qt框架的数据库封装类,简化了SQL操作,提高了代码可读性和维护性。适用于各类需要数据库支持的应用程序开发。
```cpp
m_pDbProcPic = new CDBProcess(sqlserver);
m_pDbProc->openDB(mysvr, mydb, myusername, mypwd);
m_pDbProcPic = new CDBProcess(mysql);
m_pDbProc->openDB(localhost, mydb, root, password);
m_pDbProcPic = new CDBProcess(access);
m_pDbProc->openDB(, strMDB, , );
m_pDbProcPic = new CDBProcess(sqlite);
m_pDbProcPic->openDB(, mysqlitedb);
// 构造函数:CDBProcess(const QString strType);
// 参数为数据库类型,不区分大小写。支持的类型有 sqlite mysql access sqlserver
// 示例:
// CDBProcess db(sqlite);
bool openDB(
const QString& strSvrName, // 服务器名
const QString& strDBname, // 数据库名
const QString& strUserID = , // 用户名
const QString& strUserPwd = ); // 密码
// 打开数据库成功返回true,否则返回false。
// 对于sqlite,只有strDBname有效,其它参数忽略。如 db.openDB(, mysqlite.db)
// 对MS Access数据库,strSvrName为空,默认用户名为sa。 如 db.openDB(, myaccess.mdb);
// mysql和sqlserver需要提供所有参数。例如:db.openDB(svr1, mydb, user1, abcd);
void closeDB(); // 关闭数据库
// CDBProcess析构时,亦会自动关闭数据库
bool excuteSQL(const QString& strSql);
bool openRecordsetBySql(
const QString& strSql, // SQL语句
int idx = -1); // 记录集id,默认为-1。例如:db.openRecordsetBySql(SELECT * FROM TB1, 5);
void closeRecordset(int idx = -1);
// 关闭记录集,参数默认值为-1。
bool isOpen() const;
bool recEOF(int idx = -1) const;
// 记录集游标是否在结尾
bool recBOF(int idx = -1) const;
bool dropTable(const QString& strTbl); // 删除表
bool getFieldsValueFromRec(
int idx, // 记录集id
const char* szFldInfo, ... // 字段名加类型标识,如 sn%d, &iSN
);
int iSN;
QString strName;
double dHeight;
QDateTime dt;
QByteArray ba;
db.getFieldsValueFromRec(5,
sn%d, &iSN,
name%s, &strName,
height&f, &dHeight,
birthday%t, &dt,
photo%b, &ba);
bool updateTblFieldsValue(
const QString strTbl, // 表名
QString strWhere, // SQL WHERE 子句,如WHERE sn=20
const char * szFldInfo, ...); // 可变参数,同 addFieldsValueToTbl
long getRecordCount(int idx = -1) const;
bool moveFirst(int idx = -1) const;
bool movePrevious(int idx = -1) const;
bool moveNext(int idx = -1) const;
bool moveLast(int idx = -1) const;
bool moveTo(int n, int idx = -1) const;
QString getDbName() const;
// 下面的函数未验证
bool execStoreProcOfArgList(
int idx,
const QString strStoreProc,
const char* szFldsInfo, ...
);
bool exexProc(const QString strStoreProc,
QString str1,
QString& str2);
bool transaction();
bool commit();
bool rollback();
```