Python:dataframe.to_sql和dataframe.read_sql
2017-09-25 14:53
447 查看
一个新的数据库接口sqlalchemy,感觉很像Java领域的Hibernate框架。
从此,就可以用dataframe.to_sql和df.read_sql了。
df.to_sql('t_trade_record',engine,index=False,if_exists=xxx)
一定要注意的是指定,if_exists!
千万要小心,是replace,还是append!
不然小心数据库啥数据都被搞没了!
另外,engine.execute(sql)是可以直接执行sql语句的。
from sqlalchemy import create_engine db_info = {'user':'user', 'password':'pwd', 'host':'localhost', 'database':'xx_db' } engine = create_engine('mysql+pymysql://%(user)s:%(password)s@%(host)s/%(database)s?charset=utf8' % db_info,encoding='utf-8')
从此,就可以用dataframe.to_sql和df.read_sql了。
df.to_sql('t_trade_record',engine,index=False,if_exists=xxx)
一定要注意的是指定,if_exists!
千万要小心,是replace,还是append!
不然小心数据库啥数据都被搞没了!
另外,engine.execute(sql)是可以直接执行sql语句的。
相关文章推荐
- python远程连接mysql以及pandas.DataFrame.to_sql写入数据库
- python连接数据库,tushare,Dataframe to sql
- python pandas dataframe to_sql方法error及其解决
- Spark ML 之 RDD to DataFrame (python版)
- How to Use 'Exec Sp_executesql' to Read SQL String with Data Exchange(Interface)
- dataframe 存入MySQL,Python 'timestamp' cannot be converted to a MySQL type
- java.sql.SQLException: Connection is read-only. Queries leading to data modification are not allowed
- Python学习笔记 --- Pycharm中遇到 no data sources are configured to run thi s sql...
- (精)java.sql.SQLException: No more data to read from socket
- java.sql.SQLException: Connection is read-only. Queries leading to data modification are not allowed
- dataframe to sql
- java.sql.SQLException: Connection is read-only. Queries leading to data modification are not allowed
- How to read frame data (Fighting Game)
- Cause: java.sql.SQLException: Connection is read-only. Queries leading to data modification are not
- java.sql.SQLException: Connection is read-only. Queries leading to data modification are not allowed
- 拣点芝麻:dbcp (Oracle)重新链接的问题 - Cause: java.sql.SQLException: No more data to read from socket
- java.sql.SQLException: Connection is read-only. Queries leading to data modification are not allowed
- python dict to dataframe
- java.sql.SQLException: Connection is read-only. Queries leading to data modification are not allowed
- Python dataframe to array