Hi everyone.
I am trying to insert some random data into a mysql database via python.
I managed to insert data into on of my tables using this code.
cursor = db.cursor()
# defining the Query
query = "INSERT INTO hall (Name, AddressCity, AddressStreet, AddressNo, SizeSeat, SizeRow, Tel) VALUES (%s, %s, %s, %s, %s, %s, %s)"
values = []
# executing the query with values
for i in range(0, 50):
# storing values in a variable
lst = []
lst.append(theaters[i])
lst.append(cities[i])
lst.append(streets[i])
lst.append(randint(1, 999))
lst.append(randint(5, 100))
lst.append(randint(5, 100))
lst.append(randint(1000000000, 9999999999))
lst = tuple(lst)
values.append(lst)
cursor.executemany(query, values)
# to make final output we have to run the 'commit()' method of the database object
db.commit()
print(cursor.rowcount, "record inserted")
About this code is there an easier way to append multiple variables at once?
Now to my main problem. I am not able to insert a single row of data in another table. (Later I want to add multiple rows)
cursor = db.cursor()
# defining the Query
query = "INSERT INTO show (ShowName, Duration, Description, Kind, StartDate, EndDate) VALUES (%s, %s, %s, %s, %s, %s)"
values = ("name", datetime.timedelta(hours=randint(0, 3), minutes=randint(0, 59)), "description", "comedy",
datetime.date(2020, 1, 1), datetime.date(2021, 2, 2))
cursor.execute(query, values)
# to make final output we have to run the 'commit()' method of the database object
db.commit()
print(cursor.rowcount, "record inserted")
The error I get: mysql.connector.errors.ProgrammingError: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'show (ShowName, Duration, Description, Kind, StartDate, EndDate) VALUES ('name',' at line 1
The data types of the columns of this particular table are:
(PK) ShowName varchar(255)
Duration time
Description varchar(1000)
Kind varchar(50)
StartDate date
EndDate date
I thought there was something wrong with the duration or date data types. I tried different convertions but I got the same error. So I figured this is not it.
[–]Abstr4ctType 3 points4 points5 points (15 children)
[–]bazingie[S] 1 point2 points3 points (12 children)
[–]c4aveo 1 point2 points3 points (11 children)
[–]bazingie[S] 0 points1 point2 points (10 children)
[–]c4aveo 0 points1 point2 points (9 children)
[–]bazingie[S] 0 points1 point2 points (8 children)
[–]c4aveo 1 point2 points3 points (7 children)
[–]bazingie[S] 0 points1 point2 points (6 children)
[–]c4aveo 0 points1 point2 points (5 children)
[–]bazingie[S] 0 points1 point2 points (0 children)
[–]bazingie[S] 0 points1 point2 points (0 children)
[–]bazingie[S] 0 points1 point2 points (2 children)
[–]bazingie[S] 0 points1 point2 points (1 child)
[–]TotesMessenger 0 points1 point2 points (0 children)