Knowledgebase
[QODBC ALL] QODBC: How to Escape Single Quotes in SQL Statements via the Driver
Posted by William - QODBC Support on 01 October 2026 12:01 PM

[QODBC ALL] QODBC: How to Escape Single Quotes in SQL Statements via the Driver

Problem Description:

When inserting or updating records in QuickBooks tables via QODBC using SQL statements, string fields that contain single quotes (e.g., O'Brien, Sharee's Beauty Supply) cause SQL syntax errors because the single quote terminates the string literal prematurely.

Affected products: QODBC Desktop, QODBC Online, QuickBooks Desktop (all editions)
Expected outcome: All string fields containing single quotes are properly escaped so the INSERT or UPDATE query executes without syntax errors.

Symptoms:

  • SQL INSERT or UPDATE queries fail with a syntax error such as "Expected lexical element not found: = )" when any string field contains a single quote.
  • The customer tries using a backslash to escape the single quote (e.g., `ShipAddressAddr1='Sharee\'s Beauty Supply'`), but QODBC passes the value without the backslash, and the query still fails.
  • Some fields are escaped with double single quotes (`''`) while others contain a raw single quote - the unescaped field causes the entire statement to fail even if the others are correct.

Cause:

QODBC follows standard SQL escaping rules. In SQL, a single quote within a string literal must be escaped by doubling it - using '' (two consecutive single quotes). A backslash (\') is NOT valid SQL escaping syntax and will be passed through literally or stripped, causing the query to break.

This applies universally: every field value containing a single quote must be escaped with'', not just some of them. If even one field is missed, the entire SQL statement fails.

Resolution:

  1. Replace every single quote (') in string field values with two single quotes ('').

    For example, the name Sharee's Beauty Supply becomes:
    ShipAddressAddr1='Sharee''s Beauty Supply'
  2. Verify ALL fields in the statement. Every string value that contains a single quote must be escaped - not just the one that originally caused the error. In complex INSERT statements with many columns, miss even one unescaped field and the entire query will fail.

    For example:
    INSERT INTO Customer (LastName,BillAddressCity) VALUES(
      'RI-KYIA''S BEAUTY SUPPLY','O''FALLON')
  3. Do NOT use backslash escaping. The backslash (\) is not recognized as an escape character in standard SQL and will cause the query to break.

    WRONG: 'Sharee\'s Beauty Supply'
    RIGHT: 'Sharee''s Beauty Supply'
  4. Test your full query. After escaping all relevant fields, run the statement again. It should execute without syntax errors.

SQL Examples:

Correct - all single quotes properly escaped:

UPDATE SalesOrder
  SET ShipAddressAddr1 = 'Sharee''s Beauty Supply',
      BillAddressCity = 'O''FALLON'
  WHERE TxnID = '762481-1737580522'

Incorrect - unescaped single quote causes syntax error:

UPDATE SalesOrder
  SET ShipAddressAddr1 = 'O'FALLON'
  WHERE TxnID = '762481-1737580522'

Example Scenarios:

Scenario 1 - Customer name with apostrophe:

A customer record has the name O'Brien. When inserting via QODBC:
WRONG: Name='O'Brien' - fails with a SQL syntax error.
RIGHT: Name='O''Brien' - executes successfully.

Scenario 2 - Multi-field INSERT with multiple apostrophes:

An INSERT into the Customer table has fields like CompanyName = RI-KYIA'S BEAUTY SUPPLY (234 O'FALLON,MO). Every single quote across all fields must be doubled:
RIGHT: CompanyName='RI-KYIA''S BEAUTY SUPPLY (234 O''FALLON,MO)'

Additional Notes:

  • Doubling single quotes is standard SQL. This is not a QODBC-specific rule - it applies to all SQL databases that follow the ANSI SQL standard (SQL Server, MySQL, PostgreSQL, Oracle, etc.).
  • In application code (C#, Python, etc.): If building SQL strings programmatically, use parameterized queries or a proper escaping function rather than manual string concatenation to avoid missing any escaped characters.
  • QODBC Online: The same single-quote doubling rule applies when constructing SQL statements via QODBC Online.

QODBC: Working with Special Characters in QuickBooks Data Fields QODBC: SQL Query Syntax Reference Best Practices for Building SQL Queries with QODBC

Keywords:

Single Quote Escape Character SQL Syntax Error String Escaping INSERT UPDATE Apostrophe Double Quote QODBC SQL Special Characters ANSI SQL String Literal C#
(0 vote(s))
Helpful
Not helpful

Comments (0)
Post a new comment
 
 
Full Name:
Email:
Comments:
CAPTCHA Verification 
 
Please complete the captcha below (we use this to prevent automated submissions).