flask,尝试从mysql db读入数据(不是在字符串格式化期间转换的所有参数)

xpszyzbs  于 2021-06-25  发布在  Mysql
关注(0)|答案(2)|浏览(350)

正在尝试使用条形图创建页面。我正在使用flask框架和mysql数据库。

@app.route("/grafiek")
def chart():
    data = []
    years = optimize_list(get_data("SELECT DISTINCT year FROM tbldata"))
    print(years)

    sql = "SELECT cast(sum(Number_employees) as signed) FROM  tbldata"
    sql += "WHERE year = (%s) GROUP BY year, month ORDER BY year, month ASC"
    print(sql)

    for year in years:
        data_part = optimize_list(get_data(sql, year))
        data.append(data_part)

        print(data)

        labels = optimize_list(get_data("SELECT DISTINCT month FROM faillisementen.tbldata"))
        titels= "Number Employees in faillisementen"
        colors = ['red', 'green']

        return render_template("chart.html", data=data, labels=labels)

运行时出现错误:

getting data
SELECT cast(sum(Nulber_employees) as signed) FROM  tbldata
not all arguments converted during string formatting

我假设错误与这些行有关:

sql = "SELECT cast(sum(Number_employees) as signed) FROM  tbldata"
sql += "WHERE year = (%s) GROUP BY year, month ORDER BY year, month ASC"

请问我该怎么修?
更新:当没有使用第二行(%s)时,我得到错误“在字符串格式化期间没有转换所有参数”。当这条线被使用时,我得到了一个不同的错误。

(1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '= (2011) GROUP BY jaar, maand ORDER BY jaar, maand ASC' at line 1")
vatpfxk5

vatpfxk51#

我找到了一种不同的方法将数据库中的数据输入到图表中。
不过还是谢谢你。

def get_faillissementen(jaar):
    connectie = mysql.connect()
    cursor = connectie.cursor()

    try:
        cursor.execute("select sum(Aantal_faillissementen) from data where jaar = '%s' group by Maand ", jaar)

    except Exception as e:
        print(e)

    # alles eruit halen
        # Als je maar 1 gegeven hebt => fetchone()
    results = cursor.fetchall()
    db_data = []

   for row in results:
        db_data.append(int(row[0]))

    cursor.close()
    connectie.close()

    return db_data

@app.route("/chart")
def chart():
    jaar_2011 = get_faillissementen(2011)
    jaar_2012 = get_faillissementen(2012)
    jaar_2013 = get_faillissementen(2013)

这使它变得容易多了。

wfveoks0

wfveoks02#

可能是第二行中的(%s):
sql+=“其中年=(%s)按年分组,按年月订单,按月asc”
如果你移开那条线,它能用吗?
请参阅python mysqldb typeerror:并非所有参数都在字符串格式化期间转换

相关问题