1. 解决QSqlDatabasePrivate::removeDatabase: connection ‘myConnection’ is still in use, all queries will cease to work的问题
该问题主要是因为没有关闭之前的数据库连接,然后又需要创建新的数据库连接导致。解决方案:必须释放该连接的所有查询,即删除所有与该连接有关的query;同时在释放该连接时,需要先获取该连接的连接名,然后先关闭当前数据库,并将当前数据库使用默认构造函数覆盖,最后再移除当前连接。
解决代码:
void database::close()
{
QString connection;
connection = m_db.connectionName();
m_db.close();
m_db = QSqlDatabase();
m_db.removeDatabase(connection);
}
参考链接:http://stackoverflow.com/questions/9519736/warning-remove-database
http://stackoverflow.com/questions/8461333/sql-connection-still-open-even-after-deleting-it
2.解决Attempting to add QLayout “” to QDialog “”, which already has a layout的问题
该问题主要是由于将QDialog对象同时加入到了多个布局中间导致的,解决方案是在创建子布局时不要设置默认部件。即在创建布局时采用默认构造函数,不要设置部件。
解决代码:
QDialog* dlg = new QDialog(this);
//输入框
serverLineEdit = new QLineEdit(dlg);
//文本提示标签
QLabel* serverLabel = new QLabel(dlg);
//按钮
QPushButton *okBtn = new QPushButton(dlg);
QPushButton *cancelBtn = new QPushButton(dlg);
okBtn->setText(QString("确定"));
cancelBtn->setText(QString("取消"));
serverLabel->setText(QString("server:"));
//server栏布局
QHBoxLayout* ServerHLayout = new QHBoxLayout;//不要设置参数!!!
serverLabel->setFixedWidth(80);
serverLineEdit->setFixedWidth(200);
serverLineEdit->setText("127.0.0.1");
ServerHLayout->addWidget(serverLabel);
ServerHLayout->addWidget(serverLineEdit);
QHBoxLayout* btnHLayoutBtn = new QHBoxLayout;//不要设置参数!!!
btnHLayoutBtn->insertWidget(1,okBtn,0,Qt::AlignHCenter);
btnHLayoutBtn->insertWidget(2,cancelBtn,0,Qt::AlignHCenter);
QVBoxLayout *VLayout = new QVBoxLayout;//不要设置参数!!!
VLayout->addLayout(ServerHLayout);
VLayout->addLayout(btnHLayoutBtn);
VLayout->setSpacing(20);
QGridLayout* globLayout = new QGridLayout(dlg);//此处可以设置参数,顶级窗口
globLayout->addLayout(VLayout,2,10);
dlg->setLayout(globLayout);
connect(okBtn,SIGNAL(clicked()),this,SLOT(getDBLoginInfo()));
connect(okBtn,SIGNAL(clicked()),dlg,SLOT(close()));
connect(cancelBtn,SIGNAL(clicked()),dlg,SLOT(close()));
dlg->setModal(true); //阻塞父窗口
dlg->show();`
3. Qt判断SQL server中某张表是否存在的方法
解决代码:
selectSql ="select * from sys.tables where name='"+tableName+"'";
query->exec(selectSql);
if(query->next())
cout<<"the table is exsist!"<
cout<<"Not exsist!"<
4.Qt判断SQLite中某张表是否存在的方法
解决代码:
selectSql = QString("select count(*) from sqlite_master where type='table' and name='%1'").arg(tableName);
flag = query->exec(selectSql);
---------------------
作者:云鹤起舞
来源:CSDN
原文:https://blog.csdn.net/owen7500/article/details/53192060
版权声明:本文为博主原创文章,转载请附上博文链接!