Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. QSqlError("", "Unable to fetch row", "No query")

QSqlError("", "Unable to fetch row", "No query")

Scheduled Pinned Locked Moved Solved QML and Qt Quick
9 Posts 3 Posters 9.5k Views
  • 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.
  • P Offline
    P Offline
    patcs
    wrote on last edited by
    #1

    Hi, I know that I have an error in commas but I can't find the error. Could someone help me, please?

    This is my code:

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QDebug>

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);

    qDebug()<<"Aplicación inicializada...";
    
    QString nombre;
    nombre.append("baseDeDatos1.sqlite");
    db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName(nombre);
    
    if(db.open()){
        qDebug()<<"Se ha conectado a la base de datos.";
    }else{
        qDebug()<<"ERROR! NO se ha conectado a la base de datos.";
    }
    
    createUserTable();
    

    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }

    void MainWindow::createUserTable()
    {
    QString consultation;
    consultation.append("CREATE TABLE IF NOT EXISTS usuarios("
    "id INTEGER PRIMARY KEY AUTOINCREMENT,"
    "nombre VARCHAR(100),"
    "primer_apellido VARCHAR(100),"
    "segundo_apellido VARCHAR(100),"
    "edad INTEGER NOT NULL"
    ");");

    QSqlQuery create;
    create.prepare(consultation);
    
    if(create.exec())
    {
        qDebug()<<"La tabla USUARIO existe o se ha creado correctamente.";
    
    }else{
        qDebug()<<"La tabla USUARIO NO se ha creado correctamente.";
        qDebug()<<"ERROR!"<<create.lastError();
    
    }
    

    }

    void MainWindow::InsertUser()
    {

    QString consultation;
    consultation.append("INSERT INTO usuarios("
                        "nombre,"
                        "primer_apellido,"
                        "segundo_apellido,"
                        "edad)"
                        "VALUES("
                        "'"+ui->lineEditNombre->text()+"',"
                        "'"+ui->lineEditPrimerApellido->text()+"',"
                        "'"+ui->lineEditSegundoApellido->text()+"',"
                        ""+ui->lineEditEdad->text()+""
                        ");");
    
    QSqlQuery insert;
    insert.prepare(consultation);
    
    if(insert.exec())
    {
        qDebug()<<"El USUARIO se ha insertado correctamente.";
    
    }else{
        qDebug()<<"La USUARIO NO se ha insertado correctamente.";
        qDebug()<<"ERROR!"<<insert.lastError();
    
    }
    

    }

    void MainWindow::on_pushButtonAgregarUsuario_clicked()
    {
    InsertUser();
    }

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      From a quick look you are missing the single quotes in your last parameter of consultation.

      A cleaner solution would be to use a prepared query like described here.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • P Offline
        P Offline
        patcs
        wrote on last edited by patcs
        #3

        Hi @SGaist , thank you for reply, but my last parameter is a INT so it shouldn't have a single quotes. Any way I tried but it doesn't work yet.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Then you should also check what the generated query was. In any case, I still recommend using a prepared query. That will make your code cleaner and clearer.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          1
          • P Offline
            P Offline
            patcs
            wrote on last edited by
            #5

            @SGaist
            How can I check what is the generated query? could you help me? I'm a beginner. Thanks a lot.

            1 Reply Last reply
            0
            • P Offline
              P Offline
              patcs
              wrote on last edited by
              #6

              Please, someone can see my error??? I tried missing everything and it doesn't work.

              J.HilkJ 1 Reply Last reply
              0
              • P patcs

                Please, someone can see my error??? I tried missing everything and it doesn't work.

                J.HilkJ Offline
                J.HilkJ Offline
                J.Hilk
                Moderators
                wrote on last edited by
                #7

                @patcs

                QString consultation;
                consultation.append("CREATE TABLE IF NOT EXISTS usuarios("
                "id INTEGER PRIMARY KEY AUTOINCREMENT,"
                "nombre VARCHAR(100),"
                "primer_apellido VARCHAR(100),"
                "segundo_apellido VARCHAR(100),"
                "edad INTEGER NOT NULL"
                ");");
                

                Last line thats ; should not be there

                QString consultation;
                consultation.append("CREATE TABLE IF NOT EXISTS usuarios("
                "id INTEGER PRIMARY KEY AUTOINCREMENT,"
                "nombre VARCHAR(100),"
                "primer_apellido VARCHAR(100),"
                "segundo_apellido VARCHAR(100),"
                "edad INTEGER NOT NULL"
                ")");
                

                Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                Q: What's that?
                A: It's blue light.
                Q: What does it do?
                A: It turns blue.

                1 Reply Last reply
                2
                • P Offline
                  P Offline
                  patcs
                  wrote on last edited by
                  #8

                  Hi @J.Hilk I changed this, and the error it's the same...

                  1 Reply Last reply
                  0
                  • P Offline
                    P Offline
                    patcs
                    wrote on last edited by patcs
                    #9

                    I could, fix my error, I only have to delete my data base and build again. It works now.

                    1 Reply Last reply
                    1

                    • Login

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