samedi 23 octobre 2021

python - how to repeat to write numbers in mysql

Problem: please why can't I write to mysql repeatedly via -- for f in range(10):?

import mysql.connector
from mysql.connector import Error
import random 




mydb = mysql.connector.connect(
  host="localhost",
  user="db123",
  password="1111",
  database="db123"
)


mycursor = mydb.cursor()
#sql = "INSERT INTO customers (c1, c2, c3, c4, c5, d1, d2) VALUES (%s, %s, %s, %s, %s, %s, %s)"
sql = """INSERT INTO customers (c1, c2, c3, c4, c5, d1, d2) VALUES (%s, %s, %s, %s, %s, %s, %s)"""


for f in range(10):
   # int = (c1, c2, c3, c4, c5, d1, d2)
     c1 = random.randint(1, 10)
     c2 = random.randint(1, 10)
     c3 = random.randint(1, 10)
     c4 = random.randint(1, 10)
     c5 = random.randint(1, 10)
     d1 = random.randint(1, 10)
     d2 = random.randint(1, 10)
val = (c1, c2, c3, c4, c5, d1, d2)

mycursor.execute(sql, val)
#for mycursor.execute(sql, val) in range(3):

mydb.commit()

print(mycursor.rowcount, "record inserted.") 

Output: 1 record inserted.

Question: what should i do to have 10 - 100million random write to mysql?

Thx




Aucun commentaire:

Enregistrer un commentaire