【问题标题】:How insert quotes in postgresql [duplicate]如何在postgresql中插入引号[重复]
【发布时间】:2014-11-09 18:57:45
【问题描述】:

如何使用存储过程在 postgres 中插入双引号和单引号。

I have create the table as :
CREATE TABLE public.test (
  id INT, 
  name varchar(50),
  lname varchar(100)
) 
WITH (oids = false);

2 。创建了要插入该表的存储过程。

CREATE OR REPLACE FUNCTION test_insert( p_array in TEXT[] ) RETURNS TEXT AS
$$
DECLARE
  arrstrMixrecordData ALIAS FOR $1;
  plid integer = 0;
  pid integer = arrstrMixrecordData[1];
  pname varchar = arrstrMixrecordData[2];
  plname varchar = arrstrMixrecordData[3];
BEGIN
  INSERT INTO
    test(id, name, lname)
  values
    (pid, pname, plname);
  plid = ( SELECT id from test ORDER BY id desc limit 1 );
  plid = plid + 1;
  RETURN plid;
END;
$$
  LANGUAGE plpgsql;

3.使用此查询插入数据:

SELECT * from test_insert( '{1,abc,pqr}'::TEXT[] );

有效!!!

4.以下所有组合均失败。当双引号出现在单引号中时,反之亦然

SELECT * from test_insert( '{1,ab"c,pqr}'::TEXT[] );
SELECT * from test_insert( "{1,ab'c,pqr}"::TEXT[] );
SELECT * from test_insert( "{1,ab"c,pqr}"::TEXT[] );
SELECT * from test_insert( '{1,ab'c,pqr}'::TEXT[] );

如何使用存储过程插入以上数据??

【问题讨论】:

  • 失败并出现什么错误到底是什么?

标签: php postgresql


【解决方案1】:

我没有完全阅读您的问题,但请参阅Postgres Documentation 中的quote_identquote_literal,了解可能适合您需求的引用技巧。

否则,您可能需要以下内容:

SELECT * from test_insert( E'{1,ab\'c,pqr}'::TEXT[] );

【讨论】:

  • 您现在应该强烈更喜欢使用带有%I%L 说明符的format 函数,而不是quote_literalquote_ident。但是,这里没有动态 SQL (EXECUTE),所以不需要引用。
  • 这也不是问题。跟手续没关系。用户只是不明白如何转义引号。
  • 是的。我再次在前往目的地的路上提供了答案,但没有阅读问题(也许我不应该),但我的答案的第二部分提供了服务。 -- 不熟悉 %* 占位符。我知道报价功能是向后兼容的,但我必须研究一下 - 谢谢
  • 无法在存储过程文本数组中插入引号
  • 在外面使用双引号是错误的,你有没有看到答案中关于`E'ab\'c'的部分
猜你喜欢
  • 2019-02-15
  • 2012-08-05
  • 1970-01-01
  • 1970-01-01
  • 2016-08-26
  • 2012-09-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多