Results 1 to 12 of 12

Thread: SQLite in QT

  1. #1
    Join Date
    Apr 2009
    Location
    China
    Posts
    127
    Thanks
    30
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question SQLite in QT

    I want to create a table in an existed database, and I have usccessfully opened the database.
    How can I use the QSqlQuery::exec( QString& ) to create a table with the following fields:
    id, INTEGER, the primary key
    name, VARCHAR(90)
    author, VARCHAR(50)
    copy, INTEGER
    press, VARCHAR(120)

    AND, the table's name is a varible named "tableName".

    Thank you very much!!

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: SQLite in QT

    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Apr 2009
    Location
    China
    Posts
    127
    Thanks
    30
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: SQLite in QT

    I'm sorry, but what do you mean??
    I think I am asking a Qt related question, because I want to use SQLite in Qt, but it doesn't work well.

  4. #4
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: SQLite in QT

    you have to prepare correct SQL-query for a table creation and wysota posted a link how to do this in right way.
    EDITED: take a look at connection.h which is located in QTDIR/examples/sql.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  5. #5
    Join Date
    Apr 2009
    Location
    China
    Posts
    127
    Thanks
    30
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: SQLite in QT

    I use this to create a table whick is a variable "tableName", why the following codes doesn't woek:
    Qt Code:
    1. QSqlQuery build;
    2. build.exec( QString("create table %1 ("
    3. "id INTEGER PRIMARY KEY AUTOINCREMENT,"
    4. "name VARCHAR(90)"
    5. "author VARCHAR(50)"
    6. "copy INTEGER"
    7. "press VARCHAR(120))").arg( name) )
    To copy to clipboard, switch view to plain text mode 

    and before these codes, I have open the database.
    Thanks in advance.

  6. #6
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: SQLite in QT

    what kind of error do you get? maybe this table already exists?
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  7. #7
    Join Date
    Apr 2009
    Location
    China
    Posts
    127
    Thanks
    30
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: SQLite in QT

    I don't know, I jusr use the following codes:
    Qt Code:
    1. QSqlQuery build;
    2. bool ok = build.exec( QString("create table %1 ("
    3. "id INTEGER PRIMARY KEY AUTOINCREMENT,"
    4. "name VARCHAR(90)"
    5. "author VARCHAR(50)"
    6. "copy INTEGER"
    7. "press VARCHAR(120))").arg( name) )
    8. if( !ok )
    9. QMessageBox::information(this, "Fail", "Fail to create new table!!");
    To copy to clipboard, switch view to plain text mode 

    But I am sure the table doesn't exit.
    puzzled.

  8. #8
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: SQLite in QT

    modify you code like this
    Qt Code:
    1. QSqlQuery build;
    2. bool ok = build.exec( QString("create table %1 ("
    3. "id INTEGER PRIMARY KEY AUTOINCREMENT,"
    4. "name VARCHAR(90)"
    5. "author VARCHAR(50)"
    6. "copy INTEGER"
    7. "press VARCHAR(120))").arg( name) )
    8. if( !ok )
    9. QMessageBox::information(this, "Fail", build.lastError().text());
    To copy to clipboard, switch view to plain text mode 
    and show us a result.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  9. #9
    Join Date
    Apr 2009
    Location
    China
    Posts
    127
    Thanks
    30
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: SQLite in QT

    Oh, I have solved it !!
    I miss the comma among the fields!!
    Thank you for your replying.

  10. #10
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: SQLite in QT

    ok, but anyway, use code which I posted above for determination errors, it will help you in a future.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

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

    sophister (8th April 2009)

  12. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: SQLite in QT

    Quote Originally Posted by sophister View Post
    Oh, I have solved it !!
    I miss the comma among the fields!!
    Thank you for your replying.
    So after all your question was not related to Qt but to a proper SQL statement
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  13. #12
    Join Date
    Apr 2009
    Location
    China
    Posts
    127
    Thanks
    30
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: SQLite in QT

    Uh, yeah, you are right maybe.
    I learn a lot from this forum.

Similar Threads

  1. Qt SQLite user functions
    By cevou in forum Qt Programming
    Replies: 1
    Last Post: 10th March 2009, 19:43
  2. sqlite version in Qt
    By janus in forum Newbie
    Replies: 4
    Last Post: 5th February 2009, 14:18
  3. SQLite
    By cyberboy in forum Installation and Deployment
    Replies: 1
    Last Post: 15th April 2008, 19:46
  4. sqlbrowser and sqlite
    By janus in forum Installation and Deployment
    Replies: 2
    Last Post: 31st March 2008, 14:59
  5. The Sqlite Error In Run!
    By alphaboy in forum Installation and Deployment
    Replies: 1
    Last Post: 19th November 2007, 14: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.