Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to deal with comma and apostrophe chars when inserting into SQLight



  • i have string that contains apostrophe and comma's and when i excute insert into SQLight it gives me error for example with string like this :
    @...., 'The Smiths - I Know It's Over', .....
    "Over": syntax error Unable to execute statement@

    how can i or what can i do to keep the apostrophe's in the string but preform valid insert? .
    im using simple :
    @QString InsertSqlStst =" INSERT ......"
    QSqlQuery query;
    ret = query.exec(InsertSqlStst);@



  • You are talking about SQLite, aren't you?

    Please have a look at "SQLite documentation":http://www.sqlite.org/lang_expr.html:

    bq. A single quote within the string can be encoded by putting two single quotes in a row - as in Pascal.



  • yeah SQLite , using the Qt tools to connect
    @m_db = QSqlDatabase::addDatabase("QSQLITE");@
    that means i need to run over each string to check ?
    isn't there some build in solution ?



  • [quote author="umen242" date="1328098068"]
    that means i need to run over each string to check ?
    isn't there some build in solution ?[/quote]

    Use "QSqlDriver::formatValue":http://developer.qt.nokia.com/doc/qt-4.8/qsqldriver.html#formatValue According to Qt documentation it will do the job:

    bq. Any embedded single-quote characters are escaped (replaced with two single-quote characters).



  • Use placeholders and bind them instead of handcrafting things. [[Doc:QSqlQuery]] docs have the gory details.



  • i just looked the :formatValue source code , its using simple replace
    (: and this is what i did.
    Thanks for helping guys


Log in to reply