简体   繁体   English

MySQL报价,双引号一团糟

[英]MySQL quotes, double quotes mess

I'm trying to insert content into a MySQL and it looks like that: 我正在尝试将内容插入MySQL,看起来像这样:

table name is foo and the columns are: 表名是foo ,列是:

     id INTEGER,
     name VARCHAR(50),
     content VARCHAR(255),
     url VARCHAR(255)

the problem arises when I'm trying to insert string which has quotes and double quotes inside it and escaping must be done since when inserting strings into MySQL you need to surround it in single quotes. 当我尝试插入其中带有引号和双引号的字符串时,就会出现问题,并且必须进行转义,因为将字符串插入MySQL时,您需要将其括在单引号中。

How do I escape the following text ? 如何转义以下文字?

'<div><a href='http://www.ynet.co.il/articles/0,7340,L-4391031,00.html'><img src='http://www.ynet.co.il/PicServer3/2013/06/11/4677612/AP0AMR108-Main-2013-05-25T16-54-20.070Z239775_a.jpg' alt='צילום: AP' title='צילום: AP' border='0' width='116' height='116'></a></div>בית המשפט העליון אישר בהחלטה תקדימית לחיילים ושוטרים להצביע בבחירות - לראשונה מאז שנות ה-70. האיסלאמיסטים זעמו על הפסיקה בעוד הליברלים ואנשי האופוזיציה בירכו: "יש מדינות שמעניקות לאנשי הצבא שלהן להצביע בבחירות ובהם דרום אפריקה, ברזיל, הודו, רוסיה, ארצות הברית, ואפילו ישראל"' 

You escape any string only for what's around it. 您只对周围的字符串进行转义。 So if you want to put double quotes around this one, you have to protect only the double quote inside. 因此,如果要在双引号周围加上双引号,则只需保护其中的双引号。

You can escape a quote using \\ also known as the escape character. 您可以使用\\也称为转义字符)对引号进行转义。 Have a look at this page that showcases the available escape sequences. 看看此页面 ,其中展示了可用的转义序列。

You can escape quotes with backslash: 您可以使用反斜杠对引号进行转义:

insert into foo (id,name,content,url) values (0, 'test123', '<div id=\'test\'>ת"\א היא עיר מצוינת</div>', 'http://www.cnn.com')

You can also use the opposite kind of quotes: 您还可以使用相反的引号:

insert into foo (id,name,content,url) values (0, 'test123', "someContent's", 'http://www.cnn.com')

Here's how to do it with WebSQL parameterized queries: 这是使用WebSQL参数化查询的方法:

transaction.executeSQL('INSERT INTO foo (id, name, content, url) VALUES (?, ?, ?, ?)',
    [0, 'test123', '<div id=\'test\'>ת"\א היא עיר מצוינת</div>', 'http://www.cnn.com'],
    callbackfn);

尝试这个 :

insert into foo (id,name,content,url) values (0,'test123','someContent\'s','http:\/\/www.cnn.com')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM