我正在使用Python将数据从Mysql DB移动到PostgresDB。我的代码如下所示 -
conn = psycopg2.connect("dbname='aaa' user='aaa' host='localhost' password='aaa' ")
curp = conn.cursor()
db = MySQLdb.connect(host="127.0.0.1", user="root", passwd="root" , unix_socket='/var/mysql/mysql.sock', port=3306 )
cur.execute('select * from aaa_pledge')
list = cur.fetchall()
for l in list:
print l
for i in range(len(l)):
print i, l[i]
qry = "insert into aaa_pledge values ('%s','%s','%s','%s',%s,%s,'%s',%s,%s,%s,%s,'%s')" %( l[0], l[1], l[2], l[3], l[4], l[5], l[6], str(l[7]) , l[8], l[9], l[10], l[11] )
print qry
res = curp.execute( qry)
print res
curp.execute( "commit")
curp.close()
cur.execute( "commit")
cur.close()
代码适用于 有效日期 并执行如下 -
('500055', 'NEERAJ SINGAL', datetime.date(2014, 12, 4), 'NEERAJ SINGAL', Decimal('57221712.00'), Decimal('25.2600000'), 'INVOCATION', datetime.date(2014, 12, 4), Decimal('0E-7'), Decimal('0E-7'), Decimal('0E-7'), '-')
0 500055
1 NEERAJ SINGAL
2 2014-12-04
3 NEERAJ SINGAL
4 57221712.00
5 25.2600000
6 INVOCATION
***7 2014-12-04***
8 0E-7
9 0E-7
10 0E-7
11 -
insert into aaa_pledge values ('500055','NEERAJ SINGAL','2014-12-04','NEERAJ SINGAL',57221712.00,25.2600000,'INVOCATION','2014-12-04',0E-7,0E-7,0E-7,'-')
但是当源有空值时,在元组中我们得到一个无
('500055', 'NEERAJ SINGAL', datetime.date(2014, 11, 26), 'NEERAJ SINGAL', Decimal('57394244.00'), Decimal('25.3300000'), 'INVOCATION', ***None,*** Decimal('0E-7'), Decimal('0E-7'), Decimal('0E-7'), '-')
0 500055
1 NEERAJ SINGAL
2 2014-11-26
3 NEERAJ SINGAL
4 57394244.00
5 25.3300000
6 INVOCATION
***7 None***
8 0E-7
9 0E-7
10 0E-7
11 -
insert into aaa_pledge values ('500055','NEERAJ SINGAL','2014-11-26','NEERAJ SINGAL',57394244.00,25.3300000,'INVOCATION','None',0E-7,0E-7,0E-7,'-')
Traceback (most recent call last):
File "pransfer_data.py", line 27, in <module>
res = curp.execute( qry)
psycopg2.DataError: invalid input syntax for type date: "None"
LINE 1: ...EERAJ SINGAL',57394244.00,25.3300000,'INVOCATION','None',0E-...
我读了一些关于使用%s而不是'%s'以及使用str(l [7])的注释。但是,在每种情况下,如果代码对于有效日期运行正常,则对于无效日期,它会失败,反之亦然。我也将面临其他整数值的问题。
有没有办法
PS - 我有很多日期和数字cols,这将是必需的。我不能为每个列做一个if if else。
谢谢, 和Manish
答案 0 :(得分:5)
您需要做的是调用psycopg2的cursor.execute
方法的parameterized form - 传递查询(使用%s
占位符,不使用进行内插马上)作为execute
的第一个参数的字符串,然后传入一个包含参数值作为第二个参数的元组。类似的东西:
insertQuery = 'INSERT INTO foo VALUES (%s, %s, %s)'
cursor.execute(insertQuery, (1, 'Alice', datetime.date(2014, 12, 4)))
考虑以下愚蠢的测试表:
rchang=> \d foo
Table "public.foo"
Column | Type | Modifiers
--------+-----------------------------+-----------
id | integer |
name | text |
ts | timestamp without time zone |
使用以下Python代码执行插入:
insertQuery = 'INSERT INTO foo VALUES (%s, %s, %s)'
conn = psycopg2.connect(connString)
cursor = conn.cursor()
cursor.execute(insertQuery, (1, 'Alice', datetime.date(2014, 12, 4)))
cursor.execute(insertQuery, (2, 'Bob', None))
conn.commit()
conn.close()
然后我们在数据库中得到以下结果:
rchang=> select * from foo;
id | name | ts
----+-------+---------------------
1 | Alice | 2014-12-04 00:00:00
2 | Bob |
(2 rows)
请注意,使用参数化版本时,None
会作为空值正确插入到PostgreSQL中。
答案 1 :(得分:1)
你需要一个NULLIF和一个CAST:
CREATE TABLE foo(bar date);
INSERT INTO foo(bar) VALUES('2015-01-01');
INSERT INTO foo(bar) VALUES('None'); -- fails
INSERT INTO foo(bar) VALUES(CAST(NULLIF('None', 'None') AS DATE)); -- works fine
INSERT INTO foo(bar) VALUES(CAST(NULLIF('2015-01-01', 'None') AS DATE)); -- works fine as well