当前位置: 代码迷 >> QT开发 >> 数据库 多个表建立 有关问题
  详细解决方案

数据库 多个表建立 有关问题

热度:58   发布时间:2016-04-25 03:11:14.0
数据库 多个表建立 问题
如下代码:
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
        db.setDatabaseName("test.db");
        if (!db.open()) {
            QMessageBox::critical(0, qApp->tr("Cannot open database"),
                qApp->tr("Unable to establish a database connection.\n"
                         "This example needs SQLite support. Please read "
                         "the Qt SQL driver documentation for information how "
                         "to build it.\n\n"
                         "Click Cancel to exit."), QMessageBox::Cancel);
            return false;
        }
    QSqlQuery sql_query;
          if(sql_query.exec("create table person (id int primary key, hos_name varchar(20))"))
          {
                 qDebug()<<QObject::tr("创建表1成功");
          }
          QString strSQL;
          strSQL=QString("insert into person values(1, '哈哈')");
          qDebug()<<strSQL;
          if(sql_query.exec(strSQL)){
                 qDebug()<<QObject::tr("表1插入数据成功");
          }

          QSqlQuery sql_query1;
          if(sql_query1.exec("create table visit (id int primary key, visit_time varchar(40))"))
          {
                  qDebug()<<QObject::tr("创建表2成功");
           }
              strSQL=QString("insert into visit values(2, '呵呵')");
              qDebug()<<strSQL;
              if(sql_query1.exec(strSQL)){
                      qDebug()<<QObject::tr("表2插入数据成功成功");
              }
     return true;

运行结果是:
创建表1成功
表1插入数据成功
创建表2成功     
然而表2插入确没成功。。。 不知道为什么啊  求大神解释。。。。。。。
------解决思路----------------------
楼主问你个问题啊,我在QT里也想用数据库,数据库是MySQL,QT是Qt5.3,怎么把MySQL与QT关联起来啊?还有链接库是什么?
------解决思路----------------------
在我这里运行没有问题,表2创建成功!
  相关解决方案