python mysql insert参数化

  # 打开数据库连接
    db = pymysql.connect("*****"//主机地址0.0.0.0格式, "*****"//用户名, "****"//密码, "*****"//数据库名, port=3306, charset='utf8') 
 # 使用cursor()方法获取操作游标
    cursor = db.cursor()
 try:
    cursor.execute("INSERT INTO table1 (name, age, height) values ('{0}','{1}','{2}')".format(name, age, height))
    db.commit()  # 提交到数据库执行
 except:
    db.rollback()  # 如果发生错误则回滚
    # 关闭数据库连接
    db.close()

猜你喜欢

转载自blog.csdn.net/qq_35416214/article/details/106231404