由于双引号,无法更新postgres DB中的字典列吗?

时间:2019-12-18 05:23:54

标签: python sql postgresql psql

我可以通过两个CLI命令来更新相关列:

psql -U postgres
UPDATE taskt SET update= '[{"id": 0, "timestamp": "2019-12-17T22:16:28.985Z", "statusUpdate": "three little piggies"}]' WHERE uri = '/rest/tasks/xyz';

那很好,但是我要在python中运行命令并执行类似subprocess.check_output(cli_0 +“&” + cli_1,shell = True)的结果,导致我认为第二个是bash命令。理想情况下,我希望运行以下内容:

psql -U postgres -c "UPDATE taskt SET update= '[{"id": 0, "timestamp": "2019-12-17T22:16:28.985Z", "statusUpdate": "three little piggies"}]' WHERE uri = '/rest/tasks/xyz';"

但是字典中的双引号有点杀了这个主意。将它们变成单引号也不起作用。任何建议都将非常有用。我可以使用python3.5,但无法安装任何东西。

1 个答案:

答案 0 :(得分:1)

您应该可以反斜杠转义内部的双引号:

// Woocommerce - Removes category link from woocommerce product category widgets so they are not seen
add_filter( 'get_terms', 'get_subcategory_terms', 10, 3 );

function get_subcategory_terms( $terms, $taxonomies, $args ) {

  $new_terms = array();

  $category_to_check = get_term_by( 'slug', 'wholesale-category', 'product_cat' );

  // if a product category and on the shop page
  if ( in_array( 'product_cat', $taxonomies ) && ! ( current_user_can( 'wholesale_customer' ) || current_user_can( 'administrator' ) ) ) {

    foreach ( $terms as $key => $term ) {

      if ( ! in_array( $term->slug, array( 'wholesale-category' ) ) ) {
        $new_terms[] = $term;
      }

    }

    $terms = $new_terms;

  }

  return $terms;
}

否则,您可以保存到文件并运行它:

psql -U postgres -c "UPDATE taskt SET update= '[{\"id\": 0, \"timestamp\": \"2019-12-17T22:16:28.985Z\", \"statusUpdate\": \"three little piggies\"}]' WHERE uri = '/rest/tasks/xyz';"

您还应该可以直接在cat <<_EOF_ > /tmp/myfile > UPDATE taskt SET update= '[{"id": 0, "timestamp": "2019-12-17T22:16:28.985Z", "statusUpdate": "three little piggies"}]' WHERE uri = '/rest/tasks/xyz'; > _EOF_ psql -U postgres < /tmp/myfile 中做一个Heredoc:

psql