Page 1 of 2 12 LastLast
Results 1 to 20 of 27

Thread: QSqlite in QT4

  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 QSqlite in QT4

    I am trying to use QSqlite in my QT project.
    according to the QT assistant, I use the QSqlquery to insert data into database, but failed!!
    Here's my code:
    QSqlQuery insert(db);
    insert.exec("select * from Person");
    int num = insert.size();
    if( !insert.isActive())
    QMessageBox::information(this, "Query not activited", "Sorry but this query is not activited!");
    bool ok = insert.exec( QString("insert into Person values (%1, \'%2\', \'%3\', %4)").arg(num + 1).arg(name).arg(sex).arg(age));

    Plz tell me how to make it if anybody know it.
    Thanks!!
    Last edited by sophister; 3rd April 2009 at 16:25. Reason: This problem has gone, and another comes

  2. #2
    Join Date
    Feb 2009
    Location
    Venezuela
    Posts
    10
    Thanked 4 Times in 4 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlite in QT4

    Hi!

    It's: %4, %1, %2, %3

    With '%' before digits.

  3. #3
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlite in QT4

    1) The formatting is wrong: "1%" instead of "%1"

    2) You open the database before use QSqlQuery?

    For Example
    Qt Code:
    1. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    2. db.setDatabaseName(dbName);
    3. db.open();
    To copy to clipboard, switch view to plain text mode 

    3) Using "tr" in QUERY string it's dangerous (wrong). You must not translate queries.
    A camel can go 14 days without drink,
    I can't!!!

  4. #4
    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: QSqlite in QT4

    Thanks, I do open the database before using QSqlQuery.
    I want to insert some virables into the table. If I don't use tr, how can I insert the virables then??
    My platform is XP.

  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: QSqlite in QT4

    thanks, I have corrected it.
    But now what do trouble me is how to insert data into the table.
    It's really difficult.

  6. #6
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlite in QT4

    Quote Originally Posted by sophister View Post
    I want to insert some virables into the table. If I don't use tr, how can I insert the virables then??
    Qt Code:
    1. insert.exec( QString("insert into Person values (%4, \'%1\', \'%2\', %3)").arg(num + 1).arg(name).arg(sex).arg(age))
    To copy to clipboard, switch view to plain text mode 

    Pay attention to values type. I think "name" and "sex" are strings.
    A camel can go 14 days without drink,
    I can't!!!

  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

    Question Re: QSqlite in QT4

    I find some guides to SQLite, whick say that we can use the following code to insert data into one table----

    insert into table_name values(data1, data2, data3, ...);


    But when I use it, the application just cannot do that successfully.

  8. #8
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlite in QT4

    I edited my previous post.

    The problem is that some values are strings (name and sex); in this case you have to wrap with ''
    Qt Code:
    1. insert.exec( QString("insert into Person values (%4, \'%1\', \'%2\', %3)").arg(num + 1).arg(name).arg(sex).arg(age))
    To copy to clipboard, switch view to plain text mode 
    A camel can go 14 days without drink,
    I can't!!!

  9. #9
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlite in QT4

    I edited my previous post.

    The problem is that some values are strings (name and sex); in this case you have to wrap with ''

    Qt Code:
    1. insert.exec( QString("insert into Person values (%4, \'%1\', \'%2\', %3)").arg(num + 1).arg(name).arg(sex).arg(age))
    To copy to clipboard, switch view to plain text mode 
    __________________
    A camel can go 14 days without drink,
    I can't!!!

  10. The following user says thank you to mcosta for this useful post:

    sophister (3rd April 2009)

  11. #10
    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: QSqlite in QT4

    yeah, you guess it!!
    I really appreciate your help!!!
    I successfully insert the values into the table.

    But just one problem, the "age"'s type is int, how can I insert it into the table?

  12. #11
    Join Date
    Feb 2009
    Location
    Venezuela
    Posts
    10
    Thanked 4 Times in 4 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlite in QT4

    Like strings... But without ' ':

    Qt Code:
    1. ... QString("INSERT INTO table VALUES ( %1, %2 )").arg( int1 ).arg( int2 ) ...
    To copy to clipboard, switch view to plain text mode 

    Note:
    String, date with ' '.
    Double, float, integer: without ' '.

  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: QSqlite in QT4

    you see, when I show the table in the QTextEdit, the INTEGER cannot display appropriately.

    I use the following code to display the int age---
    age = query.value(3).toInt();

    Where am I wrong??

  14. #13
    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: QSqlite in QT4

    yeah, thanks!!
    But now I have another question.

    you see, when I show the table in the QTextEdit, the INTEGER cannot display appropriately.

    I use the following code to display the int age---
    age = query.value(3).toInt();

    Where am I wrong??
    Would you plz tell me??

  15. #14
    Join Date
    Feb 2009
    Location
    Venezuela
    Posts
    10
    Thanked 4 Times in 4 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlite in QT4

    mmm...

    Qt Code:
    1. int age = query.value(3).toInt();
    2. ...
    3. textEdit->append( QString("Age = %1").arg( age ) );
    To copy to clipboard, switch view to plain text mode 

    That should work... If "age" is in column 3 of table and "age"'s type is Integer.

  16. The following user says thank you to JhonJames for this useful post:

    sophister (3rd April 2009)

  17. #15
    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: QSqlite in QT4

    Thank you very much!!
    But why the following codes don't work----
    name = query.value(1).toString();
    sex = query.value(2).toString();
    age = query.value(3).toInt();
    textEdit->append( name + '\t' + sex + '\t' +age);
    Sorry to trouble you again.

  18. #16
    Join Date
    Feb 2009
    Location
    Venezuela
    Posts
    10
    Thanked 4 Times in 4 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlite in QT4

    Qt Code:
    1. QString name = query.value(1).toString();
    2. QString sex = query.value(2).toString();
    3. int age = query.value(3).toInt();
    4. textEdit->append( QString("%1\t%2\t%3").arg( name ).arg( sex ).arg( age ) );
    5.  
    6. //or
    7.  
    8. textEdit->append( name + "\t" + sex + "\t" + QString::number(age) );
    To copy to clipboard, switch view to plain text mode 

  19. The following user says thank you to JhonJames for this useful post:

    sophister (3rd April 2009)

  20. #17
    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: QSqlite in QT4

    Thanks!!
    I really learn a lor from you!!

  21. #18
    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: QSqlite in QT4

    Sorry to bother you again, I use the following codes to insert data into table, but I am sure there is something wrong with the following codes---
    QSqlQuery insert(db);
    insert.exec("select * from Person");
    int num = insert.size();

    How can I get the numbers of the records in this table??
    QSqlQuery::size, or QSqlQuery::numberRowAffected(), do not work!!
    They two return -1!!
    Where is wrong??

  22. #19
    Join Date
    Feb 2009
    Location
    Venezuela
    Posts
    10
    Thanked 4 Times in 4 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlite in QT4

    Try with this:
    Qt Code:
    1. QSqlQuery insert(db);
    2. if (!insert.exec("select * from Person"))
    3. QMessageBox::critical(0, "Error", insert.lastError().text());
    4. else
    5. QMessageBox::information(0, "Successfull", QString("Query successfully with %1 rows affected").arg( insert.numRowsAffected() ));
    To copy to clipboard, switch view to plain text mode 

    With this code you can see the error or the number of rows affected by query.

  23. #20
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlite in QT4

    try:

    Qt Code:
    1. QSqlQuery query("SELECT * FROM Person", db);
    2. int num = query.size();
    3. textEdit->append("Number of persons: " + QString::number(num));
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 1
    Last Post: 7th July 2008, 20:13
  2. QSQLITE | unable to establish connection
    By suresh in forum Newbie
    Replies: 4
    Last Post: 1st July 2008, 11:17
  3. QSQLITE dirver not loaded
    By mismael85 in forum Installation and Deployment
    Replies: 1
    Last Post: 3rd April 2008, 20:07
  4. QSQLITE Questions
    By larry104 in forum Qt Programming
    Replies: 9
    Last Post: 24th April 2007, 23:54
  5. QSQLITE Unable to fetch row in create
    By xgoan in forum Newbie
    Replies: 3
    Last Post: 18th October 2006, 14:39

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
  •  
Qt is a trademark of The Qt Company.