python 和 postgresql 中的前导零 insert/update 问题
leading zero insert/update issue in python and postgresql
我正在尝试将 empid 插入到我的 Postgres table 中,这里的问题是我发现在所有情况下都缺少前导零。我怎样才能保留它?
我的python代码:
sql = "insert into emp (empid,name,sal) values (%s,%s,%s)"
data = (05599,xyz,10000)
cur.execute(sql, data)
Postgresql 列数据类型
empid --> character varying(5)
在 table 中我只看到 5599 而不是 05599。当 inserting/updating 数据进入 table 时如何保留前导零?
谢谢
不确定您是如何使用该代码的:
create table emp (empid varchar ,name varchar, sal numeric);
import psycopg2
con = psycopg2.connect("dbname=test host=localhost user=aklaver")
cur = con.cursor()
sql = "insert into emp (empid,name,sal) values (%s,%s,%s)"
data = (05599,xyz,10000)
^
SyntaxError: invalid token
# This works
data = ('05599', 'xyz' ,10000)
cur.execute(sql, data)
con.commit()
select * from emp;
empid | name | sal
-------+------+-------
05599 | xyz | 10000
所以@a_horse_with_no_name 建议以字符串形式传入。
我正在尝试将 empid 插入到我的 Postgres table 中,这里的问题是我发现在所有情况下都缺少前导零。我怎样才能保留它?
我的python代码:
sql = "insert into emp (empid,name,sal) values (%s,%s,%s)"
data = (05599,xyz,10000)
cur.execute(sql, data)
Postgresql 列数据类型
empid --> character varying(5)
在 table 中我只看到 5599 而不是 05599。当 inserting/updating 数据进入 table 时如何保留前导零?
谢谢
不确定您是如何使用该代码的:
create table emp (empid varchar ,name varchar, sal numeric);
import psycopg2
con = psycopg2.connect("dbname=test host=localhost user=aklaver")
cur = con.cursor()
sql = "insert into emp (empid,name,sal) values (%s,%s,%s)"
data = (05599,xyz,10000)
^
SyntaxError: invalid token
# This works
data = ('05599', 'xyz' ,10000)
cur.execute(sql, data)
con.commit()
select * from emp;
empid | name | sal
-------+------+-------
05599 | xyz | 10000
所以@a_horse_with_no_name 建议以字符串形式传入。