简体   繁体   English

从 Flask-SQLAlchemy 将数据导出到 CSV 文件

[英]Exporting data into CSV file from Flask-SQLAlchemy

I'm looking to generate(export) a csv from a flask-sqlalchemy app i'm developing.我正在寻找从我正在开发的 flask-sqlalchemy 应用程序中生成(导出)一个 csv。 But i'm getting some unexpected outcomes in my csv i.e. instead of the actual data from the MySQL DB table populated in the csv file, i get the declarative class model entries (placeholders??) . But i'm getting some unexpected outcomes in my csv i.e. instead of the actual data from the MySQL DB table populated in the csv file, i get the declarative class model entries (placeholders??) . The issue possibly could be the way i structured the query or even, the entire function.问题可能是我构建查询的方式,甚至是整个 function。

Oddly enough - judging from the csv output (pic) - it would seem i'm on the right track since the row/column count is the same as the DB table but actual data is just not populated.奇怪的是 - 从 csv output (图片)来看 - 似乎我在正确的轨道上,因为行/列数与数据库表相同,但实际数据并未填充。 I'm fairly new to SQLAlchemy ORM and Flask, so looking for some guidance here to pull through.我对 SQLAlchemy ORM 和 Flask 很陌生,所以在这里寻找一些指导来完成。 Constructive feedback appreciated.建设性的反馈表示赞赏。

#class declaration with  DB object (divo)
class pearl(divo.Model):
    __tablename__ = 'users'                 
    work_id = divo.Column(divo.Integer, primary_key=True)
    user_fname = divo.Column(divo.String(length=255))
    user_lname = divo.Column(divo.String(length=255))
    user_category = divo.Column(divo.String(length=255))
    user_status = divo.Column(divo.String(length=1))
    login_id = divo.Column(divo.String(length=255))
    login_passwd = divo.Column(divo.String(length=255))

#user report function
@app.route("/reports/users")
def users_report():
    with open(r'C:\Users\Xxxxxxx\Projects\_repository\zzz.csv', 'w') as s_key:
        x15 = pearl.query.all()
        for i in x15:
#        x16 = tuple(x15)
            csv_out = csv.writer(s_key)
            csv_out.writerow(x15)
    flash("Report generated. Please check designated repository.", "green")
    return redirect(url_for('reports_landing'))  # return redirect(url_for('other_tasks'))

#csv outcome (see attached pic) #csv 结果(见附图) csv 截图

instead of the actual data from the MySQL DB table populated in the csv file, i get the declarative class model entries (placeholders??)而不是来自 csv 文件中填充的 MySQL DB 表中的实际数据,我得到声明性 class Z20F35E630DAF49DF884C 条目(?

Each object in the list列表中的每个 object

x15 = pearl.query.all()

represents a row in your users table.代表users表中的一行。

What you're seeing in the spreadsheet are not placeholders, but string representations of each row object (See object. repr ).您在电子表格中看到的不是占位符,而是每行 object 的字符串表示形式(参见object.repr )。

You could get the value of a column for a particular row object by the column name attribute, for example:您可以通过列名属性获取特定行 object 的列值,例如:

x15[0].work_id # Assumes there is at least one row object in x15

What you could do instead is something like this:你可以做的是这样的:

with open(r'C:\Users\Xxxxxxx\Projects\_repository\zzz.csv', 'w') as s_key:
    x15 = divo.session.query(pearl.work_id, pearl.user_fname) # Add columns to query as needed
    for i in x15:
        csv_out = csv.writer(s_key)
        csv_out.writerow(i)

i in the code above is a tuple of the form:上面代码中的i是以下形式的元组:

('work_id value', 'user_fname value')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM