#include
#include
#include
int main(int argc,char* argv[]){
QApplication app(argc,argv);
QTextCodec::setCodecForTr(QTextCodec::codecForName("utf-8"));
QSqlDatabase db=QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("localhost");
db.setDatabaseName("test");
db.setUserName("root");
db.setPassword("12345678");
if(!db.open()){
QMessageBox::warning(0,QObject::tr("无法打开数据库!"),db.lastError().text());
qDebug()<<:tr connection fail>
return -1;
}
QSqlQuery query;
bool bSuccess=query.exec("select * from mytable;");
if(bSuccess){
while(query.next()){
qDebug()<
}
qDebug()<<:tr>
QMessageBox box;
box.setWindowTitle(QObject::tr("提示"));
box.setIcon(QMessageBox::Information);
box.setText("连接Mysql成功!");
box.setStandardButtons(QMessageBox::Yes|QMessageBox::No);
box.show();
// QMessageBox::about(0,QObject::tr("提示"),QObject::tr("连接Mysql成功!"));
}else
qDebug()<<:tr>
return 0;
}