I have 8 kinds of data that I would like to insert into a mysql table through mysql-connector using python. I have looked at some documents saying that it is better to use int, string, or tuple when using mysql-connector. I have tried to adjust some data types into either string or tuple, but the IDE keeps showing error.... If anyone please help me clarify which data type I shall use.
The data structure is set as follows(if anything is better to be changed please kindly let me know):
+----------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+---------------+------+-----+---------+-------+
| URL | varchar(1023) | YES | | NULL | |
| Title | varchar(1023) | YES | | NULL | |
| Content | varchar(1023) | YES | | NULL | |
| Month | varchar(1023) | YES | | NULL | |
| Date | varchar(1023) | YES | | NULL | |
| Year | varchar(1023) | YES | | NULL | |
| Time | varchar(1023) | YES | | NULL | |
| TimeZone | varchar(1023) | YES | | NULL | |
+----------+---------------+------+-----+---------+-------+
My codes are as follows:
for i in range(len(URL)):
dbcur.execute(
"""INSERT INTO scripting (URL, Title, Content, Month, Date, Year, Time, TimeZone)
VALUES ("%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s")""",
((URL[i],), (Title[i],), (Content[i],), (Month[i],), (Date[i],), (Year[i],),
(Time1[i],), (TimeZone[i],)))
dbcon.commit()
ps- URL[], Title[], Content[]... TimeZone[] are lists of data, and their ranges are the same.
URL: url. I set this with tuple and it is fine.(can be successfully stored)
Title: A title of an essay.(i.e. an sentence) I have tried to either set it as tuple with
(Title[i],)
or a string withStringIO.String(Title[i])
, but the error areMySQLConverter' object has no attribute '_tuple_to_mysql AttributeError: 'module' object has no attribute 'String'
separately.
Content: several sentences. faced same problems as above
Month, Date, Year, Time, TimeZone: have not tried yet, but I guess I can import these data with tuple type?
I have looked over stackoverflow and tried several data types but it still doesn't work out here. How do I deal with these data may someone kindly let me know?