python的pymsql的使用

我想写一个函数,能向指定的表中插入一些数据,但是参数怎么传递 不清楚,麻烦大家帮忙改一下,没加入table之前函数能正常使用

def mysql_insert(table,date,high,low):
    try:
           param=[table,date,high,low]
           sql="INSERT INTO table (date,high,low) VALUES(%s,%s,%s)"
           cur.execute(sql,param)
           conn.commit()
    except:
            return 0   
阅读 3.9k
1 个回答

没用过pymysql,不过这样做可以实现你的功能:

sql = "INSERT INTO {} (date, high, low) VALUES(%s, %s, %s)".format(table)
cur.execute(sql, [date, high, low])
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题