• 9352阅读
  • 3回复

【提问】在QT中怎么连接MySQL4.1 ?谢谢,急等 [复制链接]

上一主题 下一主题
离线icyheart98
 

只看楼主 倒序阅读 楼主  发表于: 2005-09-19
— 本帖被 XChinux 从 General Qt Programming 移动到本区(2011-01-02) —
我在Win2000下QT3.2.3的例程中连接MySQL时,编译可以,执行时报错“driver not loaded”,请各各位大侠帮忙解决,谢谢!我不知道去那里安装MySQL的驱动

程序如下:
#include <qapplication.h>
#include <qsqldatabase.h>
#include <qdatatable.h>
#include <qsqlcursor.h>
#include <qmessagebox.h>

/* Modify the following to match your environment */
/*QODBC3 (ODBC),
QOCI8 (Oracle),
QTDS7 (Sybase Adaptive Server),
QPSQL7 (PostgreSQL),
QMYSQL4 (MySQL),
and QDB2 (IBM DB2). */

#define DRIVER     "QMYSQL3" /* see the Qt SQL documentation for a list of available drivers */
#define DATABASE   "liming" /* the name of your database */
#define USER       "liming"   /* user name with appropriate rights */
#define PASSWORD   "1978928"   /* password for USER */
#define HOST       "localhost" /* host on which the database is running */

class SimpleCursor : public QSqlCursor
{
public:
  SimpleCursor () : QSqlCursor( "simpletable" ) {}
protected:
  QSqlRecord* primeInsert()
  {
        /* a real-world application would use sequences, or the like */
        QSqlRecord* buf = QSqlCursor::primeInsert();
        QSqlQuery q( "select max(id)+1 from simpletable;" );
        if ( q.next() )
              buf->setValue( "id", q.value(0) );
        return buf;
  }
};

int main( int argc, char ** argv )
{
  QApplication a( argc, argv );

  QSqlDatabase *db = QSqlDatabase::addDatabase( DRIVER );
  db->setDatabaseName( DATABASE );
  db->setUserName( USER );
  db->setPassword( PASSWORD );
  db->setHostName( HOST );

  if( !db->open() )
    {
        db->lastError().showMessage( "An error occured. Please read the README file in the sqltable"
              "dir for more information.\n\n" );
        return 1;
  }

  SimpleCursor cursor;

  QDataTable table( &cursor ); /* data table uses our cursor */
  table.addColumn( "name", "Name" );
  table.addColumn( "address", "Address" );
  table.setSorting( TRUE );

  a.setMainWidget( &table );
  table.refresh(); /* load data */
  table.show();   /* show widget */

  return a.exec();
}
[ 此贴被XChinux在2005-09-19 14:56重新编辑 ]
离线XChinux

只看该作者 1楼 发表于: 2005-09-19
MySQL驱动默认的是未编译的,需要自己编译。
可以使用myODBC来连接MySQL
二笔 openSUSE Vim N9 BB10 XChinux@163.com 网易博客 腾讯微博
承接C++/Qt、Qt UI界面、PHP及预算报销系统开发业务
离线guhuo
只看该作者 2楼 发表于: 2005-10-05
在linux下 怎么连接?
嵌入式linux嘿嘿
qq:
15416920
离线kytexzy

只看该作者 3楼 发表于: 2005-10-06
按Assistant中的例子编译之
try to find forgiveness for yourself and forgive others
快速回复
限100 字节
 
上一个 下一个