首页 > 解决方案 > How do you escape strings for SQLite table names in c?

问题描述

How do you escape strings for SQLite table names in c?

I find a document, but it do not tell me the detail https://www.sqlite.org/lang_keywords.html

And this document says that sql is end with '\x00' https://www.sqlite.org/c3ref/prepare.html

Here is the similar question in python: How do you escape strings for SQLite table/column names in Python?

标签: csqlite

解决方案


Example:

  • example to 'example'
  • 'a to '''a'

Detail:

  • Do not use the byte value 0 in the string.It will return an error like unrecognized token: "'" even if you pass the correct zSql len to sqlite3_prepare_v2().
  • Replace ' to '' in the string, add ' to the start and the end to the string. ' is single quote (byte 39).
  • It is not recommend to use invalid utf8 string. The string do not need to be valid utf8 string according to the parse code in sqlite3 version 3.28.0 . I have tested that invalid utf8 string can use as table name, but the document of sqlite3_prepare_v2() says you need SQL statement, UTF-8 encoded

  • I have write a program to confirm that any byte list with len 1,2 without byte value 0 in it, can use as the table name, and the program can read value from that table, can list the table from the SQLITE_MASTER table in sqlite3 version 3.28.0.


推荐阅读