- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要编写一个 INSERT 语句,首先检查数据是否已经存在。当前代码在 python 中使用 psycopg2 连接到 postgresql 数据库:
sql = """IF NOT EXISTS (SELECT * FROM table \
WHERE col_1 = (%s) AND col_2 = (%s) ) \
INSERT INTO table (col1, col2) \
VALUES (%s, %s);"""
data = ( col1_data, col2_data, col1_data, col2_data)
try:
CURSOR.execute(sql, data)
DB.commit()
except:
print "Cursor failed INSERT INTO table.\n"
这是行不通的(而且我还没有进行质量错误处理,所以我没有得到任何有用的信息)。所以,我进入 psql 并尝试了:
IF NOT EXISTS (SELECT * FROM t WHERE c1=d1 AND c2=d2)
INSERT INTO t (c1, c2) VALUES (d1,d2);
我得到了以下错误:
ERROR: syntax error at or near "IF"
LINE 1: IF NOT EXISTS (SELECT * FROM table WHERE c1 = d1...
^
所以我相信我的错误是在 sql 而不是 python 中(尽管我可能是错的)因为这个有效:
sql = """INSERT INTO t2 (col_0, col_1, col_2) \
VALUES (%s, %s, %s);"""
data = (d1, d2, time.time())
try:
CURSOR.execute(sql, data)
DB.commit()
except:
print "Cursor failed to INSERT INTO t2.\n"
对于表 1,我的 CREATE 是:
db=> CREATE TABLE table (
col_0 SERIAL PRIMARY KEY,
col_1 varchar(16),
col_2 smallint
);
NOTICE: CREATE TABLE will create implicit sequence "pm_table_ip_id_seq" for serial column "pm_table.ip_id"
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "pm_table_pkey" for table "pm_table"
CREATE TABLE
非常感谢任何帮助和指导。
最佳答案
我用了plpgsql在我的项目中有这样的需求
insert_function = """
CREATE LANGUAGE plpgsql;
CREATE FUNCTION insert_if_unique (sql_insert TEXT)
RETURNS VOID
LANGUAGE plpgsql
AS $$
BEGIN
EXECUTE sql_insert;
RETURN;
EXCEPTION WHEN unique_violation THEN
RETURN;
-- do nothing
END;
$$;
"""
cursor.execute(insert_function);
你可以像下面这样使用它:
cursor.execute("insert_if_unique("+sql+")"%data)
上面的查询没有参数化。因此,如果您从外部源获取输入,请警惕 SQL 注入(inject)。
注意:您可以使用cursor.mogrify()
来规避SQL 注入(inject)攻击。
sql = cursor.mogrify(sql,data)
cursor.execute("insert_if_unique("+sql+")")
关于python psycopg2 条件插入语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9895326/
目前,我正在尝试配置 Django 以在项目中使用,并且在尝试运行 python manage.py syncdb 时遇到持续错误。 File "/x/x/x/x/x/x/base.py", line
sqlite 的 Python db-api 实现有一个方便的方法 executescript() 来执行多语句 SQL 脚本。它对于创建数据库非常有用。参见 sqlite driver docume
我有一个时间戳,我正在尝试使用 psycopg 将其插入到 postgres 中,但我无法解决问题,我认为这是因为我正在尝试使用 python 的字符串格式将日期放入正确的位置。我有这样的东西 val
我试图生成一些有效的 Postgresql 语句,但是 cursor.execute 方法以一种奇怪的方式解释我的参数。 这是我的代码切割: for key in data_dict.keys():
我在尝试插入数据库时遇到了问题: ur_psql.execute("""insert into smth(data, filedate, filedby)""" "
已结束。此问题不符合 Stack Overflow guidelines .它目前不接受答案。 关闭 9 年前。 有关您编写的代码问题的问题必须在问题本身中描述具体问题——并包含有效代码以重现它。见
我们使用一个对象来保持与 PostgreSQL 数据库的连接并创建新的游标来处理请求。我观察到奇怪的行为:即使读取响应并关闭游标,请求仍然卡在数据库中,阻止更新表等。 当连接关闭时,它就消失了。 我了
我在更改我的 postgres 数据库中的表时遇到了一些问题。我正在使用 psycopg2 并使用 Python。我试图添加一个串行主键。它花了很长时间(大表),并且没有抛出任何错误,所以它做了一些事
当使用 psycopg 连接到 postgresql 数据库时,我拉了网线,没有出现任何错误。我如何在代码中检测到这一点以通知用户? 最佳答案 psycopg 无法检测到网络发生了什么。例如,如果您拔
我不断收到这个 error: psycopg2.ProgrammingError: column "someentry" does not exist. 当 someentry 不是列时,错误表明 s
我是 python 的初学者。我们使用这段代码来执行 SQL 命令。 cur.execute("INSERT INTO test (num, data) VALUES (%s, %s)", (100,
我正在处理 Udacity 的在线项目。我正在使用他们配置的 vagrant 来运行包含数据库的服务器。不幸的是,当我试图赋予代码持久性时,服务器每次都会返回错误。我是 python 的新手,所以请原
我用psycopg2创建数据库失败,语法错误,但在前几行使用相同的语法是行得通的,为什么要问?两行代码,为什么是第二行语法错误? [代码] db_name = 'series_id' self._cu
这个问题在这里已经有了答案: ValueError: operation parameter must be str or unicode (1 个回答) 关闭 4 年前。 我最近修改了我的插入查询
如何修复 Python 中的 SQL 语句? 数据库连接有效。但是,cur.execute 返回 none,这是错误的。 我的代码 import os, pg, sys, re, psycopg2 t
我正在尝试运行这样的代码: query = "copy (select email from my_table) TO 'STDOUT' WITH (FORMAT csv, DELIMITER '|
在 OSX Mojave 上安装了 Python3 和 Postgres 11.3,运行 pip install psycopg2 并收到以下冗长的错误消息。 据我所知,满足 psycopg 的要求,
有没有办法让 psycopg 和 postgres 无需重新建立连接就可以处理错误,比如 MySQLdb?下面的注释版本适用于 MySQLdb,注释使其适用于 Psycopg2: results =
我收到了一个来自野外的 unicode 字符串,它导致我们的一些 psycopg2 语句失败。 我已将问题简化为 SSCE: import psycopg2 conn = psycopg2.conne
我正在尝试在 Windows(Windows 7、64 位)下安装 psycopg2。我正在使用 Python 2.7.2 from Python(x,y)和 PostgreSQL 9.2.1。 我的
我是一名优秀的程序员,十分优秀!