python动态输入,更新表

python动态输入,更新表,python,mysql-python,Python,Mysql Python,为了动态更新数据库表,我编写了一个程序,但出现了一个错误。我在节目里塞满了我所知甚少的东西。这是我的密码: import MySQLdb class data: def __init__(self): self.file123 = raw_input("Enter film: ") self.title_ = raw_input("Enter film: ") self.year = raw_input("Enter year: ")

为了动态更新数据库表,我编写了一个程序,但出现了一个错误。我在节目里塞满了我所知甚少的东西。这是我的密码:

import MySQLdb
class data:
    def __init__(self):
        self.file123 = raw_input("Enter film: ")
        self.title_ = raw_input("Enter film: ")
        self.year = raw_input("Enter year: ")
        self.director = raw_input("Enter director: ")
a=data()

db = MySQLdb.connect(host="localhost", # your host, usually localhost
                     user="root", # your username
                      passwd="mysql", # your password
                      db="sakila") # name of the data base

cursor = db.cursor()

cursor.execute("INSERT INTO films (file123, title_, year, director) VALUES (?, ?, ?, ?)", (a.file123, a.title_, a.year, a.director))
db.commit()
db.close()
这就是错误:

File "C:\Python27\maybe1.py", line 20, in <module>
 cursor.execute("INSERT INTO films (file123, title_, year, director) VALUES (?, ?, ?, ?)", (a.file123, a.title_, a.year, a.director))
      File "C:\Python27\lib\site-packages\MySQLdb\cursors.py", line 184, in execute
        query = query % db.literal(args)
    TypeError: not all arguments converted during string formatting
文件“C:\Python27\maybe1.py”,第20行,在
cursor.execute(“插入电影(文件123,标题,年份,导演)值(?,,,,?)”,(a.file123,a.title,a.year,a.director))
文件“C:\Python27\lib\site packages\MySQLdb\cursors.py”,第184行,在execute中
query=查询%db.literal(args)
TypeError:在字符串格式化过程中并非所有参数都已转换

如何解决此问题?

您应该将
更改为
%s


问题是为什么mysqldb使用
%s
而不是

我会这样做:

query = "INSERT INTO films (file123, title_, year, director) VALUES (%s, %s, %s, %s)" % (a.file123, a.title_, a.year, a.director)

cursor.execute(query)

将%s替换为正确的数据类型,否则它将尝试所有可能在表级别中断的字符串。

使用
%s
而不是
。我仍然得到以下错误:OperationalError:(1366,“不正确的整数值:'a'用于第1行的列'file123')这是mysql错误:'a'不是整数类型,但是您正在尝试将其插入一个整数字段。如何为每个字段专门定义数据类型,请提供任何示例链接。这也会很有帮助吗?
CREATE TABLE test(id INT)
使用名为
id
的字段创建一个表,其类型为
INT
。如果我需要字符串,在上面的例子中,我应该怎么做才能输入字符串?这样连接参数是不安全的。为什么说它不安全?