#include <QApplication>
#include <QtSql>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
qDebug()<<"available drivers:";
QStringList drivers = QSqlDatabase::drivers();
foreach(QString driver, drivers)
qDebug()<<driver;
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("localhost");
db.setPort(3306);
db.setDatabaseName("hzq");
db.setUserName("root");
db.setPassword("123456");
if(!db.open())
qDebug()<<"failed to connect to mysql";
else
qDebug()<<"success";
QString select_all_sql = "select * from teacher_class";
//查询所有数据
QSqlQuery sql_query;
sql_query.prepare(select_all_sql);
if(!sql_query.exec())
{
qDebug()<<sql_query.lastError();
}
else
{
while(sql_query.next())
{
int id = sql_query.value(0).toInt();
QString templ_name = sql_query.value(2).toString();
qDebug()<<QString("Id:%1 Templ Name:%2").arg(id).arg(templ_name);
}
}
return a.exec();
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/u011735736/article/details/47339645