MySQLConverter'对象没有mysql-connector属性'_tuple_to_mysql'异常 [英] MySQLConverter' object has no attribute '_tuple_to_mysql' exception with mysql-connector

查看:92
本文介绍了MySQLConverter'对象没有mysql-connector属性'_tuple_to_mysql'异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有8种数据,我想使用python通过mysql-connector插入到mysql表中. 我看过一些文档,说在使用mysql-connector时最好使用 int,字符串或元组 . 我试图将某些数据类型调整为字符串或元组,但是IDE一直显示错误.... 如果有人,请帮助我弄清楚我将使用哪种数据类型.

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 []是数据列表,它们的范围是相同的.

ps- URL[], Title[], Content[]... TimeZone[] are lists of data, and their ranges are the same.

  • URL:URL. 我用元组设置它就可以了(可以成功存储)

  • URL: url. I set this with tuple and it is fine.(can be successfully stored)

标题:论文标题(即句子) 我尝试使用(Title[i],)将其设置为元组,或者使用StringIO.String(Title[i])将其设置为字符串,但是错误是

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 with StringIO.String(Title[i]), but the error are

MySQLConverter' object has no attribute '_tuple_to_mysql  
AttributeError: 'module' object has no attribute 'String'

分别.

内容:几个句子. 面临与上述相同的问题

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?

我查看了stackoverflow并尝试了几种数据类型,但在这里仍然无法解决.有人可以让我知道如何处理这些数据?

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?

推荐答案

安提·哈帕拉(Antti Haapala)给出了一个很好的答案,但您也可以使用zip来为您构建要插入的行来对其进行整理

Antti Haapala has a great answer but you could also tidy it up by using zip to build the row to insert for you

for row in zip(URL, Title, Content, Month, Date, Year, Time1, TimeZone):
    dbcur.execute(
        """INSERT INTO scripting (URL, Title, Content, Month, Date, Year, Time, TimeZone)
           VALUES ("%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s")""",
           row)
    dbcon.commit()

这篇关于MySQLConverter'对象没有mysql-connector属性'_tuple_to_mysql'异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆