Results 1 to 6 of 6

Thread: SQLite Woes

  1. #1

    Default SQLite Woes

    Hello ive been trying to work out all day long why my sql lite database was not accepting insert queries until i finally found the problem:

    basically using this pyqt code:
    Qt Code:
    1. query.exec_("""CREATE TABLE calls (
    2. id INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE NOT NULL,
    3. caller VARCHAR(40) NOT NULL,
    4. starttime DATETIME NOT NULL,
    5. endtime DATETIME NOT NULL,
    6. random_new_field VARCHAR(80) NOT NULL,
    7. topic VARCHAR(80) NOT NULL)""")
    8.  
    9. topics = ("Complaint", "Information request", "Off topic",
    10. "Information supplied", "Complaint", "Complaint")
    11.  
    12. now = QDateTime.currentDateTime()
    13. print "Populating table..."
    14. query.prepare("INSERT INTO calls (caller, starttime, endtime, "
    15. "topic) VALUES (?, ?, ?, ?)")
    16. for name in ('Joshan Cockerall', 'Ammanie Ingham',
    17. 'Diarmuid Bettington', 'Juliana Bannister',
    18. 'Oakley-Jay Buxton', 'Reilley Collinge',
    19. 'Ellis-James Mcgehee', 'Jazmin Lawton',
    20. 'Lily-Grace Smythe', 'Coskun Lant', 'Lauran Lanham',
    21. 'Millar Poindexter', 'Naqeeb Neild', 'Maxlee Stoddart',
    22. 'Rebia Luscombe', 'Briana Christine', 'Charli Pease',
    23. 'Deena Mais', 'Havia Huffman', 'Ethan Davie',
    24. 'Thomas-Jack Silver', 'Harpret Bray', 'Leigh-Ann Goodliff',
    25. 'Seoras Bayes', 'Jenna Underhill', 'Veena Helps',
    26. 'Mahad Mcintosh', 'Allie Hazlehurst', 'Aoife Warrington',
    27. 'Cameron Burton', 'Yildirim Ahlberg', 'Alissa Clayton',
    28. 'Josephine Weber', 'Fiore Govan', 'Howard Ragsdale',
    29. 'Tiernan Larkins', 'Seren Sweeny', 'Arisha Keys',
    30. 'Kiki Wearing', 'Kyran Ponsonby', 'Diannon Pepper',
    31. 'Mari Foston', 'Sunil Manson', 'Donald Wykes',
    32. 'Rosie Higham', 'Karmin Raines', 'Tayyibah Leathem',
    33. 'Kara-jay Knoll', 'Shail Dalgleish', 'Jaimie Sells'):
    34. start = now.addDays(-random.randint(1, 30))
    35. start = now.addSecs(-random.randint(60 * 5, 60 * 60 * 2))
    36. end = start.addSecs(random.randint(20, 60 * 13))
    37. topic = random.choice(topics)
    38. query.addBindValue(QVariant(QString(name)))
    39. query.addBindValue(QVariant(start))
    40. query.addBindValue(QVariant(end))
    41. query.addBindValue(QVariant(QString(topic)))
    42. query.exec_()
    43. QApplication.processEvents()
    To copy to clipboard, switch view to plain text mode 

    it creates the database table but does not insert the information because there are more fields in the database than there is in the insert query?!?!?!?

    Now i come from using php and mysql background and it no problem at all to insert less data into a database than the fields that take up the database.

    IS this normal behaviour? im i the one who is crazy?

    basically to make this query work i had to put the extra field in the query and just send a null value to is in the addBindValue variable for it:

    Qt Code:
    1. query.prepare("INSERT INTO calls (caller, starttime, endtime, random_new_field, "
    2. "topic) VALUES (?, ?, ?, ?, ?)")
    3. for name in ('Joshan Cockerall', 'Ammanie Ingham',
    4. 'Diarmuid Bettington', 'Juliana Bannister',
    5. 'Oakley-Jay Buxton', 'Reilley Collinge',
    6. 'Ellis-James Mcgehee', 'Jazmin Lawton',
    7. 'Lily-Grace Smythe', 'Coskun Lant', 'Lauran Lanham',
    8. 'Millar Poindexter', 'Naqeeb Neild', 'Maxlee Stoddart',
    9. 'Rebia Luscombe', 'Briana Christine', 'Charli Pease',
    10. 'Deena Mais', 'Havia Huffman', 'Ethan Davie',
    11. 'Thomas-Jack Silver', 'Harpret Bray', 'Leigh-Ann Goodliff',
    12. 'Seoras Bayes', 'Jenna Underhill', 'Veena Helps',
    13. 'Mahad Mcintosh', 'Allie Hazlehurst', 'Aoife Warrington',
    14. 'Cameron Burton', 'Yildirim Ahlberg', 'Alissa Clayton',
    15. 'Josephine Weber', 'Fiore Govan', 'Howard Ragsdale',
    16. 'Tiernan Larkins', 'Seren Sweeny', 'Arisha Keys',
    17. 'Kiki Wearing', 'Kyran Ponsonby', 'Diannon Pepper',
    18. 'Mari Foston', 'Sunil Manson', 'Donald Wykes',
    19. 'Rosie Higham', 'Karmin Raines', 'Tayyibah Leathem',
    20. 'Kara-jay Knoll', 'Shail Dalgleish', 'Jaimie Sells'):
    21. start = now.addDays(-random.randint(1, 30))
    22. start = now.addSecs(-random.randint(60 * 5, 60 * 60 * 2))
    23. end = start.addSecs(random.randint(20, 60 * 13))
    24. topic = random.choice(topics)
    25. random_new_field = ""
    26. query.addBindValue(QVariant(QString(name)))
    27. query.addBindValue(QVariant(start))
    28. query.addBindValue(QVariant(end))
    29. query.addBindValue(QVariant(QString(random_new_field)))
    30. query.addBindValue(QVariant(QString(topic)))
    31. query.exec_()
    32. QApplication.processEvents()
    To copy to clipboard, switch view to plain text mode 

    the main reson i ask about this is is this just an sqlite problem or is this a qt problem and i will have this same issue what ever database i put on the end of these queries.

    or have i totally f-d up the way i should be using the sql module and i can actually just insert one new field and row into a table with 20 colums if ishould ever feel like so with out having to send null values to the rest of the colums
    Last edited by morraine; 12th August 2008 at 17:10.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: SQLite Woes

    Quote Originally Posted by morraine View Post
    random_new_field VARCHAR(80) NOT NULL,
    This field was declared with "NOT NULL" constraint, which means that you have to give it some value.

    Quote Originally Posted by morraine View Post
    query.prepare("INSERT INTO calls (caller, starttime, endtime, "
    "topic) VALUES (?, ?, ?, ?)")
    Here you don't specify that random_new_field. This means it will be set to the default value, which is null and as you saw above it is forbidden.

    Quote Originally Posted by morraine View Post
    Now i come from using php and mysql background and it no problem at all to insert less data into a database than the fields that take up the database.
    Except for "NOT NULL" fields.

    Quote Originally Posted by morraine View Post
    IS this normal behaviour? im i the one who is crazy?
    Yes (at least for the former ).

    Quote Originally Posted by morraine View Post
    basically to make this query work i had to put the extra field in the query and just send a null value to is in the addBindValue variable for it:
    ...
    random_new_field = ""
    ...
    query.addBindValue(QVariant(QString(random_new_fie ld)))
    If you look closely you will see that you don't set it to NULL, but to an empty string. You can avoid the problem by adding DEFAULT '' or removing NOT NULL constraint.

  3. #3

    Default Re: SQLite Woes

    thanks for your reply jacek

    please tell me

    what is the difference between using

    Qt Code:
    1. random_new_field VARCHAR(80) NOT NULL DEFAULT,
    To copy to clipboard, switch view to plain text mode 

    and

    Qt Code:
    1. random_new_field VARCHAR(80),
    To copy to clipboard, switch view to plain text mode 

    which is better to use or what would be the right way to use them both?

    thanks again

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: SQLite Woes

    Quote Originally Posted by morraine View Post
    random_new_field VARCHAR(80) NOT NULL DEFAULT,
    You have to specify some value after DEFAULT. For example DEFAULT ''.

    The difference is that if you keep NOT NULL, that field never will be null (but it still can be an empty string).

    Quote Originally Posted by morraine View Post
    which is better to use or what would be the right way to use them both?
    You can use both (i.e. declare a field that has a default value and is nullable at the same time), but null values can cause trouble, so it is better to avoid them. Unless you have some good reason for using nulls in random_new_field, you should use the first option (NOT NULL DEFAULT '').

  5. #5

    Default Re: SQLite Woes

    Sorry yes i forgot to put the single quote marks of a nothing string

    Thanks for you help on this mate your a life saver

  6. #6

    Default Re: SQLite Woes

    works like a dream now, thanks

Similar Threads

  1. Insert unicode in SQlite
    By Kastagne in forum Qt Programming
    Replies: 3
    Last Post: 11th October 2011, 14:07
  2. SQLite
    By cyberboy in forum Installation and Deployment
    Replies: 1
    Last Post: 15th April 2008, 19:46
  3. sqlbrowser and sqlite
    By janus in forum Installation and Deployment
    Replies: 2
    Last Post: 31st March 2008, 14:59
  4. The Sqlite Error In Run!
    By alphaboy in forum Installation and Deployment
    Replies: 1
    Last Post: 19th November 2007, 14:45
  5. Bulk insert into SQLite
    By munna in forum Qt Programming
    Replies: 6
    Last Post: 19th November 2007, 03:56

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.