Results 1 to 7 of 7

Thread: QSQLITE problem with FOREIGN KEY

  1. #1
    Join Date
    Apr 2011
    Posts
    3
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default QSQLITE problem with FOREIGN KEY

    hi everybody, i have a problem, when i compile such a code:

    query.exec("CREATE TABLE IF NOT EXISTS ProductTypeGroup(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT)");
    query.exec("CREATE TABLE IF NOT EXISTS ProductType(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, low_level INTEGER )");

    everything is ok, the table called ProductType will be created

    but when i compile such a code:

    query.exec("PRAGMA foreign_keys = ON;");
    query.exec("CREATE TABLE IF NOT EXISTS ProductTypeGroup(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT)");
    query.exec("CREATE TABLE IF NOT EXISTS ProductType(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, low_level INTEGER , FOREIGN KEY (group) REFERENCES ProductTypeGroup(id)");

    i don't have any errors but table called ProductType won't be created

    Do You know what can be a problem??
    Last edited by kamilus; 24th April 2011 at 22:19.

  2. #2
    Join Date
    Jan 2011
    Posts
    14
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSQLITE problem with FOREIGN KEY

    Quote Originally Posted by kamilus View Post
    hi everybody, i have a problem, when i compile such a code:

    query.exec("CREATE TABLE IF NOT EXISTS ProductTypeGroup(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT)");
    query.exec("CREATE TABLE IF NOT EXISTS ProductType(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, low_level INTEGER )");

    everything is ok, the table called ProductType will be created

    but when i compile such a code:

    query.exec("PRAGMA foreign_keys = ON;");
    query.exec("CREATE TABLE IF NOT EXISTS ProductTypeGroup(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT)");
    query.exec("CREATE TABLE IF NOT EXISTS ProductType(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, low_level INTEGER ), FOREIGN KEY (group) REFERENCES ProductTypeGroup(id)");

    i don't have any errors but table called ProductType won't be created

    Do You know what can be a problem??
    You have certainly errors, test the return value of QSqlQuery::exec
    You foreign key is bad (not exists).

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

    kamilus (25th April 2011)

  4. #3
    Join Date
    Apr 2011
    Posts
    3
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QSQLITE problem with FOREIGN KEY

    yes You' re right the first query.exec return true and the second (this with FOREIGN KEY) return false, but the whole project is compiling. Have you got any idea what i do wrong?

  5. #4
    Join Date
    Apr 2011
    Posts
    124
    Thanks
    1
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows Symbian S60

    Default Re: QSQLITE problem with FOREIGN KEY

    The fact that it compiles proves nothing. You could have utter gibberish in the query strings and it would still compile without error.

    First off, you need to develop the habit of checking every return code from SQL, even if just with an assert.

    Then, if you have trouble with a given SQL statement and the cause isn't immediately obvious, the thing to do is to use the sqlite3 command line tool to test the statement and slowly add/remove/change things until you find your error.

    In your case, it appears that you have specified a child key that does not exist.

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

    kamilus (25th April 2011)

  7. #5
    Join Date
    Apr 2011
    Posts
    3
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QSQLITE problem with FOREIGN KEY

    now i see the problem, thank you for help

  8. #6
    Join Date
    Apr 2011
    Posts
    1
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QSQLITE problem with FOREIGN KEY

    Hello

    Try running the query in this way.

    "CREATE TABLE IF NOT EXISTS ProductType(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, low_level INTEGER , FOREIGN KEY (group) REFERENCES ProductTypeGroup(id))"

    att

    Diego Oliveira

  9. #7
    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: QSQLITE problem with FOREIGN KEY

    Here are the problems I can see in theProductType create table:
    • A stray closing parentheses
    • The column 'group', used in the foreign key constraint, does not exist in table ProductType
    • The name 'group' is a reserved word anyway


    Here's a working one with the foreign key as a table constraint:
    Qt Code:
    1. CREATE TABLE ProductType(
    2. id INTEGER PRIMARY KEY AUTOINCREMENT,
    3. name TEXT,
    4. low_level INTEGER,
    5. prod_group INTEGER NOT NULL,
    6. FOREIGN KEY (prod_group) REFERENCES ProductTypeGroup(id)
    7. );
    To copy to clipboard, switch view to plain text mode 
    or as a column constraint:
    Qt Code:
    1. CREATE TABLE ProductType(
    2. id INTEGER PRIMARY KEY AUTOINCREMENT,
    3. name TEXT,
    4. low_level INTEGER,
    5. prod_group INTEGER NOT NULL REFERENCES ProductTypeGroup(id)
    6. );
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Problem deploying qt application using qsqlite on Windows XP/7
    By anoraxis in forum Installation and Deployment
    Replies: 4
    Last Post: 8th April 2011, 13:18
  2. Foreign key ON
    By fantom in forum Qt Programming
    Replies: 0
    Last Post: 2nd March 2011, 10:32
  3. Replies: 1
    Last Post: 7th July 2008, 20:13
  4. FOREIGN KEY + QComboBox
    By eleanor in forum Qt Programming
    Replies: 1
    Last Post: 8th November 2007, 10:37
  5. QSqlite problem
    By dragon in forum Qt Programming
    Replies: 2
    Last Post: 11th April 2007, 02:02

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.