如何将表从numpy插入到Mysql中

时间:2011-09-13 18:18:43

标签: python mysql numpy

我无法在此代码中使用插件。 我有其他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 shell中使用“INSERT INTO test._t VALUES (1, 25, 28, 31)”工作正常。现在有很多冗余代码。想法是使用executemany,但我不能 简单的代码。

1 个答案:

答案 0 :(得分:2)

您的提交遗失了一些问题:

cxn.commit

# Should be:
cxn.commit()

从解释器运行,cxn.commit没有parens只返回对commit()方法的引用:

# Something like
>>> conn.commit
<built-in method commit of Connection object at 0x9cfe13c>
相关问题