【问题标题】:Insert "$$" in text column, POSTGRESQL在文本列中插入“$$”,POSTGRESQL
【发布时间】:2022-07-02 09:08:57
【问题描述】:

我目前在我的 postgresql 查询中遇到了一个在文本列中插入字符串的奇怪问题,我会解释一下:

我有一个具有以下架构的表:

 CREATE TABLE IF NOT EXISTS template_formula
            (
                ID SERIAL PRIMARY KEY ,
                formula VARCHAR(500) DEFAULT NULL,
                display VARCHAR(500) DEFAULT NULL
);

此表将包含一个公式名称和一个包含降价的显示字符串。

我的插入查询如下:

DO $$
BEGIN 
    BEGIN
        --- Insert Template Formula 
         INSERT INTO template_formula(id,formula,display) VALUES 
         (7,'1000*(sin(deg2rad($A)))-(init($A)','$\textrm{Calcul IPI décrit comme :}$ $$R = 1000 \cdot (sin(degr2rad(A_i)))-A_0  $$ $\textrm{où :}$ $$ \textrm{$A_i$} = \textrm{Valeur courante de lecture} $$ $$ \textrm{$A_0$} = \textrm{Valeur initiale} $$')
    END;
    COMMIT;  
END;
$$  

当我尝试在 DBeaver 上执行此查询时,出现以下错误:

SQL Error [42601]: Unterminated dollar quote started at position 290 in SQL DO $$

这个错误是由于插入到显示列的字符串中的“$$”:

$$R = 1000 

你知道如何将这两个字符转义为字符串吗?

【问题讨论】:

    标签: postgresql insert


    【解决方案1】:

    为 DO 块使用其他分隔符:

    DO $do$
    BEGIN 
        BEGIN
            --- Insert Template Formula 
             INSERT INTO template_formula(id,formula,display) VALUES 
             (7,'1000*(sin(deg2rad($A)))-(init($A)','$\textrm{Calcul IPI décrit comme :}$ $$R = 1000 \cdot (sin(degr2rad(A_i)))-A_0  $$ $\textrm{où :}$ $$ \textrm{$A_i$} = \textrm{Valeur courante de lecture} $$ $$ \textrm{$A_0$} = \textrm{Valeur initiale} $$');
        END;
        COMMIT;  
    END;
    $do$;
    

    或者彻底摆脱无用的 DO 块:

    BEGIN TRANSACTION;
    INSERT INTO template_formula(id,formula,display) VALUES 
    (7,'1000*(sin(deg2rad($A)))-(init($A)','$\textrm{Calcul IPI décrit comme :}$ $$R = 1000 \cdot (sin(degr2rad(A_i)))-A_0  $$ $\textrm{où :}$ $$ \textrm{$A_i$} = \textrm{Valeur courante de lecture} $$ $$ \textrm{$A_0$} = \textrm{Valeur initiale} $$');
    COMMIT;  
    

    (您还忘记了 ; 来结束 INSERT 语句)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-01
      • 2012-02-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多