我正在加密一些数据并尝试将其作为字节插入postgres。我正在使用psycopg2和python。当我尝试使用python时,它会给出一个关于变化角色的错误。
类型字符变化的错误值太长(30)
进入存在传递!
当我使用python中使用SQL语句的确切输出将其插入Postgresql时,它将起作用。我通过显示存储语句的变量使用eclipse控制台窗口获得了语句。这是PG控制台输出:
metering=# insert into customers(customer_id,customer_name, inactive, datetime) values (101,'\021;\213D\351O\0339"(($v_\033\262'::bytea,'Y', CURRENT_TIMESTAMP);
INSERT 0 1
^
metering=# select * from customers;
customer_id | customer_name | inactive | datetime
-------------+------------------------------------+----------+------------
101 | \x113b8b44e94f1b3922282824765f1bb2 | Y | 2014-10-06
(1 row)
数据库表定义和数据库格式
ENCODING = 'UTF8'
TABLESPACE = pg_default
LC_COLLATE = 'en_US.UTF-8'
LC_CTYPE = 'en_US.UTF-8'
CONNECTION LIMIT = -1;
customer_id bigint NOT NULL,
customer_name bytea NOT NULL,
inactive character varying(1) NOT NULL,
datetime date NOT NULL,
CONSTRAINT customers_pkey PRIMARY KEY (customer_id)
python返回的错误
2014-10-16 12:19:23,077 ERROR Error invalid byte sequence for encoding "UTF8": 0xca 0x3c
2014-10-16 12:20:47,796 ERROR Error invalid byte sequence for encoding "UTF8": 0xca 0x3c
插入加密客户名称的代码。
password = hashlib.sha256(secretmofokey).digest()
IV = 16 * '\x00'
mode = AES.MODE_CBC
encryptor = AES.new(password, mode, IV=IV)
for customer_id, customer_name, inactive in oracle_cursor:
try:
encrypted_customer_name = encryptor.encrypt(pad(customer_name))
pg_delete.execute("""delete from customers where customer_id = %s """ % customer_id)
customers_sql = ("""insert into customers(customer_id,
customer_name , inactive, datetime)
values (%s, '%s', '%s' , CURRENT_TIMESTAMP) """ % (customer_id, encrypted_customer_name, inactive))
pg_insert.execute(customers_sql)
postgres.commit()
except psycopg2.DatabaseError, e:
logging.error('Error %s' % e)
postgres.rollback()
continue
答案 0 :(得分:1)
Per the psycopg2 manual,您必须明确标识要插入为bytea的数据:
您也错误地尝试引用格式说明符。您不需要也不应该在%s
附近包含单引号; psycopg2将进行适当的引用。
所以你想要:
"""... values (%s, %s, %s , CURRENT_TIMESTAMP) """
% (customer_id, psycopg2.Binary(encrypted_customer_name), inactive)