#include<QtSql>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QSqlDatabase db=QSqlDatabase::addDatabase("QMYSQL");
db.setUserName("root");
db.setPassword("root");
db.setHostName("localhost");
db.setDatabaseName("xinxi");
QSqlQuery query(db);
query.exec("create table student (id int primary key,name varchar)");
query.exec("insert into student values(1,'xiaogang')");
query.exec("insert into student values(2,'xiaoming')");
query.exec("insert into student values (3,'xiaohong')");
return a.exec();
}
5 个解决方案
#1
你要编译驱动的 ,QT的mysql的驱动是以插件形式加载的 。要自己编译。百度QT mysql应该有很多文章。
#2
已经编译mysql的驱动了,
#include <QtCore/QCoreApplication>
#include<QtSql>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
qDebug()<<"Available drivers:";
QStringList drivers=QSqlDatabase::drivers();
foreach(QString driver ,drivers)
qDebug()<<"\t" << driver;
return a.exec();
}
然后运行出了
但在运行以下代码是就不行了
#include <QtCore/QCoreApplication>
#include<QtSql>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QSqlDatabase db=QSqlDatabase::addDatabase("QMYSQL");
db.setUserName("root");
db.setPassword("root");
db.setHostName("localhost");
db.setDatabaseName("xinxi");
if(db.open()){
qDebug()<<"open\n"<<db.lastError().driverText()<<"\n";
}
else{
qDebug()<<"open faile\n";
}
QSqlQuery query(db);
query.exec("create table student (id int primary key,name varchar)");
query.exec("insert into student values(1,'xiaogang')");
query.exec("insert into student values(2,'xiaoming')");
query.exec("insert into student values (3,'xiaohong')");
return a.exec();
}
#include <QtCore/QCoreApplication>
#include<QtSql>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
qDebug()<<"Available drivers:";
QStringList drivers=QSqlDatabase::drivers();
foreach(QString driver ,drivers)
qDebug()<<"\t" << driver;
return a.exec();
}
然后运行出了
但在运行以下代码是就不行了
#include <QtCore/QCoreApplication>
#include<QtSql>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QSqlDatabase db=QSqlDatabase::addDatabase("QMYSQL");
db.setUserName("root");
db.setPassword("root");
db.setHostName("localhost");
db.setDatabaseName("xinxi");
if(db.open()){
qDebug()<<"open\n"<<db.lastError().driverText()<<"\n";
}
else{
qDebug()<<"open faile\n";
}
QSqlQuery query(db);
query.exec("create table student (id int primary key,name varchar)");
query.exec("insert into student values(1,'xiaogang')");
query.exec("insert into student values(2,'xiaoming')");
query.exec("insert into student values (3,'xiaohong')");
return a.exec();
}
#3
希望各位高人多多指点。
#4
你自己对比一下不就知道了。
#5
你少了db.open
#1
你要编译驱动的 ,QT的mysql的驱动是以插件形式加载的 。要自己编译。百度QT mysql应该有很多文章。
#2
已经编译mysql的驱动了,
#include <QtCore/QCoreApplication>
#include<QtSql>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
qDebug()<<"Available drivers:";
QStringList drivers=QSqlDatabase::drivers();
foreach(QString driver ,drivers)
qDebug()<<"\t" << driver;
return a.exec();
}
然后运行出了
但在运行以下代码是就不行了
#include <QtCore/QCoreApplication>
#include<QtSql>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QSqlDatabase db=QSqlDatabase::addDatabase("QMYSQL");
db.setUserName("root");
db.setPassword("root");
db.setHostName("localhost");
db.setDatabaseName("xinxi");
if(db.open()){
qDebug()<<"open\n"<<db.lastError().driverText()<<"\n";
}
else{
qDebug()<<"open faile\n";
}
QSqlQuery query(db);
query.exec("create table student (id int primary key,name varchar)");
query.exec("insert into student values(1,'xiaogang')");
query.exec("insert into student values(2,'xiaoming')");
query.exec("insert into student values (3,'xiaohong')");
return a.exec();
}
#include <QtCore/QCoreApplication>
#include<QtSql>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
qDebug()<<"Available drivers:";
QStringList drivers=QSqlDatabase::drivers();
foreach(QString driver ,drivers)
qDebug()<<"\t" << driver;
return a.exec();
}
然后运行出了
但在运行以下代码是就不行了
#include <QtCore/QCoreApplication>
#include<QtSql>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QSqlDatabase db=QSqlDatabase::addDatabase("QMYSQL");
db.setUserName("root");
db.setPassword("root");
db.setHostName("localhost");
db.setDatabaseName("xinxi");
if(db.open()){
qDebug()<<"open\n"<<db.lastError().driverText()<<"\n";
}
else{
qDebug()<<"open faile\n";
}
QSqlQuery query(db);
query.exec("create table student (id int primary key,name varchar)");
query.exec("insert into student values(1,'xiaogang')");
query.exec("insert into student values(2,'xiaoming')");
query.exec("insert into student values (3,'xiaohong')");
return a.exec();
}
#3
希望各位高人多多指点。
#4
你自己对比一下不就知道了。
#5
你少了db.open