Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Trying to get a Database going but I get an error.
Qt 6.12 LTS is out! See what's new in the release blog

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

Scheduled Pinned Locked Moved General and Desktop
4 Posts 4 Posters 5.5k 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mohsen
      wrote on last edited by
      #2

      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);@

      1 Reply Last reply
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

        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?

        1 Reply Last reply
        0
        • ? Offline
          ? Offline
          A Former User
          wrote on last edited by
          #4

          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.

          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
          • Unsolved