【发布时间】:2014-10-12 12:24:43
【问题描述】:
请认为我是 psycopg2 的新手。我的目标是将 dtype 对象的 1D numpy 数组(其中元素只是字符串)插入到 postgresQL 表中。我的主程序将字段保存为 numpy 数组中的字符串。然后,我想将每个单独的元素添加到 postgresql 表中的一列(或者,如果您愿意,一维数组是一行)。请注意,实际数组有 36 个元素!我需要一个方法把它们都放进去。
我正在使用 cur.execute 命令,尽管我认为字符串转换存在一些问题。
Array=np.empty(3,dype=object)
Array[0]='Hello'
Array[1]='Tea?'
Array[2]='Bye'
statement= "INSERT INTO testlog (field1,field2,field3) VALUES (%s)" #Etc.
cur.execute(statement,Array)
我得到错误:
cur.execute(statement,Array)
TypeError: not all arguments converted during string formatting
也试过了:
cur.executemany('INSERT INTO testlog VALUES ( %s )', [[v] for v in Array ]
谢谢
【问题讨论】:
-
@Clodoaldo 你是对的,刚刚读到它here
标签: python mysql arrays database postgresql