参看之前的文字,在qt += sql 写了一个例子如下:
#include <QtCore/QCoreApplication>
 #include<Qt/QtSql>
 #include <QtCore/QStringList>
 
 int main(int argc, char *argv[])
 {
  
     QCoreApplication a(argc, argv);
 
     QSqlQuery query;
       QSqlDatabase db=QSqlDatabase::addDatabase("QMYSQL");
 
       db.setHostName("localhost");
 
       db.setDatabaseName("test");// just two : test or mysql
 
       db.setUserName("root");
 
       db.setPassword("shmily");
 
       if(!db.open()){
  
 
       qDebug()<<"Unable to open database";
 
       }else{
  
 
       qDebug()<<"Database connection established";
         QStringList qlist = db.tables();
          qDebug()<<qlist;
          db.exec("create table ran();");
     //     QSqlQuery query("show tables",db);
          query = db.exec("show tables;");
          while(query.next())
          {
  
          QString id = query.value(0).toString();
           qDebug()<<id;
           }
          //   qlist = db.tables();
        //   qDebug()<<qlist;
          db.close();
       }
 
       return a.exec();
 }
 现在还没有实现对数据库test添加数据表,明天在弄了,还要早起
 昨天自己好傻,刚才一看就写了个测试就过了。
         db.exec("create table student ( \
                  id int(3) auto_increment not null primary key,\
                  name char(10) not null, \
                  address varchar(50) default'chengdu',\
                  year date );");
             qlist = db.tables();
           qDebug()<<qlist;
 
 无语中。。。。
打印如下:
"student"
"teacher"