Results 1 to 3 of 3

Thread: inserting content in database

  1. #1
    Join Date
    Apr 2009
    Posts
    206
    Thanks
    34
    Thanked 2 Times in 2 Posts

    Default inserting content in database

    Hello I have a simple question .

    what is the best way to insert content to a database. What about cutting up the string if the content is to long for field in the database. Is there a flag to set for automatic cutting cause when it is to long te insert fails.

    Any idea??

    This is my approach:

    Qt Code:
    1. if(qstr_textLine.count(";",Qt::CaseInsensitive)== 2){
    2. qstr_textLine.replace(QString(","), QString("."));
    3. qstr_textLine.replace(QString(";"), QString(","));
    4. qstr_textLine.replace(QString("'"), QString("''"));
    5. QSqlQuery insertquery;
    6. qstr_textLine.replace(QString(","), QString("','"));
    7. qstr_textLine.prepend("'");
    8. qstr_textLine.append("'");
    9. qstr_textLine.replace(QString("'NULL'"), QString("NULL"));
    10.  
    11. int sqlbool=insertquery.exec("INSERT INTO testtbl (field1,field2,field3) "
    12. "VALUES ("+qstr_textLine+")");
    13. }
    To copy to clipboard, switch view to plain text mode 

    I use mssql.

  2. #2
    Join Date
    Dec 2006
    Posts
    426
    Thanks
    8
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: inserting content in database

    Don't say "I have a simple question", if it is simple, why do you ask?

    How about using QString::left(...)?

    But yet, it is simple

  3. #3
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: inserting content in database

    Quote Originally Posted by codeman View Post
    Qt Code:
    1. if(qstr_textLine.count(";",Qt::CaseInsensitive)== 2){
    2. qstr_textLine.replace(QString(","), QString("."));
    3. qstr_textLine.replace(QString(";"), QString(","));
    4. qstr_textLine.replace(QString("'"), QString("''"));
    5. QSqlQuery insertquery;
    6. qstr_textLine.replace(QString(","), QString("','"));
    7. qstr_textLine.prepend("'");
    8. qstr_textLine.append("'");
    9. qstr_textLine.replace(QString("'NULL'"), QString("NULL"));
    10.  
    11. int sqlbool=insertquery.exec("INSERT INTO testtbl (field1,field2,field3) "
    12. "VALUES ("+qstr_textLine+")");
    13. }
    To copy to clipboard, switch view to plain text mode 
    Yeah, definitely look at QSqlQuery::prepare()!

Similar Threads

  1. Threads and database connection
    By probine in forum Qt Programming
    Replies: 9
    Last Post: 7th August 2013, 08:30
  2. Problem while inserting the data into database
    By sudheer168 in forum Qt Programming
    Replies: 2
    Last Post: 13th December 2008, 12:22
  3. cannot share the database connection!!!!
    By cbarmpar in forum Qt Programming
    Replies: 13
    Last Post: 23rd September 2008, 14:42
  4. Multiple database connections
    By cyberboy in forum Qt Programming
    Replies: 3
    Last Post: 30th March 2008, 16:56
  5. Database Master-Detail Entry Form
    By Phan Sin Tian in forum Newbie
    Replies: 4
    Last Post: 3rd February 2008, 14:31

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.