您的位置:首页 > 编程语言 > Python开发

python调用mysql执行duplicate简单例子

2015-01-30 21:43 441 查看
#!/usr/bin/python
import MySQLdb
db=MySQLdb.connect("localhost","root","openos","python")
cur=db.cursor()
try:
sql="""INSERT INTO A (ID,NAME) VALUES (112,'%s') ON DUPLICATE KEY UPDATE NAME=VALUES(NAME);""" % "landpack"
cur.execute(sql)
db.commit()
except MySQLdb.Error,e:
print "MySQL error %d:%s" % (
e.args[0],e.args[1])


代码片段1如上:

#!/usr/bin/python
import MySQLdb
db=MySQLdb.connect("localhost","root","openos","python")
cur=db.cursor()
try:
sql="""INSERT INTO B (ID,NAME,SEX,AGE) VALUES (12,'%s','%s',%d) ON DUPLICATE KEY UPDATE NAME=VALUES(NAME),
                           SEX=VALUES(SEX),AGE=VALUES(AGE);""" % ("mac","G",45)
cur.execute(sql)
db.commit()
except MySQLdb.Error,e:
print "MySQL error %d:%s" % (
e.args[0],e.args[1])


代码片段2如上:
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: