pgSQL:我试图使用dblink_exec()方法插入表中,但是出现以下错误

时间:2014-03-15 10:13:40

标签: postgresql insert special-characters dblink stringescapeutils

select dblink_exec('hostaddr=59.89.210.56 port=5432 dbname=mydb user=postgres password=****','insert into gtab04 (productid,product,itgrid,itemtype,patentid,taxid,convfact,boxpack,cntid )values (2301,''D PEARL 2000 Caps 10's'',4,2,7,3,10,'''',0)')

而exe此查询出现以下错误

ERROR:  syntax error at or near "s"
LINE 2: ...oxpack,cntid )values (2301,''D PEARL 2000 Caps 10's'',4,2,7,..

问题在于产品名称,即我的产品名称是D PEARL 2000 Caps 10's(我认为'问题出现') 查询将此作为''D PEARL 2000 Caps 10''< - 这是问题

3 个答案:

答案 0 :(得分:0)

Caps 10's应为上限10''''s

您将dblink的单引号加倍,并且第二次在字符串内加倍。

答案 1 :(得分:0)

我希望这会对你有所帮助,

create or replace function create_or_update_test_employee_table() returns trigger as $_$
declare
    query text;
    fname text;
    lname text;
    id integer;
    created_at timestamp;
begin
    fname = NEW.first_name;
    lname = NEW.last_name;

    IF (TG_OP = 'INSERT') THEN
        id = NEW.id;
        query = 'insert into test_employee(id, first_name, last_name) values(''' || id || ''',''' || fname || ''',''' || lname || ''')';
        PERFORM dblink_exec('dbname=test_db',query);
        RETURN NEW;
    END IF;

    IF (TG_OP = 'UPDATE') THEN
        id = OLD.id;
        query = 'update test_employee set first_name = ''' || fname || ''', last_name = ''' || lname || ''' WHERE id = ' || id ||'';
        PERFORM dblink_exec('dbname=test_db',query);
        RETURN NEW;
    END IF;

    IF(TG_OP = 'DELETE') THEN
        PERFORM dblink_exec('dbname=test_db','DELETE FROM test_employee WHERE id = ' || OLD.id);
        RETURN OLD;
    END IF;

end $_$ language 'plpgsql';

答案 2 :(得分:-1)

逃避单引号

D PEARL 2000 Caps 10\'\'s