Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python将数组添加到数据库_Python_Mysql_Numpy - Fatal编程技术网

python将数组添加到数据库

python将数组添加到数据库,python,mysql,numpy,Python,Mysql,Numpy,我正在尝试使用h5py从H5文件中获取数组,并将它们写入MySQL数据库。 我写了这段代码: #!/usr/bin/env python import sys, os, h5py, MySQLdb, numpy def initial(FileName): f = h5py.File(str(FileName), 'r') ch1 = f[str("dset1")][:] ch2 = f[str("dset2")][:] ch3 = f[str("dset3"

我正在尝试使用h5py从H5文件中获取数组,并将它们写入MySQL数据库。 我写了这段代码:

#!/usr/bin/env python
import sys, os, h5py, MySQLdb, numpy

def initial(FileName):
    f = h5py.File(str(FileName), 'r')

    ch1 = f[str("dset1")][:]
    ch2 = f[str("dset2")][:]
    ch3 = f[str("dset3")][:]
    ch4 = f[str("dset4")][:]
    ch5 = f[str("dset5")][:]
    ch6 = f[str("dset6")][:]
    ch7 = f[str("dset7")][:]
    ch8 = f[str("dset8")][:]
    ch9 = f[str("dset9")][:]
    ch10 = f[str("dset10")][:]
    ch11 = f[str("dset11")][:]
    ch12 = f[str("dset12")][:]
    ch13 = f[str("dset13")][:]
    ch14 = f[str("dset14")][:]
    ch15 = f[str("dset15")][:]

    print(numpy.shape(ch1))

    db = MySQLdb.connect(host="localhost", user="myUSR", passwd="myPasswd", db = "myDB")
    cur = db.cursor()
    cur.execute("INSERT INTO myTable (FileName, date, ch1, ch2, ch3, ch4, ch4r, ch5, ch6, ch7, ch8, ch9, ch10, ch11, ch12, Slot, Info) values ( " +
    "\"" + str(FileName) + "\"" +\
    ",\"" +"Bu aralar" + "\"" +\
    ",\"" + ch1 + "\"" +\
    ",\"" + ch2 +  "\"" +\
    ",\"" + ch3 +  "\"" +\
    ",\"" + ch4 + "\"" +\
    ",\"" + ch5 +  "\"" +\
    ",\"" + ch6 +  "\"" +\
    ",\"" + ch7 +  "\"" +\
    ",\"" + ch8 +  "\"" +\
    ",\"" + ch9 +  "\"" +\
    ",\"" + ch10 +  "\"" +\
    ",\"" + ch11 +  "\"" +\
    ",\"" + ch12 +  "\"" +\
    ",\"" + ch13 +  "\"" +\
    ",\"" + ch14 +  "\"" +\
    ",\"" + ch15 +  "\"" + " ) ")
    db.commit()

initial(sys.argv[1])
然而,有一个错误告诉我不能将这样的数据写入数据库。因此,我尝试为每个数据设置srt(),如下所示:

    .
    .
    .
    ",\"" + str(ch4) + "\"" +\
    ",\"" + str(ch5) +  "\"" +\
    ",\"" + str(ch6) +  "\"" +\
    ",\"" + str(ch7) +  "\"" +\
    ",\"" + str(ch8) +  "\"" +\
    ",\"" + str(ch9) +  "\"" +\
    .
    .
    .
但它破坏了我的数据。这就是我的阵列在数据库中的外观

[[  0   0   0 ...,   0   0   0]
 [  0   0   0 ...,   0   0   0]
 [  0   0   0 ...,   0   0   0]
 ..., 
 [500 432 472 ...,  46  46  32]
 [309 231 325 ...,  47  47  40]
 [187 236 363 ...,  44  39  39]]

所以,您能告诉我一种将数组写入数据库的方法吗?

将数组转储到
pickle
json
。这是一种更好的方法,而不是
str()

我无法得到确切的错误是什么,但我建议使用string.format()来避免与引号混淆
query='insert into table_name(FileName,date,…)值(“{0}”,“{1}”,“…)。format([str(FileName),ch1,…])
然后
cur execute(query)