pandas.read_sql(),读取sqlite3保存的数据说明

pandas.read_sql

pandas.read_sql(sqlconindex_col=Nonecoerce_float=Trueparams=Noneparse_dates=Nonecolumns=None,chunksize=None)

Read SQL query or database table into a DataFrame.

Parameters:

sql : string SQL query or SQLAlchemy Selectable (select or text object)

to be executed, or database table name.

con : SQLAlchemy connectable(engine/connection) or database string URI

or DBAPI2 connection (fallback mode) Using SQLAlchemy makes it possible to use any DB supported by that library. If a DBAPI2 object, only sqlite3 is supported.

index_col : string or list of strings, optional, default: None

Column(s) to set as index(MultiIndex)

coerce_float : boolean, default True

Attempt to convert values to non-string, non-numeric objects (like decimal.Decimal) to floating point, useful for SQL result sets

params : list, tuple or dict, optional, default: None

List of parameters to pass to execute method. The syntax used to pass parameters is database driver dependent. Check your database driver documentation for which of the five syntax styles, described in PEP 249’s paramstyle, is supported. Eg. for psycopg2, uses %(name)s so use params={‘name’ : ‘value’}

parse_dates : list or dict, default: None

  • List of column names to parse as dates
  • Dict of {column_name: format string} where format string is strftime compatible in case of parsing string times or is one of (D, s, ns, ms, us) in case of parsing integer timestamps
  • Dict of {column_name: arg dict}, where the arg dict corresponds to the keyword arguments of pandas.to_datetime() Especially useful with databases without native Datetime support, such as SQLite

columns : list, default: None

List of column names to select from sql table (only used when reading a table).

chunksize : int, default None

If specified, return an iterator where chunksize is the number of rows to include in each chunk.

Returns:

DataFrame


PEP 249‘s paramstyle:

paramstyle

String constant stating the type of parameter marker formatting expected by the interface. Possible values are [2]:

paramstyle Meaning
qmark Question mark style, e.g. ...WHERE name=?
numeric Numeric, positional style, e.g. ...WHERE name=:1
named Named style, e.g. ...WHERE name=:name
format ANSI C printf format codes, e.g. ...WHERE name=%s
pyformat Python extended format codes, e.g. ...WHERE name=%(name)s


我从Mysql中读取的数据,用to_sql保存到sqlite中,然后需要读取保存为sqlite的数据:需要格式化字符串
我是这样实现的:
transday_sql = "SELECT * FROM ma_data WHERE transday=:day"
for transday, state_index in zip(transday_data, np.arange(len(transday_data))):
data = pd.read_sql(sql=transday_sql, con=sqlite3_con, params={'day': str(transday)})
 
 
 
 
还可以用第二种方式格式化字符串,就变成了transday=:1 ,params=[str(transday)],注意parames是list的格式, transday 的数字1 对应的就是list中的第一个字符串, 如果有多个需要格式化字符串的,就继续在后面name2=:2,name3=:3,然后list中的元素也是对应的
如果是读取MySQL的话格式化的样子就是最后一种name=%(name)s, 那就改成transday=%(day)s, params中的字典保持不变, 
也许还有读取其他数据库的格式,每种数据库都会有对应的格式化字符串的解析方式,不是对应的解析方式是会报DataBaseError


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值