如何将numpy中的表插入MySQL
我在这段代码中无法让插入操作正常工作。虽然我有其他的 numpy 和 mysql 的代码运行得很好,但这段代码在运行时没有任何错误,mysql 也没有报错。我是在 winx64 上使用 python 2.7 和 pyodbc。
p = np.arange(1, 33, 3)
print p
def prow (f1,series):
rowp=[]
series = series.tolist()
ss = series[8:]
rowp.append(1)
rowp.extend(ss)
print len (rowp)
print rowp
print type(rowp)
InSQL = 'INSERT INTO test._t VALUES (1, 25, 28, 31)'
print InSQL
# csr.executemany(InSQL,rowp)
csr.execute(InSQL)
print 1234
cxn.commit
prow('foo', p)
下面是我的 MySql 代码:
CREATE TABLE `_t` (
`Id` int(11) DEFAULT NULL,
`T1` float(12,4) DEFAULT NULL,
`T2` decimal(12,4) DEFAULT NULL,
`T7` float(12,4) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1
这些打印语句的输出是:
[ 1 4 7 10 13 16 19 22 25 28 31]
4
[1, 25, 28, 31]
<type 'list'>
INSERT INTO test._t VALUES (1, 25, 28, 31)
1234
在 mysql 命令行中使用 "INSERT INTO test._t VALUES (1, 25, 28, 31)
" 可以正常工作。现在代码中有很多冗余的部分。我的想法是使用 executemany,但我无法让简单的代码正常工作。
1 个回答
2
你的提交缺少括号:
cxn.commit
# Should be:
cxn.commit()
在解释器中运行时,直接写 cxn.commit
而不加括号,实际上只是返回了 commit()
方法的一个引用:
# Something like
>>> conn.commit
<built-in method commit of Connection object at 0x9cfe13c>