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. Trouble with QSqlRecord class
Qt 6.11 is out! See what's new in the release blog

Trouble with QSqlRecord class

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 1.8k 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
    daGenie
    wrote on last edited by
    #1

    I am creating a database with SQLite and I am trying to insert information with the QSqlRecord class but it doesn't work at all. I created a class for the database called "DatabaseManager". This class contains a QSqlDatabase object which I manipulate to create my table. I successfully create the database using QSqlQuery:

    bool DatabaseManager::createNamesTable() {

    bool ret = false;
    QSqlQuery query;

    // db is the database object
    if(db.isOpen()) {
    ret = query.exec("CREATE TABLE names ("
    "id INTEGER,"
    "firstname VARCHAR(30),"
    "lastname VARCHAR(30)"
    ")");
    }

    return ret;
    

    }

    Unfortunately, when I try to use the QSqlRecord class, nothing happens. It doesn't even give an error. This is the definition in the .cpp file of DatabaseManager.

    void DatabaseManager::insertField(const QString &tableName, int pos, const QString &fieldName,
    QVariant::Type type) {
    db.record(tableName).insert(pos, QSqlField(fieldName, type));
    }

    And in the main(), I write:

    int main(int argc, char** argv)
    {
    QCoreApplication app(argc, argv);

    DatabaseManager db("New.db.sqlite");
    

    if( db.createNamesTable()) {
    db.insertField("names", 3, "places", QVariant::String); // here, i try to add a column called "places"
    qDebug() << "Does names contain places? " << db.contains("names", "places");
    }

    app.exec();
    

    }

    I'm completely confused on what I'm doing wrong! Any form of help would be appreciated!

    1 Reply Last reply
    0
    • V Offline
      V Offline
      vittalonline
      wrote on last edited by
      #2

      hi daGenie,

      I think you missed this statement.

      QSqlDatabase db = QSqlDatabase::addDatabase( "QSQLITE" );

      This statement helps to get the already existed database connection object.

      1 Reply Last reply
      0
      • D Offline
        D Offline
        daGenie
        wrote on last edited by
        #3

        [quote author="Vittal" date="1373873277"]hi daGenie,

        I think you missed this statement.

        QSqlDatabase db = QSqlDatabase::addDatabase( "QSQLITE" );

        This statement helps to get the already existed database connection object.[/quote]

        Thanks for the reply.

        That statement is in the constructor of the DatabaseManager class but unfortunately, it still doesn't work.

        1 Reply Last reply
        0
        • V Offline
          V Offline
          vittalonline
          wrote on last edited by
          #4

          ok friend ,

          send error report.Database connection management is easiest programming in Qt ,so its not a big problem.I always help u.

          1 Reply Last reply
          0
          • D Offline
            D Offline
            daGenie
            wrote on last edited by
            #5

            [quote author="Vittal" date="1373880559"]ok friend ,

            send error report.Database connection management is easiest programming in Qt ,so its not a big problem.I always help u.[/quote]

            Do you mean I should call the lastError().text() function and send the results? If that is the case, I have already tried that, and it returns an empty string which means "no error" I suppose.

            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