转义单引号字符以用于SQLite查询 [英] Escape single quote character for use in an SQLite query

查看:123
本文介绍了转义单引号字符以用于SQLite查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了数据库模式(到目前为止只写了一张表),并将该表的INSERT语句写在一个文件中.然后,我按如下所示创建数据库:

I wrote the database schema (only one table so far), and the INSERT statements for that table in one file. Then I created the database as follows:

$ sqlite3 newdatabase.db
SQLite version 3.4.0
Enter ".help" for instructions
sqlite> .read ./schema.sql
SQL error near line 16: near "s": syntax error

文件的第16行看起来像这样:

Line 16 of my file looks something like this:

INSERT INTO table_name (field1, field2) VALUES (123, 'Hello there\'s');

问题是单引号的转义字符.我还尝试了对单引号进行两次转义(使用 \\\'而不是 \'),但这也不起作用.我在做什么错了?

The problem is the escape character for a single quote. I also tried double escaping the single quote (using \\\' instead of \'), but that didn't work either. What am I doing wrong?

推荐答案

尝试将单引号加倍(许多数据库都希望这样),所以应该是:

Try doubling up the single quotes (many databases expect it that way), so it would be :

INSERT INTO table_name (field1, field2) VALUES (123, 'Hello there''s');

来自文档的相关报价:

通过将字符串括在单引号(')中来形成字符串常量.可以通过将两个单引号放在一行中来对字符串中的单引号进行编码-就像在Pascal中一样.不支持使用反斜杠字符的C样式转义,因为它们不是标准的SQL.BLOB文字是包含十六进制数据并以单个"x"或"X"字符开头的字符串文字....文字值也可以是标记"NULL".

A string constant is formed by enclosing the string in single quotes ('). A single quote within the string can be encoded by putting two single quotes in a row - as in Pascal. C-style escapes using the backslash character are not supported because they are not standard SQL. BLOB literals are string literals containing hexadecimal data and preceded by a single "x" or "X" character. ... A literal value can also be the token "NULL".

这篇关于转义单引号字符以用于SQLite查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆