SQL on MAC
Unsolved
General and Desktop
-
I try to use sql with Qt, however, my code get the following errors:
Available drivers: "QSQLITE" "QMYSQL" "QMYSQL3" "QODBC" "QODBC3" "QPSQL" "QPSQL7" QSqlDatabase: QMYSQL driver not loaded QSqlDatabase: available drivers: QSQLITE QMYSQL QMYSQL3 QODBC QODBC3 QPSQL QPSQL7
Here is my original code:
#include <QCoreApplication> #include <QSqlDatabase> #include <QDebug> #include <QStringList> #include <QSqlQuery> int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); //List all available databases qDebug() << "Available drivers:"; QStringList drivers = QSqlDatabase::drivers(); for(auto t_s: drivers) { qDebug() << t_s; } // open mySql QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL"); db.setHostName("localhost"); db.setDatabaseName("mydata"); db.setUserName("root"); db.setPassword(""); if(!db.open()) { qDebug() << "Failed to connect to root mysql admin"; } else { qDebug() << "open"; } QSqlQuery query(db); //varchar should has lenth query.exec("create table student(id int primary key, name varchar(20))"); query.exec("insert into student values(1,'xiaogang')"); query.exec("insert into student values(2,'xiaoming')"); query.exec("insert into student values(3,'xiaohong')"); query.exec("select id, name from student where id >= 2"); while(query.next()) { int value0 = query.value(0).toInt(); QString value1 = query.value(1).toString(); qDebug() << value0 << value1; } return a.exec(); }
Did I miss something? thank you!
-
Hi,
- Which version of MySQL are you using ?
- Where is it installed ?
-
Ok, then the plugin can't find its dependencies.
You can use
install_name_tool
on it to point it to your MySQL installation or rebuild the plugin following the doc recommendations.