Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8822469
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T06:06:51+00:00 2026-06-14T06:06:51+00:00

Excerpt from SQLite reference on sqlite3_mprintf() API http://www.sqlite.org/c3ref/mprintf.html The %Q option works like %q

  • 0

Excerpt from SQLite reference on sqlite3_mprintf() API

http://www.sqlite.org/c3ref/mprintf.html

“The %Q option works like %q except it also adds single quotes around the outside of the total string. Additionally, if the parameter in the argument list is a NULL pointer, %Q substitutes the text “NULL” (without single quotes).”

When using %q, we have to be careful to always use single quotes around it e.g.

char *zText = "It's a happy day!";
char *zSQL = sqlite3_mprintf("INSERT INTO table VALUES('%q')", zText);

It seems more convenient to always use %Q instead of %q as follows:

char *zSQL = sqlite3_mprintf("INSERT INTO table VALUES(%Q)", zText);

My question – is there a valid use case where ‘%q’ is more suitable or more efficient? Or can I safely use %Q as a replacement for %s in all statements?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T06:06:53+00:00Added an answer on June 14, 2026 at 6:06 am

    Everything is about to create SQL-statements in a secure way to minimize the possibility of SQL-injections.

    You should prefer %q over %s all the time.

    The %q option works like %s in that it substitutes a nul-terminated string from the argument list. But %q also doubles every '\'' character. %q is designed for use inside a string literal. By doubling each '\'' character it escapes that character and allows it to be inserted into the string.

    The %Q has some futher advantages over %q.

    The %Q option works like %q except it also adds single quotes around the outside of the total string. Additionally, if the parameter in the argument list is a NULL pointer, %Q substitutes the text “NULL” (without single quotes).

    This means it will add single quotes and it will render nul pointers as the string literal "NULL" inside the string.

    As you can see it depends on what you want to do. If you are ok with adding single quotes and that "NULL" generating behavior you can simply use %Q (I think it makes highly sense for paramter values in SQL-statements). Maybe sometimes you dont want this behavior then you can fall back to %q.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is an excerpt from my code: xtype: 'image', src: 'http://www.sencha.com/files/blog/old/blog/wp-content/uploads/2010/06/sencha-logo.png', left: '50%', top:
Excerpt from: http://www.sqlservercentral.com/articles/Indexing/68563/ The width of the clustering key does not, however, only affect
Here is a code excerpt from AspComet project that works with Autofac. public MessageBus(IClientRepository
Here's an excerpt from Sun's Java tutorials : A switch works with the byte
I am getting data from iphone sqlite like this. I have 6 records in
Excerpt from a book: A float value consists of a 24-bit signed mantissa and
Excerpt from my JavaScript console: > 0 in [1, 2] true Why?
Here is excerpt from my larger JavaScript file: function dashboardConfig() { $(window).on('resize', function ()
Below is an excerpt from an .svg file (which is xml): <text xml:space=preserve style=font-size:14.19380379px;font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;text-align:start;line-height:125%;writing-mode:lr-tb;text-anchor:start;fill:#000000;fill-opacity:1;stroke:none;font-family:DejaVu
Here's an excerpt from a book I'm reading about application design with MVC: Ideally,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.