I have a mysql database, I am using python and the mysql.connector library
My problem set is I have a list of 21 True or False value which I want to insert into the table one at a time. The way I was doing it at first is by looping over the list and trying to write 1 at a time. This was very slow with each operation taking 0.3s
My questions specifically is how can I use the library to perform batch insert statements?
class DatabaseOperations:
def __init__(self, *args): # Establish the db connection
for i in range(len(args)): # Cycle through passed arguments and assign them to variables
if i == 0: # Assign the first argument as db_connection_user
self.host = args[0]
elif i == 1: # Assign the second argument as db_connection_password
self.user = args[1]
elif i == 2: # Assign the third argument as db_connection_database
self.password = args[2]
elif i == 3: # Assign the fourth argument as db_connection_database
self.database = args[3]
elif i == 4: # Assign the fifth argument as localhost_ip
self.localhost = args[4]
self.connection_check = True
self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.socket.connect((self.localhost, gvl.global_port_database))
self.db_connection = mysql.connector.pooling.MySQLConnectionPool(
pool_name="db_pool",
pool_size=5,
pool_reset_session=True,
host=self.host,
user=self.user,
password=self.password,
database=self.database)
My first attempt at trying to make things faster was by using a connection pool instead of just a single cursor. My confusion lies in the fact on how do I perform the batch operations. My attempt is to create a list of 'write_operations' and pass that along to do something
def batch_write_table(self, list_of_writes):
try:
db_connection = self.db_connection.get_connection()
db_cursor = db_connection.cursor()
db_cursor.execute(
f"INSERT INTO table ("
f"localhost_ip, "
f"plc_ip, "
f"address_name, "
f"address_number, "
f"address_value, "
f"timestamp) "
f"VALUES("
f"'{localhost_ip}', "
f"'{plc_ip}', "
f"'{modbus_name}', "
f"'{modbus_number}', "
f"'{modbus_value}', "
f"'{timestamp}');"
)
except Exception as e:
print(f'Error => {e}')
I do not understand how to create singular write operation objects, or if I am simply supposed to create list of values and use that along with some batch_write function. Help and clarification are greatly appreciated!
EDIT:
Added how I'm writing from the main file
for i in range(len(_list)):
print('\ni -> ', i)
print('_list[i] -> ', _list[i])
match i:
case 0:
print(f'case 0 timestamp -> {timestamp_generator()}')
write_table(
data
)
essentially there are 21 possible cases as the list is 21 in length