我想在表格的字段中插入String'xxx'xxx'。 '性格中的问题。 我如何插入这个角色?用c在postgresql DB中插入错误#
回答
双single quote
如果你是直接插入,
INSERT INTO tableName (colName) VALUES ('xxx''xxx')
,但如果你是在C#
这样做,使用参数化查询。
string connStr = "connection String here";
string val = "xxx'xxx";
string query = "INSERT INTO tableName (colName) VALUES (:val)";
using(NpgsqlConnection conn = new NpgsqlConnection(connStr))
{
using(NpgsqlCommand comm = new NpgsqlCommand())
{
comm.Connection = conn;
comm.CommandText = query;
NpgsqlParameter p = new NpgsqlParameter("val", NpgsqlDbType.Text);
p.value = val;
comm.Parameters.Add(p);
try
{
conn.Open();
comm.ExecuteNonQuery();
}
catch(NpgsqlException e)
{
// do something with
// e.ToString();
}
}
}
(:val)< - 不应该是@val – Zaki 2013-02-18 14:26:27
@ Sam1我认为':'也被支持。 – 2013-02-18 14:28:20
AddWithValue()方法不存在.. – 2013-02-18 14:31:09
你需要复制的单引号:
insert into foo (col_name)
values
('xxx''xxx');
但你应该考虑准备语句,这将不仅使之类的东西容易得多,但也将保护您免受SQL注入(我不知道知道C#,所以我帮不了你的细节)。
在C#如果你想插入单引号,你可以通过更换原有的值,因此这样做:
string x = "xxx'xxx";
string replacedText = x.Replace("'","''");
和插入时从SQL注入预防始终使用参数:
myCommand.CommandText = "INSERT INTO TableName (x) VALUES (@x)";
myCommand.Parameters.Add("@x", x);
- 1. Laravel DB插入错误500
- 2. 使用c#在Mongo DB中插入DataTable#
- 3. 使用php和DBMS在db中插入新行是postgresql?
- 4. rake db:用postgresql创建编码错误
- 5. HSQL DB插入语句错误
- 6. 将时间戳插入postgresql db
- 7. PHP - DB插入错误 - 查询为空
- 8. 忽略PostgreSQL的批量插入错误
- 9. 节点js Postgresql - 错误时插入表
- 10. postgresql插入python的时间戳错误
- 11. 批量插入时忽略错误Postgresql
- 12. django psycopg2和postgresql插入错误
- 13. Postgresql,检查插入时触发错误
- 14. Java PostgreSQL插入引发意外错误
- 15. 如何在DES中插入加密值到Postgresql DB中?
- 16. Codeigniter DB插入不正常(它插入错误的值)
- 17. PostgreSQL在应用引擎标准 - 插入错误
- 18. unordered_map插入错误C++
- 19. 插入错误
- 20. QT - PostgreSQL的插件错误
- 21. 在PostgreSQL中插入缓慢
- 22. 从C#中插入NULL到SQL DB DbCommand
- 23. 插入错误,同时将数据插入到PostgreSQL使用python psycopg2
- 24. utf8mb4_unicode_520_ci错误导入DB
- 25. SQL - 插入到postgresql表中产生分号错误
- 26. 在C中使用postgresql插入整数数组(libpq)
- 27. 返回最后插入的ID在postgresql中用c#
- 28. 无匹配函数错误,用于在C++中插入列表
- 29. 如何DB插入
- 30. 当我使用PostgreSQL作为开发时,使用heroku db获取SQLite错误db
为什么近距离投票,这是一个简单,直接和有效的问题与简单的答案。 – Johan 2013-02-19 03:23:12