在
上一篇中,已经在Windows 10 中,使用QT 6.5.0成功的连接上了MYSQL数据库。
这一篇介绍怎样判断MySQL数据库中数据表是否存在。
请看代码:
bool HtMainWindow::HtIsTableExist(QString tableName){
if(!h_db.isOpen())
{
QMessageBox::information(this,"Hovertree","请先打开数据库连接");
return false;
}
bool m_resutl = false;
QSqlQuery m_query(h_db);
m_query.exec(QString("select count(*) from information_schema.TABLES where TABLE_NAME = '%1'").arg(tableName)); //关键的判断
if (m_query.next())
{
if (m_query.value(0).toInt() > 0)
{
m_resutl = true;
}
}
return m_resutl;
}
其中h_db请参考上一篇:
https://hovertree.com/h/bjag/9xwddo8v.htm调用的代码:
QString m_tabelName="hovertree";
if(HtIsTableExist(m_tabelName))
{
QMessageBox::information(this,"Hovertree","数据表已存在");
return;
}
执行结果如下图:

备注,测试环境为Windows 10,MinGW 64位编译器