先是控制台连接,稍后转GUI
#include <QCoreApplication>
#include<QtSql>
#include<QDebug>
#include<QString>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("E:\\Projects\\sqlite\\test.db");
if(db.open())
{
qDebug()<<"opened!!\n";
}
else
{
qDebug()<<"open failed\n";
}
QSqlQuery qry;
qry.exec("select * from Course;");
while(qry.next())
{
int id=qry.value(0).toInt();
QString name=qry.value(1).toString();
qDebug()<<id<<name<<endl;
}
return a.exec();
}