Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Could anyone tell me why I cant execute the SQL query?
Forum Updated to NodeBB v4.3 + New Features

Could anyone tell me why I cant execute the SQL query?

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 1.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    DrizzleX
    wrote on last edited by
    #1

    @

    #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[])
    {
    QCoreApplication a(argc, argv);

    QSqlDatabase db=QSqlDatabase::addDatabase("QODBC");
    
    
    QString dsn = QString::fromLocal8Bit("DRIVER={SQL SERVER};SERVER=127.0.0.1;DATABASE=mydb");
    db.setDatabaseName(dsn);
    db.setUserName("admin");
    db.setPassword("123456");
    if(db.open()) {
        qDebug()<<"OK!";
    }
    else{
        qDebug()<<"Error!";
    }
    
    
    QSqlQuery query(db);
    query.exec&#40;"select * from NewStudent"&#41;;
    
    QSqlError error=query.lastError();
    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&#40;&#41;;
    

    }
    @
    the output of qDebug()<<error.number();
    is 16945,and Invalid is printed

    1 Reply Last reply
    0
    • Q Offline
      Q Offline
      qxoz
      wrote on last edited by
      #2

      Where do you get the QODBC driver(how you build and from which source)?

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved