使用:
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL"); //db.setHostName("127.0.0.1"); db.setHostName("localhost"); db.setPort(3306); db.setUserName("root"); db.setPassword("szgy@GX*zd#xt_80km!");
// QString sqlname= QCoreApplication::applicationDirPath();
//qDebug()<<" sql path:"<< sqlname;
QString sqlname = “stuaaaaa”;
#if 0
QFile sqlnamefile(sqlname);
if(sqlnamefile.exists())
{
qDebug() << “database file exist!”;
}
else
{
qDebug() << “error database file NOT exist!”;
}
#endif
db.setDatabaseName(sqlname); if(db.open()) { qDebug() << "database is established!"; createtable(); return; } else { qDebug() << "database open error!" << db.lastError(); return; }