我正在尝试将长xml字符串作为文本批量插入到postgresql 9.1数据库中。我使用的是Python 3.2和pyscopg2。我在$$中包含xml字符串并在查询字符串中使用命名变量。例如:
query = "insert into cms_object_metadata (cms_object_id, cms_object_metadata_data, cms_object_metadata_type_id, \
cms_object_metadata_status_id) values ((select id from cms_objects where cms_object_ident = %(objIdent)s), \
$$%(objMetaString)s$$, (select id from cms_object_metadata_types where cms_object_metadata_type_name = 'PDAT'), \
(select id from cms_object_metadata_status where cms_object_metadata_status_name = 'active'))"
然后我按如下方式构造一个字典对象:
dataDict = {'objIdent':objIdent, 'objMetaString':objMetaString}
传入objIdent和objMetaString值。我使用以下代码进行插入:
dbCursor.execute(query, dataDict)
当它将objMetaString值插入数据库时,它包含字符串周围的单引号。如果我将值附加到查询字符串中并执行不带命名变量的插入,则不会。例如:
query = "insert into cms_object_metadata (cms_object_id, cms_object_metadata_data, cms_object_metadata_type_id, \
cms_object_metadata_status_id) values ((select id from cms_objects where cms_object_ident = %s), \
$$%s$$, (select id from cms_object_metadata_types where cms_object_metadata_type_name = 'PDAT'), \
(select id from cms_object_metadata_status where cms_object_metadata_status_name = 'active'))" % (objIdent, objMetaString)
和插入:
dbCursor.execute(query)
我的问题是如何使用命名变量和$$批量插入大型文本数据。如果可能的话,我真的不想要预处理或后处理这个字符串,因为它们可能很大并且包含未知数量的单引号或其他需要分隔的符号。 我已阅读以下文档并在stackoverflow上搜索了答案,但未找到解决方案:
答案 0 :(得分:1)
总结评论主题。这样做:
query = "insert into cms_object_metadata (cms_object_id, cms_object_metadata_data, cms_object_metadata_type_id, \
cms_object_metadata_status_id) values ((select id from cms_objects where cms_object_ident = %(objIdent)s), \
%(objMetaString)s, (select id from cms_object_metadata_types where cms_object_metadata_type_name = 'PDAT'), \
(select id from cms_object_metadata_status where cms_object_metadata_status_name = 'active'))"
dataDict = {'objIdent':objIdent, 'objMetaString':objMetaString}
dbCursor.execute(query, dataDict)
不要在查询中的%(objMetaString)s
占位符周围添加引号。如有必要,引用该值是司机的工作。