Results 1 to 7 of 7

Thread: Generate Id's for databases

  1. #1
    Join Date
    Sep 2013
    Posts
    20
    Thanks
    7
    Qt products
    Qt5
    Platforms
    Windows

    Default Generate Id's for databases

    //I have made a database with two tables.

    CREATE TABLE member (\
    id INTEGER PRIMARY KEY,\
    name VARCHAR(40) NOT NULL,\
    gender VARCHAR(40) NOT NULL,\
    dob VARCHAR(40) NOT NULL,\
    personalprivacy VARCHAR(40) NOT NULL)

    CREATE TABLE persinfo (\
    id INTEGER PRIMARY KEY,\
    status VARCHAR(40) NULL,\
    spouse VARCHAR(40) NULL,\
    children VARCHAR(40) NULL,\
    siblings VARCHAR(40) NULL,\
    parents VARCHAR(40) NULL,\
    nameid INTEGER NOT NULL,\
    FOREIGN KEY(nameid) REFERENCES member(id)

    It works fine. I just want to know if there is a way to generate id's with an auto-increment keyword or by any other means. I have made the first table generate id numbers by using this function.

    int generateId(const QString &table)
    {
    QSqlQuery query;
    query.exec("SELECT MAX(id) FROM " + table);
    int id = 0;

    if (query.next())
    id = query.value(0).toInt() + 1;

    return id;
    }

    Like I said it works fine, but it doesn't generate any id's for the persinfo table.

    If anyone can help me please?

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Generate Id's for databases

    Depends on the database. What database are you using ?

  3. #3
    Join Date
    Sep 2013
    Posts
    20
    Thanks
    7
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Generate Id's for databases

    I'm using SQLite

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Generate Id's for databases

    Sqlite FAQ #1: How do I create an AUTOINCREMENT field.

    Your table declaration already has a unique identifier that will be automatically populated.
    To retrieve the id that was assigned during an INSERT use QSqlQuery::lastInsertId() function.

  5. The following user says thank you to ChrisW67 for this useful post:

    LaTj (23rd September 2013)

  6. #5
    Join Date
    Sep 2013
    Posts
    20
    Thanks
    7
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Generate Id's for databases

    Thanks for the advice, and weblink. I found out the real problem and I was wondering if you can help me with this. The problem is that in the function query.next() or QSqlQuery::next() is returning false. It only does this for the member table and not for the persinfo table. Can you help figure out why this happens, i've tried but I cannot figure it out.

    Here is more code on how I set the values for each table.

    int row = nameModel->rowCount();
    nameModel->insertRow(row);

    nameModel->setData(nameModel->index(row, Member_Id), generateId("member"));
    nameModel->setData(nameModel->index(row, Member_Name), nameform->getNameText());
    nameModel->setData(nameModel->index(row, Member_Gender), nameform->getGenderText());
    nameModel->setData(nameModel->index(row, Member_DOB), nameform->getDOBDate());
    nameModel->setData(nameModel->index(row, Member_PersonalInfo), nameform->getPrivacyText());
    nameModel->submitAll();

    ------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    int row = infoModel->rowCount();
    infoModel->insertRow(row);

    infoModel->setData(infoModel->index(row, Info_Id), generateId("persinfo"));
    infoModel->setData(infoModel->index(row, Info_Status), nameform->getStatus());
    infoModel->setData(infoModel->index(row, Info_Spouse), nameform->getSpouseName());
    infoModel->setData(infoModel->index(row, Info_Children), tempChildQString);
    infoModel->setData(infoModel->index(row, Info_Siblings), tempSiblingQString);
    infoModel->setData(infoModel->index(row, Info_Parents), nameform->getParentName1());
    infoModel->submitAll();

  7. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Generate Id's for databases

    As the docs say QSqlQuery::next() returns false if there is no next record. I cannot see what the code you posted has to do with that though.

  8. #7
    Join Date
    Sep 2013
    Posts
    20
    Thanks
    7
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Generate Id's for databases

    I figured out the problem already thanks. As far as the extra code, I just wanted to see if I inserted any data incorrectly, because I was getting false on QSqlQuery::next().
    Don't bother replying it has been resolved. Thanks again.

Similar Threads

  1. Databases?
    By Atomic_Sheep in forum Newbie
    Replies: 2
    Last Post: 7th May 2012, 13:15
  2. Using MySQL databases?
    By bmn in forum Qt Programming
    Replies: 1
    Last Post: 17th December 2010, 19:43
  3. Switching between two databases
    By codeman in forum Qt Programming
    Replies: 20
    Last Post: 13th May 2009, 14:33
  4. Qt and databases
    By gt.beta2 in forum Newbie
    Replies: 6
    Last Post: 18th February 2009, 08:39
  5. dictionary databases
    By rishiraj in forum Newbie
    Replies: 1
    Last Post: 5th January 2009, 08:30

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.