字符串” ' ”插入数据库时出现问题
我正在使用 Windows Phone 的 sqlite 客户端作为我的数据库。我在此代码中遇到了有关文本格式的问题:
cmd.CommandText = @" Insert into Restaurants (address,description,id,latitude,longitude,name,opening_hours,phone,sandwich,price,updated_at,website,score,rating_count,thumbnail_url) values ('" + r.address + "','" + r.description + "',"+r.id +","+r.latitude+","+r.longitude+",'"+r.name+"','"+r.opening_hours+"','"+r.phone+"','"+r.sandwich+"','"+r.price+"','"+r.updated_at+"','"+r.website+"',"+r.score+","+r.rating_count+",'"+r.thumbnail_url+"')";
cmd.ExecuteScalar();
问题是文本字段可能类似于 "xyz it's abc"
,因此 '
破坏了我的更新命令。我如何保留 '
并使我的代码运行?
i am using sqlite client for windows phone for my database. I run into an issue regarding text formatting in this code :
cmd.CommandText = @" Insert into Restaurants (address,description,id,latitude,longitude,name,opening_hours,phone,sandwich,price,updated_at,website,score,rating_count,thumbnail_url) values ('" + r.address + "','" + r.description + "',"+r.id +","+r.latitude+","+r.longitude+",'"+r.name+"','"+r.opening_hours+"','"+r.phone+"','"+r.sandwich+"','"+r.price+"','"+r.updated_at+"','"+r.website+"',"+r.score+","+r.rating_count+",'"+r.thumbnail_url+"')";
cmd.ExecuteScalar();
The issue is that the text fields maybe like "xyz it's abc"
and so the '
breaks my update command. How can i keep the '
and make my code run?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用
参数
代替硬编码字符串(查询)。Use
Parameter
instead of hard coded string (query).不要使用逐字查询字符串(更)容易受到攻击,而是使用 参数:
Instead of using a verbatim query string, which is (more) open to attack, use parameters instead:
考虑使用存储过程或参数化查询而不是直接 SQL。这将带来额外的好处,使您的代码不易出现问题。
Consider using stored procedures or parameterised queries rather than direct SQL. This will have the added benefit of making your code less susceptible to issues.
您可以通过执行以下操作来转义字符:
但是以您的方式构建查询可能会导致一些严重的安全风险。请查看以下文章,其中介绍了如何更改动态 sql 以使用 sql 参数:
You could escape the characters by doing something like:
But building a query the way you do could lead to some serious security risks. Have a look at the following article which describes how to change dynamic sql to use sql parameters: