使用Python从数组在MySQL中插入多个值

使用Python从数组在MySQL中插入多个值,mysql,python-3.x,Mysql,Python 3.x,我正在尝试将数组中的数据插入MySQL。 令我大为惊讶的是,如果您对数组执行for循环,那么如何执行该操作的示例并不多,我发现的每个示例都来自已经存在的数组列表 感谢下面的Adrian,我们注意到我的列表需要元组 更新代码 connection = mysql.connector.connect( host='localhost', database='test', user='root', password='pass' ) query = &qu

我正在尝试将数组中的数据插入MySQL。 令我大为惊讶的是,如果您对数组执行for循环,那么如何执行该操作的示例并不多,我发现的每个示例都来自已经存在的数组列表

感谢下面的Adrian,我们注意到我的列表需要元组

更新代码

connection = mysql.connector.connect(
    host='localhost', 
    database='test',
    user='root', 
    password='pass'
    )

query = "INSERT INTO blue (created, published, publisher) VALUES (%s, %s, %s)"

array = []
# The idea here is to get all table rows in the page so you can group the values into rows that are going to be added to MySQL
tr = soup.find_all('tr')
for table_row in tr:
    row_data = table_row.find_all('td')
    insert_row = []
    for data in row_data:
        data = re.sub('<[^>]*>', '', str(data))
        insert_row.append(data)
    array.append(tuple(insert_row))
print(array)

cursor = connection.cursor()
cursor.executemany(query, array)
cursor.commit()
connection=mysql.connector.connect(
host='localhost',
数据库='test',
user='root',
密码class='pass'
)
query=“插入蓝色(已创建、已发布、发布者)值(%s,%s,%s)”
数组=[]
#这里的想法是获取页面中的所有表行,以便您可以将值分组到要添加到MySQL的行中
tr=汤。查找所有('tr')
对于tr中的表_行:
行数据=表行。查找所有('td'))
插入_行=[]
对于第_行数据中的数据:
data=re.sub(']*>,'',str(数据))
插入\行。追加(数据)
array.append(元组(插入_行))
打印(数组)
cursor=connection.cursor()
cursor.executemany(查询,数组)
cursor.commit()
正在接近,但目前我收到以下消息

索引器错误:元组索引超出范围

mysql.connector.errors.ProgrammingError:SQL语句的参数不够


提前谢谢

我认为你是在混合两种解决问题的方法

一种方法是使用中所述的ExecuteMay方法

query=“插入蓝色(已创建、已发布、发布者)值(%s,%s,%s)”
数组=[]
#这里的想法是获取页面中的所有表行,以便
#可以将这些值分组到将要添加到MySQL的行中
tr=汤。查找所有('tr')
对于tr中的表_行:
行数据=表行。查找所有('td'))
插入_行=[无,无,无]
对于范围内的idx(len(行_数据)):
如果行_数据[idx]和idx<3:
data=re.sub(']*>','',str(行数据[idx]))
如果数据:
插入_行[idx]=数据
array.append(元组(插入_行))
cursor=connection.cursor()
cursor.executemany(查询,数组)
cursor.commit()
另一种方法是自己构建查询

query = "INSERT INTO blues (created, published, publisher) VALUES "
array = []
# The idea here is to get all table rows in the page so you can group the values into rows that are going to be added to MySQL
tr = soup.find_all('tr')
for table_row in tr:
    row_data = table_row.find_all('td')
    insert_row = []
    for data in row_data:
        data = re.sub('<[^>]*>', '', str(data))
        insert_row.append(data)
    array.append(tuple(insert_row))

values = []
for item in array:
    row = [None, None, None]
    for idx in range(len(item)):
        row[idx] = item[idx]
    values.append(str(tuple(row)))

query += ",".join(values)
cursor = connection.cursor()
cursor.execute(query)
cursor.commit()
query=“插入蓝色(已创建、已发布、发布者)值”
数组=[]
#这里的想法是获取页面中的所有表行,以便您可以将值分组到要添加到MySQL的行中
tr=汤。查找所有('tr')
对于tr中的表_行:
行数据=表行。查找所有('td'))
插入_行=[]
对于第_行数据中的数据:
data=re.sub(']*>,'',str(数据))
插入\行。追加(数据)
array.append(元组(插入_行))
值=[]
对于数组中的项:
行=[无,无,无]
对于范围内的idx(长度(项目)):
行[idx]=项[idx]
values.append(str(元组(行)))
查询+=“,”.join(值)
cursor=connection.cursor()
cursor.execute(查询)
cursor.commit()

希望这有助于…

我认为您正在混合两种解决问题的方法

一种方法是使用中所述的ExecuteMay方法

query=“插入蓝色(已创建、已发布、发布者)值(%s,%s,%s)”
数组=[]
#这里的想法是获取页面中的所有表行,以便
#可以将这些值分组到将要添加到MySQL的行中
tr=汤。查找所有('tr')
对于tr中的表_行:
行数据=表行。查找所有('td'))
插入_行=[无,无,无]
对于范围内的idx(len(行_数据)):
如果行_数据[idx]和idx<3:
data=re.sub(']*>','',str(行数据[idx]))
如果数据:
插入_行[idx]=数据
array.append(元组(插入_行))
cursor=connection.cursor()
cursor.executemany(查询,数组)
cursor.commit()
另一种方法是自己构建查询

query = "INSERT INTO blues (created, published, publisher) VALUES "
array = []
# The idea here is to get all table rows in the page so you can group the values into rows that are going to be added to MySQL
tr = soup.find_all('tr')
for table_row in tr:
    row_data = table_row.find_all('td')
    insert_row = []
    for data in row_data:
        data = re.sub('<[^>]*>', '', str(data))
        insert_row.append(data)
    array.append(tuple(insert_row))

values = []
for item in array:
    row = [None, None, None]
    for idx in range(len(item)):
        row[idx] = item[idx]
    values.append(str(tuple(row)))

query += ",".join(values)
cursor = connection.cursor()
cursor.execute(query)
cursor.commit()
query=“插入蓝色(已创建、已发布、发布者)值”
数组=[]
#这里的想法是获取页面中的所有表行,以便您可以将值分组到要添加到MySQL的行中
tr=汤。查找所有('tr')
对于tr中的表_行:
行数据=表行。查找所有('td'))
插入_行=[]
对于第_行数据中的数据:
data=re.sub(']*>,'',str(数据))
插入\行。追加(数据)
array.append(元组(插入_行))
值=[]
对于数组中的项:
行=[无,无,无]
对于范围内的idx(长度(项目)):
行[idx]=项[idx]
values.append(str(元组(行)))
查询+=“,”.join(值)
cursor=connection.cursor()
cursor.execute(查询)
cursor.commit()

希望这有帮助…

数组是一个列表列表,或者只是一个字符串列表,因为它看起来就像是在一个字符串列表上迭代,并且每个字符串都试图执行一个
executemany
,它需要3个参数,但你只传递1Hmm,你可能会得到一些东西,我相信是字符串列表。所以它可能认为我迭代的所有内容都是一个大字符串?如果
len(array)==3
,那么你不需要一个
for循环
,只需执行
游标即可。execute(query,array)
应该工作数组包含的数据比3多,我在数据库中的三列将包含很多行。然后你需要
array
成为一个列表列表,其中每个内部列表的长度为3数组是一个列表列表还是一个字符串列表,因为看起来你只是在一个字符串列表上进行迭代,每个字符串都试图执行一个
executemany
,它需要3个参数,但你只传递1Hmm,你可能会得到一些东西,我相信字符串列表。所以它可能认为我迭代的所有内容都是一个大字符串?如果
len(array)==3
,那么你不需要一个
for循环
,只需执行
游标即可。execute(query,array)
应该工作数组包含的数据比3多,我在数据库中的三列将包含许多行。然后你需要
array
成为一个列表列表,其中每个内部列表的长度为3行。例如,两个问题!在示例1中,您拥有来自数组的数据。但是我正在从一个数据库中检索数据