pandas连接数据库
import pymysql
from sqlalchemy import create_engine
sql = 'select * from data'
conn = create_engine('mysql+pymysql://root:[email protected]:3306/test')
pdata = pd.read_sql(sql,conn)
print(pdata)
下面解释一下上面的代码:
- 导入两个包 pymysql 和sqlalchemy
- create_engine(url)此函数作用是与数据库建立连接,下面主要说一下里面url这个参数:
在这里截取代码中官方给的注释:
The string form of the URL is
``dialect[+driver]://user:password@host/dbname[?key=value..]``, where
``dialect`` is a database name such as ``mysql``, ``oracle``,
``postgresql``, etc., and ``driver`` the name of a DBAPI, such as
``psycopg2``, ``pyodbc``, ``cx_oracle``, etc. Alternatively,
具体意思是 这个url是个字符串型,具体格式是dialect[