python mysql 长insert,Python中的MySQL INSERT语句

I am trying to use Python to insert into MySQL database, but I have an auto-increment column (TeamID). I am using the lines below and it works like a charm. BUT I would like to not specify the TeamID in my Python script as it is an auto-increment

try:

cur.execute ("INSERT INTO teams values (%d, '%s', %d, '%s')" % (11,"Sevilla", 7, "Jorge Sampaoli"))

db.commit()

this works perfectly

How can I get rid of the first %d and 11 please? I want this value to be added automatically via the script

any help is very much appreciated

EDIT:

#!/usr/bin/python

import MySQLdb

db = MySQLdb.connect(host="localhost", # your host, usually localhost

user="username", # your username

passwd="password", # your password

db="dbname") # name of the data base

cur = db.cursor()

try:

cur.execute ("INSERT INTO teams values ('%s', %d, '%s')" % ("Sevilla", 7, "Jorge Sampaoli"))

db.commit()

except Exception as e:

print("Rolling back")

print(e)

db.rollback()

db.close()

解决方案

Issue is now resolved

I did specify the column names but didn't notice I need to use %s for all columns including int values. As below:

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值