Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/72.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:将2D数组插入MySQL表_Python_Mysql_Arrays_Python 2.7 - Fatal编程技术网

Python:将2D数组插入MySQL表

Python:将2D数组插入MySQL表,python,mysql,arrays,python-2.7,Python,Mysql,Arrays,Python 2.7,我的数据库名为:test;表名:cord; 名为:id的行;x;Yz 假设我有这样的2d阵列: asd = [[1.25,2.45,3.65],[2.78,3.59,1.58]......] 如果有更多aray元素,我想将asd[0][0]元素插入x行,将asd[0][1]插入y行,将asd[0][2]插入z行,将asd[1][0]插入x行,将asd[1][1]插入y行,将asd[1][2]插入z行等等 到目前为止,我的代码(我知道插入函数只定义x): 您可以通过executemany()“一

我的数据库名为:test;表名:cord; 名为:id的行;x;Yz

假设我有这样的2d阵列:

asd = [[1.25,2.45,3.65],[2.78,3.59,1.58]......]
如果有更多aray元素,我想将asd[0][0]元素插入x行,将asd[0][1]插入y行,将asd[0][2]插入z行,将asd[1][0]插入x行,将asd[1][1]插入y行,将asd[1][2]插入z行等等

到目前为止,我的代码(我知道插入函数只定义x):

您可以通过
executemany()
“一次性”完成此操作:

import MySQLdb

# Open database connection
db = MySQLdb.connect(host="localhost",port= 3307,user="root",passwd="usbw" , db = "test")

# prepare a cursor object using cursor() method
cur = db.cursor()

# Create table as per requirement
 asd = [[1.25,2.45,3.65],[2.78,3.59,1.58]]
 for x in asd:
 cur.execute("INSERT INTO cord(x) VALUES(%s)",x)
 db.commit()

# disconnect from server
db.close()
cur.executemany("""
    INSERT INTO 
        cord
        (x, y, z)
    VALUES
        (%s, %s, %s)
""", asd)
db.commit()