Trying to get a Database going but I get an error.



  • So I am doing a database class so that I can easily manage a database I am creating.
    When I test it I get this error: Query exec() error. QSqlError(21, "Unable to execute statement", "out of memory")

    This is my current code:
    @/*
    QSqlDatabase db;
    QSqlQuery query;
    */

    DataBase::DataBase(QString name)
    {
    db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName(name);
    query = QSqlQuery(db);
    }

    bool DataBase::connect(){
    bool ret = false;
    if(!db.isOpen()){
    ret = db.open();
    if(ret == false)
    qDebug() << "Failed to connect." << db.lastError();
    else
    qDebug() << "Successfully connected.";
    return ret;
    }
    return ret;
    }

    void DataBase::disconnect(){
    if(db.isOpen())
    db.close();
    }

    bool DataBase::creatTable(QString instruction){
    bool ret = false;
    if(instruction.contains("create table", Qt::CaseInsensitive)){
    if(db.isOpen()){
    ret = query.exec(instruction);
    if(ret == false)
    qDebug() << "Query exec() error. " << query.lastError();
    else
    qDebug() << "Successful create.";
    }
    }
    else
    qDebug() << "Command does not create table.";

    return ret;
    

    }

    bool DataBase::addToTable(QString instruction){
    bool ret = false;
    if(instruction.contains("insert into", Qt::CaseInsensitive)){
    if(db.isOpen()){
    ret = query.exec(instruction);
    if(ret == false)
    qDebug() << "Query exec() error. " << query.lastError();
    else
    qDebug() << "Successful add.";
    }
    }
    else
    qDebug() << "Command does not insert to table.";

    return ret;
    

    }

    QString DataBase::getQueryResult(QString instruction){
    bool ret = false;
    if(db.isOpen()){
    ret = query.exec(instruction);
    if(ret == false)
    qDebug() << "Query exec() error. " << query.lastError();
    else{
    qDebug() << "Query exec() successful.";
    QString result = query.value(0).toString();
    return result;
    }
    }
    else
    return "NULL";
    }
    @

    This is what I do in main:
    @
    int main()
    {
    DataBase db = DataBase("Test.sql"); //also tried DataBase db = DataBase("Test");
    db.connect();
    db.creatTable("CREATE TABLE names(id INTEGER UNIQUE PRIMARY KEY, firstname VARCHAR(30), lastname VARCHAR(30))");
    db.addToTable("insert into names (id, firstname, lastname) values (1, 'John', 'Doe')");
    db.disconnect();
    return 1;
    }
    @

    So I get the error when I try to create table and of course add to it.



  • use
    @ret = db.exec(instruction);@
    instead of
    @ret = query.exec(instruction);@

    and when you want to get results from a query use
    @//QSqlQuery query;
    query = db.exec(instruction);@

    probably you wont need to use this command anymore:
    @query = QSqlQuery(db);@



  • I have no specific idea about such error, but I can see something strabge with your class. In the constructor you build a query against a closed database connection. Moreover I don't see the point of having several different methods that execute the same query flow just checking before that the query contains something specific to the method behavior.
    Are you sure the error comes from the first createTable and not from the addToTable? Have you tried to create the query inside each method to see if it a problem of how the Query object is created?



  • You setup the query object in your constructor before the database connection is established.

    There is absolutely no need to have an object-global query object if you do not need its result across method borders. Just create one when you need it and destroy it afterwards.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.