我在postgres数据库中创建了一个表:
CREATE TABLE "DataSource"(
src_id character varying(20) NOT NULL,
src_name character varying(20),
password character varying(25),
host character varying(30),
created_date date,
user_name character varying(30),
src_type character varying(15),
db_name character varying(20) NOT NULL,
port numeric(32,0),
CONSTRAINT "DataSource_pkey" PRIMARY KEY (src_id, db_name)
)
还在python中创建了一个方法,用于插入上表:
def insertDataSource(srcName, srcType ,owner, src_password, location, creationDate,db_name,port_no='DEFAULT'):
srcId=idGeneratorforData(srcName)# idGeneratorforData() method generate a id string value
conn = get_connect()
if conn:
cur = conn.cursor()#cursor_factory=psycopg2.extras.RealDictCursor)
else:
print "Failed to connect..."
cur.execute('''insert into "DataSource"("src_id","user_name","src_name","created_date","src_type","password","host", "db_name", "port") values ('%s','%s','%s','%s','%s','%s','%s','%s')''' %(srcId,owner,srcName,creationDate,srcType, src_password, location, db_name, port_no))
conn.commit()
cur.close()
conn.close()
现在,当我尝试将此方法称为
时insertDataSource("postgres", "Structured", "postgres", "root", "localhost", "12-10-2015", "postgres", "5432")
然后抛出TypeError ::
cur.execute('''insert into "DataSource"("src_id","user_name","src_name","created_date","src_type","password","host", "db_name", "port") values ('%s','%s','%s','%s','%s','%s','%s','%s')''' %(srcId,owner,srcName,creationDate,srcType, src_password, location, db_name, port_no))
TypeError:并非在字符串格式化期间转换所有参数
我还尝试传递端口号的DEFAULT值,代码仍然抛出相同的错误,但是当我通过SQL提示触发了相同的查询时..它可以工作。
我无法理解为什么会出现这个问题......,相同的代码在今天之前工作正常......