Results 1 to 4 of 4

Thread: How to insert mixed (double/single) quotes into sqlite database?

  1. #1
    Join Date
    Jan 2015
    Posts
    35
    Thanks
    20
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Question How to insert mixed (double/single) quotes into sqlite database?

    Hi,

    I want to save some sentences into sqlite database. These sentences contains mixed single quotes and qouble quotes.

    sample sentence is:
    Homer Simpson said: "Maybe, just once, someone will call me 'Sir' without adding 'you're making a scene'."

    I want to assign these words to string and then insert into database.

    QString str1 ="Homer Simpson said: "Maybe, just once, someone will call me 'Sir' without adding 'you're making a scene'."";

    str1.replace(QString("'"),QString("\'"));
    str1.replace(0x22,QString("\""));

    query->exec("insert into greatquote values(" + QString::number(intNo) + ", '" + str1 + "')";

    Sample sentence can't assign to str1. How can I change these words with escape special characters? Which functions I use?
    Finally how to insert these into sqlite database?

    Thanks

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to insert mixed (double/single) quotes into sqlite database?

    You are trying to manually include the parameters in the query by concatenating strings, while you should be allowing the SQL engine to do it.
    See QSqlQuery::prepare().

    Cheers,
    _

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

    binary001 (24th April 2016)

  4. #3
    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: How to insert mixed (double/single) quotes into sqlite database?

    If this sentence:
    Homer Simpson said, "Maybe, just once, someone will call me 'Sir' without adding 'you're making a scene'."
    Is to appear as a literal string in your C++ code (as in your example) then you must escape the embedded double quotes:
    Qt Code:
    1. QString str1 ="Homer Simpson said, \"Maybe, just once, someone will call me 'Sir' without adding 'you're making a scene'.\"";
    To copy to clipboard, switch view to plain text mode 
    This is before you do what anda_skoa suggests and use parameter binding to do the SQL insert safely.

  5. The following user says thank you to ChrisW67 for this useful post:

    binary001 (24th April 2016)

  6. #4
    Join Date
    Jan 2015
    Posts
    35
    Thanks
    20
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Re: How to insert mixed (double/single) quotes into sqlite database?

    Thanks you for your suggestions.

    It's working with QSqlQuery:repare() and addBindValue.

Similar Threads

  1. Replies: 7
    Last Post: 21st March 2014, 10:24
  2. QSqlQuery escaping single quotes
    By pdoria in forum Qt Programming
    Replies: 0
    Last Post: 1st April 2012, 21:37
  3. How to insert row to SQLite database?
    By MIH1406 in forum Qt Programming
    Replies: 6
    Last Post: 29th May 2010, 12:22
  4. Replies: 2
    Last Post: 13th April 2010, 16:50
  5. SQLite sometimes doens't INSERT into database
    By cevou in forum Qt Programming
    Replies: 5
    Last Post: 30th October 2009, 08:10

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.