fedora15 qt操作mysql

參看之前的文字,在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"



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章