gpt4 book ai didi

python - 类型错误 : 'str' object is not callable (mysql-python)

转载 作者:行者123 更新时间:2023-11-29 12:16:53 24 4
gpt4 key购买 nike

我正在使用 mysql-python 进行编码。

要将新记录添加到数据库中,我使用以下代码:

# Open database connection
db = MySQLdb.connect("localhost","root","admin","majoranalysis" )
# prepare a cursor object using cursor() method
cursor = db.cursor()
sql = "INSERT INTO
# insert a record
jobdetail(title,date_accessed,description,requirement,url) \
VALUES(%(title)s,%(data_accessed)s,%(description)s,%(requirement)s,%(url)s)"
dt = ('data analysist',date(2015,4,16),'description','requirement',joblistlink[0])
cursor.execute(sql,dt)

问题不在于声明str,但出现错误的可能性是:

Traceback (most recent call last):
File "./re-ex.py", line 81, in <module>
dt = ('data analysist',date(2015,4,16),'description','requirement',joblistlink[0])
TypeError: 'str' object is not callable

创建表的sql命令是:

CREATE TABLE IF NOT EXISTS `jobdetail` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(225) COLLATE utf8_unicode_ci NOT NULL,
`date_accessed` date NOT NULL,
`description` text COLLATE utf8_unicode_ci NOT NULL,
`requirement` text COLLATE utf8_unicode_ci NOT NULL,
`url` varchar(225) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;

你知道bug在哪里吗?

最佳答案

因为你使用%(key)s作为sql str中的占位符。所以这意味着你应该使用字典将数据提供给sql .

例如。

将元组赋予%s,例如:print "%s:%s"% ('key', 'val')

将字典提供给%(key)s,例如print '%(k1)s:%(v1)s' % {'k1':'key', 'v1': 'val'}

如果您仍然不知道如何解决问题。更改为

dt={'title':'data analysist',
'data_accessed':date(2015,4,16),
'description':'description',
'requirement':'requirement',
'url':joblistlink[0]}

关于python - 类型错误 : 'str' object is not callable (mysql-python),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29664577/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com