Results 1 to 7 of 7

Thread: SQLite with Qt4

  1. #1
    Join Date
    Aug 2009
    Location
    Saudi Arabia - Buraidah
    Posts
    48
    Thanks
    42
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default SQLite with Qt4

    Hi,

    I have tried to work with SQlite I have created a connection, a database and a table. Everything seems ready but when I tried to add some records using QSqlDatabase::exec() I faild because I need to provide the primary key for each column I have to add.

    If this is my table:
    Qt Code:
    1. Query = "CREATE TABLE `persons` ("
    2. "`id` INT UNSIGNED AUTO_INCREMENT PRIMARY KEY NOT NULL,"
    3. "`name` VARCHAR( 255 ) NOT NULL)";
    To copy to clipboard, switch view to plain text mode 

    And this is my Query for records to be added from a LineEdit object:

    Qt Code:
    1. QString submitQuery = "INSERT INTO `persons` (`id`, `name`) VALUES (NULL, '" + personName + "')";
    To copy to clipboard, switch view to plain text mode 

    Is there any way to make the `id field filled automatically just like with PHP. It should be increased by one every time I added a record.

    Thank you,
    Mohammad AlHobayyeb

  2. #2
    Join Date
    Nov 2008
    Location
    Częstochowa/Poland
    Posts
    50
    Thanks
    2
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: SQLite with Qt4

    Try using the prepare and bindValue methods of QSqlQuery.
    Qt Code:
    1. qry.prepare("SELECT Minia, Format FROM Dane where Id=:id");
    2. qry.bindValue(":id",id);
    To copy to clipboard, switch view to plain text mode 
    sorry wrong answer.
    I can't think of anything that would preform the auto incrementation You would like to achieve. I always end up writing it by my self selecting the maximal value and incrementing it manually..
    Last edited by Grimlock; 4th September 2009 at 16:25.

  3. The following user says thank you to Grimlock for this useful post:

    MIH1406 (3rd June 2010)

  4. #3
    Join Date
    Aug 2009
    Location
    Saudi Arabia - Buraidah
    Posts
    48
    Thanks
    42
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: SQLite with Qt4

    No problem how to get the maximal value in a table?

    Thanks

  5. #4
    Join Date
    Nov 2008
    Location
    Częstochowa/Poland
    Posts
    50
    Thanks
    2
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: SQLite with Qt4

    Qt Code:
    1. Select max(id) form TableName
    To copy to clipboard, switch view to plain text mode 
    Should do the trick.

    Or You may try.
    Qt Code:
    1. qry.prepare("SELECT Id FROM TableName Order by Id");
    2. if(!qry.exec())
    3. {
    4. qFatal("Failed to count records");
    5. return;
    6. }
    7. while(qry.next())
    8. {
    9. int index = qry.value(0).toInt();
    10. if(temp < index)
    11. break;
    12. else
    13. ++temp;
    14. }
    To copy to clipboard, switch view to plain text mode 
    This will get you the first free id.If by any chance You would get some free ids in between due to dropping a row.

  6. The following user says thank you to Grimlock for this useful post:

    MIH1406 (3rd June 2010)

  7. #5
    Join Date
    May 2008
    Location
    Kyiv, Ukraine
    Posts
    418
    Thanks
    1
    Thanked 29 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: SQLite with Qt4

    The doc on SQLite says that the keyword is AUTOINCREMENT, not AUTO_INCREMENT. This should take care of your primary key when you insert row without specifying the id.
    I'm a rebel in the S.D.G.

  8. The following user says thank you to lyuts for this useful post:

    MIH1406 (3rd June 2010)

  9. #6
    Join Date
    Aug 2009
    Location
    Saudi Arabia - Buraidah
    Posts
    48
    Thanks
    42
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: SQLite with Qt4

    That is a very good point. But where is the SQLite docs?

    Thank you
    Mohammad

  10. #7
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: SQLite with Qt4

    Quote Originally Posted by MIH1406 View Post
    But where is the SQLite docs?
    www.sqlite.org?

  11. The following user says thank you to Lykurg for this useful post:

    MIH1406 (3rd June 2010)

Similar Threads

  1. access SQlite from Qt (Windows & Linux)
    By Qt Coder in forum Qt Programming
    Replies: 13
    Last Post: 22nd July 2009, 10:07
  2. steps to connect to SQLite from Q
    By Qt Coder in forum Qt Programming
    Replies: 3
    Last Post: 8th July 2009, 12:12
  3. How to Run Sqlite in Windows Mobile
    By Comptrol in forum Qt Programming
    Replies: 0
    Last Post: 26th June 2009, 12:19
  4. Qt SQLite user functions
    By cevou in forum Qt Programming
    Replies: 1
    Last Post: 10th March 2009, 19:43
  5. sqlite version in Qt
    By janus in forum Newbie
    Replies: 4
    Last Post: 5th February 2009, 14:18

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.