Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import mysql.connector
- import time
- from tqdm import tqdm
- def db_connect():
- return mysql.connector.connect(host='localhost',
- database='MYDATABASE',
- user='root',
- password='MYROOT')
- def time_in_ms():
- return int(round(time.time() * 1000))
- repeats = 100
- connection = db_connect()
- cursor = connection.cursor()
- print('NORMAL')
- print('cursor', type(cursor))
- start = time_in_ms()
- for i in tqdm(range(repeats)):
- cursor.execute('DELETE FROM test')
- connection.commit()
- cursor.execute("INSERT into test (id,name) VALUES (1,'Value 1')")
- cursor.execute("INSERT into test (id,name) VALUES (2,'Value 2')")
- cursor.execute("INSERT into test (id,name) VALUES (3,'Value 3')")
- cursor.execute("INSERT into test (id,name) VALUES (4,'Value 4')")
- cursor.execute("INSERT into test (id,name) VALUES (5,'Value 5')")
- connection.commit()
- print('Time:', time_in_ms() - start)
- cursor.close()
- connection.close()
- connection = db_connect()
- cursor = connection.cursor(prepared=True)
- print('\n\nPREPARED')
- print('cursor', type(cursor))
- start = time_in_ms()
- for i in tqdm(range(repeats)):
- cursor.execute('DELETE FROM test')
- connection.commit()
- sql = "INSERT into test (id,name) VALUES (%s,%s)"
- cursor.execute(sql, (1, 'Value 1'))
- cursor.execute(sql, (2, 'Value 2'))
- cursor.execute(sql, (3, 'Value 3'))
- cursor.execute(sql, (4, 'Value 4'))
- cursor.execute(sql, (5, 'Value 5'))
- connection.commit()
- print('Time:', time_in_ms() - start)
Add Comment
Please, Sign In to add comment