Results 1 to 4 of 4

Thread: Using auto_increment in QSql

  1. #1
    Join Date
    May 2010
    Location
    Rzeszow/Poland
    Posts
    17
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Using auto_increment in QSql

    I have working database and I want to use auto_increment for id's in my tables.

    Qt Code:
    1. query.exec("create table przedmioty(id int auto_increment not null primary key, nazwa varchar(40), skrot varchar(10))");
    To copy to clipboard, switch view to plain text mode 
    creates table, but:
    Qt Code:
    1. query.exec("insert into przedmioty(skrot, nazwa) values('Matematyka', 'mat')");
    To copy to clipboard, switch view to plain text mode 
    throws QSqlError(19, "Unable to fetch row", "constraint failed") .

    I want to mention that without "auto_increment" my program adds records.

    What to do?

  2. #2
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Using auto_increment in QSql

    Assuming that you are still using SQLite, I found this by Googling "SQLite autoincrement".

  3. #3
    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: Using auto_increment in QSql

    If it is SQLite (it looks like MySQL) then the syntax for the auto key is:
    Qt Code:
    1. query.exec("create table przedmioty(id integer primary key, nazwa varchar(40), skrot varchar(10))");
    To copy to clipboard, switch view to plain text mode 
    or
    Qt Code:
    1. query.exec("create table przedmioty(id integer primary key autoincrement, nazwa varchar(40), skrot varchar(10))");
    To copy to clipboard, switch view to plain text mode 
    SQLite is very particular about the form of the id column declaration.

    This is what Sqlite makes of these options:
    Qt Code:
    1. SQLite version 3.6.23.1
    2. Enter ".help" for instructions
    3. Enter SQL statements terminated with a ";"
    4. sqlite> create table przedmioty(id int auto_increment not null primary key, nazwa varchar(40), skrot varchar(10));
    5. sqlite> insert into przedmioty (skrot, nazwa) values ('test', 'test');
    6. Error: przedmioty.id may not be NULL
    7. sqlite> drop table przedmioty;
    8.  
    9. sqlite> create table przedmioty(id integer primary key, nazwa varchar(40), skrot varchar(10));
    10. sqlite> insert into przedmioty (skrot, nazwa) values ('test', 'test');
    11. sqlite> insert into przedmioty (skrot, nazwa) values ('test', 'test');
    12. sqlite> select * from przedmioty;
    13. 1|test|test
    14. 2|test|test
    15. sqlite> drop table przedmioty;
    16.  
    17. sqlite> create table przedmioty(id integer primary key autoincrement, nazwa varchar(40), skrot varchar(10));
    18. sqlite> insert into przedmioty (skrot, nazwa) values ('test', 'test');
    19. sqlite> insert into przedmioty (skrot, nazwa) values ('test', 'test');
    20. sqlite> select * from przedmioty;
    21. 1|test|test
    22. 2|test|test
    23. sqlite>
    To copy to clipboard, switch view to plain text mode 

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

    SykeS (21st May 2010)

  5. #4
    Join Date
    May 2010
    Location
    Rzeszow/Poland
    Posts
    17
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Using auto_increment in QSql

    Thanks ChrisW67. Your code helped me a lot. Before posting a topic I found "autoincrement" in SQLite documentation, but I didn't know that "autoincrement" must be inserted after "primary key", and this column must be "integer" (I used "int"). Now everything works fine.

    See you.

Similar Threads

  1. Data file and QSql
    By OverTheOCean in forum Qt Programming
    Replies: 3
    Last Post: 30th April 2010, 23:04
  2. QSql Database
    By addu in forum Qt Programming
    Replies: 6
    Last Post: 20th July 2009, 13:37
  3. QSql error
    By renjithmamman in forum Qt Programming
    Replies: 4
    Last Post: 20th December 2006, 10:28
  4. QSqlField discovery auto_increment int value
    By patrik08 in forum Qt Programming
    Replies: 6
    Last Post: 3rd December 2006, 11:19
  5. Cannot use QSql drivers
    By xgoan in forum Newbie
    Replies: 2
    Last Post: 25th August 2006, 15:45

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.