【问题标题】:How to get the total number of rows in a database through SQLalchemy?如何通过 SQLalchemy 获取数据库中的总行数?
【发布时间】:2019-06-07 18:37:21
【问题描述】:

我在 sqlalchemy 中创建了一个 sqlite 数据库,并将数据插入其中。现在我想获得插入的总行数。

我正在尝试遵循这个堆栈溢出问题的示例

Get the number of rows in table using SQLAlchemy

但是,这涉及创建一个“Congress”类和geoalchemy 库,我不确定它们来自哪里,或者它们是如何相关的。

这是我的代码

import sqlalchemy
from sqlalchemy import create_engine
engine = sqlalchemy.create_engine("sqlite:///dbfile/CSSummaries.db")
pandasDataframeExample.to_sql('CS_table', engine, index = False, if_exists= 'append')

现在我想看看添加了多少行,也许还可以从数据库中查看一些样本,以确保一切正常。

我想看看它有 x 多行。

奖金:

一种对数据库中的一些数据进行采样的简单方法。

【问题讨论】:

    标签: python sqlite sqlalchemy


    【解决方案1】:

    可以通过 for 循环运行它:

    import sqlalchemy
    from sqlalchemy import create_engine
    engine = sqlalchemy.create_engine("sqlite:///dbfile/CSSummaries.db")
    for i in range(len(pandasDataframeExample)): 
        DataFrame.iloc[0].to_sql('CS_table', engine, index = False, if_exists= 'append')
        if i%10 == 0: 
            print('rows inserted: ' + str(i))
    

    【讨论】:

      猜你喜欢
      • 2014-05-17
      • 1970-01-01
      • 2017-01-07
      • 1970-01-01
      • 1970-01-01
      • 2019-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多