Skip to main content
Stack Overflow
  1. About
  2. For Teams

Return to Question

Commonmark migration
Source Link

I am trying to insert data from my array into MySQL. To my big surprise there were not many examples on how to do it if you perform a for-loop for your array, every example that I have found was from an already existing array list.

Thanks to Adrian below, we noticed that I need tuples for my list.

Updated code

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()

Getting close but at the moment I receive the following

IndexError: Tuple index out of range

mysql.connector.errors.ProgrammingError: Not enough parameters for the SQL statement

Thanks in advance!

I am trying to insert data from my array into MySQL. To my big surprise there were not many examples on how to do it if you perform a for-loop for your array, every example that I have found was from an already existing array list.

Thanks to Adrian below, we noticed that I need tuples for my list.

Updated code

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()

Getting close but at the moment I receive the following

IndexError: Tuple index out of range

mysql.connector.errors.ProgrammingError: Not enough parameters for the SQL statement

Thanks in advance!

I am trying to insert data from my array into MySQL. To my big surprise there were not many examples on how to do it if you perform a for-loop for your array, every example that I have found was from an already existing array list.

Thanks to Adrian below, we noticed that I need tuples for my list.

Updated code

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()

Getting close but at the moment I receive the following

IndexError: Tuple index out of range

mysql.connector.errors.ProgrammingError: Not enough parameters for the SQL statement

Thanks in advance!

added 207 characters in body
Source Link
massideux
  • 185
  • 3
  • 15

I am trying to insert data from my array into MySQL. To my big surprise there were not many examples on how to do it if you perform a for-loop for your array, every example that I have found was from an already existing array list.

Thanks to Adrian below, we noticed that I need tuples for my list.

Updated code

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(table_datainsert_row))
values = []
for items in array:
 values.append("({0}, {1}, {2})".formatprint(item[0],item[1],item[2])array)
query += ",".join(values)
cursor = connection.cursor()
cursor.executeexecutemany(query, array)
cursor.commit()

This code gives meGetting close but at the moment I receive the following

array is not definedIndexError: Tuple index out of range

I changed it to insert_row.append(tuple(table_data))

table_data is not definedmysql.connector.errors.ProgrammingError: Not enough parameters for the SQL statement

Thanks in advance!

I am trying to insert data from my array into MySQL. To my big surprise there were not many examples on how to do it if you perform a for-loop for your array, every example that I have found was from an already existing array list.

Thanks to Adrian below, we noticed that I need tuples for my list.

Updated code

query = "INSERT INTO blue (created, published, publisher) VALUES " 
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(table_data))
values = []
for items in array:
 values.append("({0}, {1}, {2})".format(item[0],item[1],item[2]))
query += ",".join(values)
cursor = connection.cursor()
cursor.execute(query)
cursor.commit()

This code gives me

array is not defined

I changed it to insert_row.append(tuple(table_data))

table_data is not defined

Thanks in advance!

I am trying to insert data from my array into MySQL. To my big surprise there were not many examples on how to do it if you perform a for-loop for your array, every example that I have found was from an already existing array list.

Thanks to Adrian below, we noticed that I need tuples for my list.

Updated code

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()

Getting close but at the moment I receive the following

IndexError: Tuple index out of range

mysql.connector.errors.ProgrammingError: Not enough parameters for the SQL statement

Thanks in advance!

deleted 53 characters in body
Source Link
massideux
  • 185
  • 3
  • 15

I am trying to insert data from my array into MySQL. To my big surprise there were not many examples on how to do it if you perform a for-loop for your array, every example that I have found was from an already existing array list.

Example: Inserting an array into mysql with Python

Thanks to Adrian below, we noticed that I assume something is wrong with myneed tuples for-loop and the executemany() my list.

The current error message is:

"mysql.connector.errors.ProgrammingError: Not enough parameters for the SQL statement"

Any suggestions?

Code below:Updated code

connection = mysql.connector.connect(
 host='localhost', 
 database='test',
 user='root', 
 password='pass'
 )
query = "INSERT INTO bluesblue (created, published, publisher) VALUES (%s," %s,
tr %s= soup.find_all('tr')"
for table_row in tr:
td row_data = souptable_row.find_all('td')
array insert_row = []
for data in tdrow_data:
 data = re.sub('<[^>]*>', '', str(data))
 array insert_row.append(data)
 print(array.append(tuple(table_data))
values = []
for items in array:
 cursorvalues.append("({0}, ={1}, connection{2})".cursorformat(item[0],item[1],item[2]))
query += ",".join(values)
cursor = connection.cursor()
cursor.executemanyexecute(query, items)
cursor.commit()

This code gives me

array is not defined

I changed it to insert_row.append(tuple(table_data))

table_data is not defined

Thanks in advance!

I am trying to insert data from my array into MySQL. To my big surprise there were not many examples on how to do it if you perform a for-loop for your array, every example that I have found was from an already existing array list.

Example: Inserting an array into mysql with Python

I assume something is wrong with my for-loop and the executemany().

The current error message is:

"mysql.connector.errors.ProgrammingError: Not enough parameters for the SQL statement"

Any suggestions?

Code below:

connection = mysql.connector.connect(
 host='localhost', 
 database='test',
 user='root', 
 password='pass'
 )
query = "INSERT INTO blues (created, published, publisher) VALUES (%s, %s, %s)"
td = soup.find_all('td')
array = []
for data in td:
 data = re.sub('<[^>]*>', '', str(data))
 array.append(data)
 print(array)
for items in array:
 cursor = connection.cursor()
 cursor.executemany(query, items)
cursor.commit()

Thanks in advance!

I am trying to insert data from my array into MySQL. To my big surprise there were not many examples on how to do it if you perform a for-loop for your array, every example that I have found was from an already existing array list.

Thanks to Adrian below, we noticed that I need tuples for my list.

Updated code

query = "INSERT INTO blue (created, published, publisher) VALUES " 
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(table_data))
values = []
for items in array:
 values.append("({0}, {1}, {2})".format(item[0],item[1],item[2]))
query += ",".join(values)
cursor = connection.cursor()
cursor.execute(query)
cursor.commit()

This code gives me

array is not defined

I changed it to insert_row.append(tuple(table_data))

table_data is not defined

Thanks in advance!

added 32 characters in body
Source Link
massideux
  • 185
  • 3
  • 15
Loading
Source Link
massideux
  • 185
  • 3
  • 15
Loading
default

AltStyle によって変換されたページ (->オリジナル) /