- #include <QSqlDatabase>
- #include <QMessageBox>
- class SqliteApp{
- public:
- SqliteApp(QString db_path){
- this->db_path = db_path;
- this->opencase = false;
- this->conn = QSqlDatabase::addDatabase("QSQLITE");
- this->conn.setDatabaseName(this->db_path);
- }
- int Open(){
- this->opencase = this->conn.open();
- return this->opencase;
- }
- QSqlQuery query(QString strSQL){
- QSqlQuery rs(this->conn);
- rs.exec(strSQL);
- return rs;
- }
- void execute(QString strSQL){
- QSqlQuery rs;
- rs.exec(strSQL);
- }
- QStringList db_table(){
- this->tables = this->conn.tables(QSql::Tables);
- return this->tables;
- }
- void Close(){
- opencase = false;
- this->conn.close();
- }
- QString setOpenSign(){
- if(this->opencase = true){
- return "true";
- }
- else{
- return "false";
- }
- }
- private:
- bool opencase; // 数据库链接状态
- QString db_path; // 数据库物理地址
- QString strconn; // 数据库链接字段
- QSqlDatabase conn; // 数据库链接对象
- QStringList tables; // 数据库中的表
- };
- //该片段来自于http://www.codesnippet.cn/detail/1011201410934.html
来源: http://www.codesnippet.cn/detail/1011201410934.html