Could anyone tell me why I cant execute the SQL query?
- #include <QtCore/QCoreApplication>
- #include <QtSql/QSqlDatabase>
- #include <QtSql>
- #include <QStringList>
- #include <QDebug>
- struct Student
- {
- QString id;
- QString name;
- QString sex;
- };
- int main(int argc, char *argv[])
- {
- db.setDatabaseName(dsn);
- db.setUserName("admin");
- db.setPassword("123456");
- if(db.open()) {
- qDebug()<<"OK!";
- }
- else{
- qDebug()<<"Error!";
- }
- query.exec("select * from NewStudent");
- qDebug()<<error.number();
- if(!query.isValid()){
- qDebug()<<"Invalid!";
- }
- while(query.next()){
- Student student;
- student.id=query.value(0).toString();
- student.name=query.value(1).toString();
- student.sex=query.value(2).toString();
- qDebug()<<"ID: "<<student.id<<" Name: "<<student.name<<" Sex: "<<student.sex;
- }
- return a.exec();
- }
the output of qDebug()<<error.number();
is 16945,and Invalid is printed
1 reply
You must log in to post a reply. Not a member yet? Register here!


