使用where子句中的变量从sqlite3读取 pandas 框架 [英] reading pandas frame from sqlite3 using variable in where clause

查看:147
本文介绍了使用where子句中的变量从sqlite3读取 pandas 框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个相当大的sqlite3数据库,有时在尝试将其读入Pandas数据帧时遇到内存错误.我可以使用WHERE函数来限制框架的大小,以便它可以工作.

I have a fairly large sqlite3 database and sometimes I get a memory error when trying to read it into a Pandas dataframe. I can use the WHERE function to limit the size of the frame so that it will work.

con = sqlite3.connect("myDB.db")
frame = pd_sql.read_frame('SELECT * FROM Basketball WHERE League == "NBA"', pinnyCon)

我将如何命名一个变量,这样类似的东西才能起作用?

How would I name a variable so that something like this would work?

def GetLeagueFrame(leagueName):
    con = sqlite3.connect("myDB.db")
    frame = pd_sql.read_frame('SELECT * FROM Basketball WHERE League == leagueName', pinnyCon)
    con.close()
    return frame

推荐答案

您可以使用?进行参数替换:

You can use parameter substitution with ? for that:

pd.read_sql_query('SELECT * FROM Basketball WHERE League = ?', con, params=(leagueName, ))

有关更多信息,请参见 read_sql_query文档字符串 params关键字的说明.

See the read_sql_query docstring for more explanation on the params keyword.

这篇关于使用where子句中的变量从sqlite3读取 pandas 框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆