Solved QSqlError("", "Unable to fetch row", "No query")
-
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();
} -
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.
-
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.
-
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.
-
@SGaist
How can I check what is the generated query? could you help me? I'm a beginner. Thanks a lot. -
Please, someone can see my error??? I tried missing everything and it doesn't work.
-
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 thereQString 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" ")");
-
Hi @J.Hilk I changed this, and the error it's the same...
-
I could, fix my error, I only have to delete my data base and build again. It works now.