Fedora下QT4.8连接MySQL

#include<QtGui>
#include<QtCore>
#include<QtSql>
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()<<QObject::tr("DB Connection Fail!");
      return -1;
   }
   QSqlQuery query;
   bool bSuccess=query.exec("select * from mytable;");
   if(bSuccess){
       while(query.next()){
          qDebug()<<query.value(0).toString()<<query.value(1).toInt()<<endl;
       }
      qDebug()<<QObject::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()<<QObject::tr("连接Mysql失败!");

   return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值