pandas.read_sql(),读取sqlite3保存的数据说明
2016-07-08 11:38
381 查看
pandas.read_sqlpandas.read_sql(sql, con, index_col=None, coerce_float=True, params=None, parse_dates=None, columns=None,chunksize=None)Read SQL query or database table into a DataFrame.
PEP 249‘s paramstyle:paramstyleString constant stating the type of parameter marker formatting expected by the interface. Possible values are [2]:
我从Mysql中读取的数据,用to_sql保存到sqlite中,然后需要读取保存为sqlite的数据:需要格式化字符串我是这样实现的:
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 URIor 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: NoneColumn(s) to set as index(MultiIndex)coerce_float : boolean, default TrueAttempt to convert values to non-string, non-numeric objects (like decimal.Decimal) to floating point, useful for SQL result setsparams : list, tuple or dict, optional, default: NoneList 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: NoneList of column names to parse as datesDict 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 timestampsDict of {column_name: arg dict}, where the arg dict corresponds to the keyword arguments of pandas.to_datetime()Especiallyuseful with databases without native Datetime support, such as SQLitecolumns : list, default: NoneList of column names to select from sql table (only used when reading a table).chunksize : int, default NoneIf specified, return an iterator where chunksize is the number of rows to include in each chunk. |
---|---|
Returns: | DataFrame |
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 |
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的
相关文章推荐
- MongoDB数据文件内部结构
- 快照技术原理
- 关于Mysql事务REPEATABLE_READ隔离级别下多版本并发控制(MVCC)重复读产生的坑的你踩过么?
- 用JDBC连接Oracle数据库 via Eclipse
- [Database] 数据库事务隔离级别
- MySQL入门知识_实验楼
- 数据库导出数据
- Mysql InnoDB行锁实现方式(转)
- MySQL分页性能优化
- Redis
- SQLite vs MySQL vs PostgreSQL: A Comparison Of Relational Database Management Systems
- mysql中中文乱码问题
- jdbc操作数据库
- 1、@Transient @[email protected]�射,ORM框架将忽略该属性; 如果一个属性并非数据库表的字段映射,就务必将其标示为@Transient,否则ORM框架默认其注解为@B
- PreparedStatement解决sql注入问题
- 向mysql数据库中插入大二进制文件和大文本
- Statement及PreparedStatement执行多个sql
- mysql双机热备实现(mysql版本:5.1.7)
- Xtrabackup构建MySQL主从环境
- django的数据库模型中的域多次引用同一个外键(表)