Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any C SQLite API for quoting/escaping the name of a table?

It's impossible to sqlite3_bind_text a table name because sqlite3_prepare_v2 fails to prepare a statement such as:

SELECT * FROM ? ;

I presume the table name is needed to parse the statement, so the quoting needs to have happened before sqlite3_prepare_v2.

Is there something like a sqlite3_quote_tablename? Maybe it already exists under a name I can't recognize, but I can't find anything in the functions list.

like image 444
gavinbeatty Avatar asked Feb 08 '10 13:02

gavinbeatty


3 Answers

SQLite will escape identifiers for you with the %w format in the https://www.sqlite.org/printf.html family of functions.

like image 159
Björn Höhrmann Avatar answered Nov 10 '22 13:11

Björn Höhrmann


your proposed sqlite3_quote_tablename function could sanitize the input to prevent sql injection attacks. To do this it could parse the input to make sure it is a string literal. http://sqlite.org/lang_expr.html#litvalue

like image 30
momeara Avatar answered Nov 10 '22 13:11

momeara


If a table name has invalid characters in it you can enclose the table name in double quotes, like this.

sqlite> create table "test table" (id);
sqlite> insert into "test table" values (1);
sqlite> select * from "test table";
id
----------
1

Of course you should avoid using invalid characters whenever possible. It complicates development and is almost always unnecessary (IMO the only time it is necessary is when you inherit a project that is already done this way and it's too big to change).

like image 1
Samuel Neff Avatar answered Nov 10 '22 13:11

Samuel Neff