Mac上的MySQL for Qt [英] MySQL for Qt on Mac

查看:401
本文介绍了Mac上的MySQL for Qt的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我花了大约6个小时来尝试按照网络上的各种说明让MySQL与Qt一起使用.我现在要切断手腕!

I have wasted around 6 hours trying to get MySQL working with Qt following all sorts of instructions from the web. I want to cut my wrist off now!

有人对如何将QMYSQL驱动程序安装到Qt进行了简单而冗长的解释吗?

Does anyone have a simple and a verbose explanation of how to install QMYSQL driver into Qt?

我有Mac 10.6,并且是初学者n00b.

I have Mac 10.6 and I am a beginner n00b.

您的帮助将由衷地感谢!

Your help will be appreciated from the bottom of my heart!

Sana.

我在执行grep时会得到以下文件,所以在这些文件中,我只是将libqsqlmysql.dylib复制到了所有文件夹中,但是仍然无法编译...我得到一个错误,说QSqlDatabase: QMYSQL driver not loaded

I get the following files when I do the grep, so among these just for kicks I copied libqsqlmysql.dylib into all of the folders, but still I don't get to compile... I get an error saying that QSqlDatabase: QMYSQL driver not loaded

/Library/Application Support/DivX/QtPlugins/sqldrivers/libqsqlite.dylib 
/Users/pfn368/QtSDK/Assistant.app/Contents/PlugIns/sqldrivers/libqsqlite.dylib
/Users/pfn368/QtSDK/Designer.app/Contents/PlugIns/sqldrivers/libqsqlite.dylib
/Users/pfn368/QtSDK/Desktop/Qt/4.8.0/gcc/plugins/sqldrivers/libqsqlite.dylib
/Users/pfn368/QtSDK/Desktop/Qt/4.8.0/gcc/plugins/sqldrivers/libqsqlite_debug.dylib
/Users/pfn368/QtSDK/Desktop/Qt/474/gcc/plugins/sqldrivers/libqsqlite.dylib
/Users/pfn368/QtSDK/Desktop/Qt/474/gcc/plugins/sqldrivers/libqsqlite_debug.dylib
/Users/pfn368/QtSDK/Madde/sysroots/harmattan-arm-sysroot/usr/lib/qt4/plugins/sqldrivers/libqsqlite.so
/Users/pfn368/QtSDK/Madde/sysroots/harmattan-nokia-arm-sysroot/usr/lib/qt4/plugins/sqldrivers/libqsqlite.so
/Users/pfn368/QtSDK/Maemo/4.6.2/sysroots/fremantle-arm-sysroot-20.2010.36-2-slim/usr/lib/qt4/plugins/sqldrivers/libqsqlite.so
/Users/pfn368/QtSDK/Qt Creator.app/Contents/MacOS/qmlpuppet.app/Contents/PlugIns/sqldrivers/libqsqlite.dylib
/Users/pfn368/QtSDK/Qt Creator.app/Contents/PlugIns/sqldrivers/libqsqlite.dylib
/Users/pfn368/QtSDK/QtSources/4.8.0/plugins/sqldrivers/libqsqlite.dylib
/Users/pfn368/QtSDK/QtSources/4.8.0/plugins/sqldrivers/libqsqlite_debug.dylib
/Users/pfn368/QtSDK/Simulator/Application/simulator.app/Contents/PlugIns/sqldrivers/libqsqlite.dylib
/Users/pfn368/QtSDK/Simulator/Qt/gcc/plugins/sqldrivers/libqsqlite.dylib
/Users/pfn368/QtSDK/Simulator/Qt/gcc/plugins/sqldrivers/libqsqlite_debug.dylib

这是我的.pro文件

QT       += sql core gui\
           network

TARGET = mini-stock-exchange
TEMPLATE = app

SOURCES += ./src/main.cpp\
        ./src/mainwindow.cpp

HEADERS  += ./header/mainwindow.h

FORMS    += ./ui/mainwindow.ui

我的包含

#include "./header/mainwindow.h"
#include "ui_mainwindow.h"
#include <QtSql/QSqlDatabase>
#include <QtSql/QSqlQuery>
#include <QFile>
#include <QtSql/QSqlError>
#include <qsqldatabase.h>
#include <QtCore>
#include <QtSql>

调用数据库的代码

QSqlDatabase defaultDB = QSqlDatabase::addDatabase("QMYSQL3");
if ( !defaultDB.isValid() ) {
    qWarning( "Failed to connect to the database driver" );
}
defaultDB.setDatabaseName( "nicu" );
defaultDB.setUserName( "root" );
defaultDB.setPassword( "root" );
defaultDB.setHostName( "http://localhost:8889" );

推荐答案

Mac Homebrew的Qt 4软件包提供了将mysql-drivers作为Qt插件默认安装的选项(这不是默认选项,这就是为什么您缺少的原因这个).

The Qt 4 packages from Mac Homebrew have an option to install mysql-drivers as a Qt Plugin for default (it's not a default option, that's why you are missing this).

按以下说明下载并安装Mac OS X Homebrew软件: http://brew.sh .

Download and install Mac OS X Homebrew software as described here: http://brew.sh.

安装自制软件后,删除以前的Qt4安装.

After installing homebrew, remove the previously Qt4 installation.

如果您是使用brew安装的,只需在终端上键入:

If you have installed it using brew just type on the terminal:

$ brew remove qt4

要在mysql支持下安装它,请运行:

To install it with mysql support run:

$ brew install qt4 --with-mysql

每次您需要在brew中安装带有某些选项的软件包,但您不知道所支持的选项时,只需键入:

And everytime you need to install a package with some options in brew but you don't know the supported options just type:

$ brew options FORMULA_NAME

它将显示给定公式的所有可用构建选项.

And it will show all the build options available for the given formula.

这篇关于Mac上的MySQL for Qt的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆