Python中的MySQL INSERT语句

Python中的MySQL INSERT语句,python,mysql,sql-insert,Python,Mysql,Sql Insert,我试图使用Python插入MySQL数据库,但我有一个自动递增的列TeamID。我正在使用下面的线条,它就像一个符咒。但是我不想在Python脚本中指定TeamID,因为它是一个自动增量 尝试: 这很好用 我怎样才能去掉第一个%d和11?我希望通过脚本自动添加此值 非常感谢您的帮助 编辑: 试一试 即明确列出列。 另外,请不要这样做-您确实需要使用准备好的语句,而不是执行“%s”,我认为在Python中它类似于: cursor.execute("INSERT INTO teams (name,

我试图使用Python插入MySQL数据库,但我有一个自动递增的列TeamID。我正在使用下面的线条,它就像一个符咒。但是我不想在Python脚本中指定TeamID,因为它是一个自动增量

尝试:

这很好用 我怎样才能去掉第一个%d和11?我希望通过脚本自动添加此值

非常感谢您的帮助

编辑:

试一试

即明确列出列。 另外,请不要这样做-您确实需要使用准备好的语句,而不是执行“%s”,我认为在Python中它类似于:

cursor.execute("INSERT INTO teams (name, numb, player) VALUES (%s, %d, %s)", ['Sevilla', 7, 'John Smith'])
阅读SQL注入。

问题现已解决

我确实指定了列名,但没有注意到我需要对所有列(包括int值)使用%s。详情如下:

cur.execute("INSERT INTO teams (TeamName, CountryID, TeamManager) values (%s,%s,%s)", ('Everton', 1, 'Ronald Koeman'))

你有什么错误?表的DDL是什么?1136,第1行的列计数与值计数不匹配。请发布脚本代码和数据库结构/usr/bin/python导入MySQLdb=MySQLdb.connecthost=localhost,您的主机,通常localhost user=username,username passwd=password,您的密码db=dbname数据库的名称cur=db.cursor try:cur.execute插入到团队值“%s”、“d”、“s”%Sevilla、7、Jorge Sampaoli db.commit,例外情况为e:print回滚printe db.rollback db.close
INSERT INTO teams (name, numb, player) VALUES ('%s', %d, '%s')
cursor.execute("INSERT INTO teams (name, numb, player) VALUES (%s, %d, %s)", ['Sevilla', 7, 'John Smith'])
cur.execute("INSERT INTO teams (TeamName, CountryID, TeamManager) values (%s,%s,%s)", ('Everton', 1, 'Ronald Koeman'))