插入带有未知字符的字符串(即',“,”等)

时间:2020-12-29 05:33:31

I am not too much good with database, but I can manage simple database operation. Currently I am facing one issue when insert data into table. Let me explain scenario.

我对数据库不太好,但我可以管理简单的数据库操作。目前,我在将数据插入表时面临一个问题。让我解释一下情景。

In my app, I am searching places via GoogleMapSDK and then drop down list of places I am selecting one place. Then I want to add that place as a favourite and insert all data in database. Now the problem is GoogleMapSDK returns place name awkward something like : ''Sapna''SPa’’’’’’’. And database query does not let me insert this data into database.

在我的应用程序中,我通过GoogleMapSDK搜索地点,然后下拉我选择一个地方的地方列表。然后我想将该位置添加为收藏夹并将所有数据插入数据库中。现在问题是GoogleMapSDK返回地名尴尬的东西:''Sapna''SPa'''''''。并且数据库查询不允许我将此数据插入数据库。

How can I insert data like this ?

如何插入这样的数据?

I know is in above name if it contains one single quote or double quotes then can be managed with back slash, but how it can manage if you don’t know what data will be there ?

我知道如果它包含一个单引号或双引号,则可以使用反斜杠进行管理,但如果您不知道哪些数据存在,它是如何管理的?

I want to store data as it is, without replacing or removing any char.

我希望按原样存储数据,而无需替换或删除任何字符。

If anyone have solution, please share.

如果有人有解决方案,请分享。

1 个解决方案

#1


1  

You can prepare your querys with variable placeholders instead of constructing the full query string yourself.

您可以使用可变占位符准备查询,而不是自己构造完整的查询字符串。

For example:

例如:

a) INSERT INTO yourtable (name) VALUES ('''Sapna''SPa’’’’’’’')

b) INSERT INTO yourtable (name) VALUES (?)

I'm not familiar with objective-c but usually you can run your querys safely in 3 steps:

我不熟悉objective-c,但通常你可以通过3个步骤安全地运行你的查询:

  1. write your query with placeholders and prepare it (like query b)
  2. 用占位符编写查询并准备它(如查询b)
  3. bind variables to placeholders
  4. 将变量绑定到占位符
  5. execute your prepared statement
  6. 执行准备好的声明

Consult your sqlite driver's documentation for details.

有关详细信息,请参阅sqlite驱动程序的文档。

#1


1  

You can prepare your querys with variable placeholders instead of constructing the full query string yourself.

您可以使用可变占位符准备查询,而不是自己构造完整的查询字符串。

For example:

例如:

a) INSERT INTO yourtable (name) VALUES ('''Sapna''SPa’’’’’’’')

b) INSERT INTO yourtable (name) VALUES (?)

I'm not familiar with objective-c but usually you can run your querys safely in 3 steps:

我不熟悉objective-c,但通常你可以通过3个步骤安全地运行你的查询:

  1. write your query with placeholders and prepare it (like query b)
  2. 用占位符编写查询并准备它(如查询b)
  3. bind variables to placeholders
  4. 将变量绑定到占位符
  5. execute your prepared statement
  6. 执行准备好的声明

Consult your sqlite driver's documentation for details.

有关详细信息,请参阅sqlite驱动程序的文档。