Python 到 MySQL/MariaDB 插入错误

Python to MySQL/MariaDB Insert Error

所以我正在尝试编写一个 python 程序来自动跟踪我的共同基金。我刚开始,发现自己几乎停留在开始阶段。

以下是我正在使用的代码(只是一个虚拟插入块,稍后我将对其进行参数化):

import MySQLdb
import datetime

host        =       "some host"                 #server where mariadb is hosted. Diskstation in my case
user        =       "some uname"                #mariadb user
password    =       "some pwd"                  #mariadb password
schema      =       "portfolio"                 #schema to be used                      

db    = MySQLdb.connect(host, user, password, schema)
curs  = db.cursor()

try:
        curs.execute ("""delete from portfolio.mutual_funds""")
        db.commit()
        curs.execute ("""INSERT INTO mutual_funds (mf_id, mf_name, mf_nav, mf_nav_dt) VALUES (%s, %s, %l, %s)""", (120523,"Axis Fund",14.6357,88888))
        db.commit()
except db.Error as error:
         print("Error: {}".format(error))
         db.rollback()

#display the table contents
    curs.execute("""SELECT * from portfolio.mutual_funds""")
    print curs.fetchall()

curs.close()
db.close()

我收到这些随机错误。

File "/usr/lib/python2.7/dist-packages/MySQLdb/cursors.py", line 159, in execute query = query % db.literal(args) ValueError: unsupported format character ',' (0x2c) at index 43

我做错了什么?我花了两天时间试图克服这个障碍,但都失败了。有人请把我拉出来。

顺便说一句,我正在使用安装在 Synology Diskstation 上的 MariaDB,运行 来自 Python 2.7 的查询安装在 raspberry Pi.

尝试将 %s, %s, %l, %s 更改为 %%s, %%s, %%l, %%s

谢谢大家。我已经尝试了列出的选项,但不知何故不起作用。最后我能够稍微调整代码(拆分查询和参数)并且以下代码对我有用:

        needed_mf_info = (fetched_tuple[0],fetched_tuple[3],fetched_tuple[4],datetime.datetime.strptime(fetched_tuple[7],'%d-%b-%Y'))

        query = "INSERT INTO mutual_funds(mf_id, mf_name, mf_nav, mf_nav_dt) " \
        "VALUES(%s,%s,%s,%s)"

        #Inserting into the table
        try:                                                    
                curs.execute(query, needed_mf_info)
                db.commit()                 

        except db.Error as error:
            print("Error: {}".format(error))
            db.rollback()