现在本地电脑上的数据情况如下:可以看到数据库dd已经存在
-------------------------------------------------------------------------------------
用下面的命令就可以很方便的将数据框对象导入到mysql数据库中作为一张表格
---------------------------------------------------------------------------------------
import pandas as pd
import numpy as np
#准备数据框
data1=pd.DataFrame({"x":[1,2,3],"姓名":["张三","李四","王五"]})
from sqlalchemy import create_engine
#需要事先安装pymysql库 pip install pymysql
connection1=create_engine("mysql+pymysql://root:12345@localhost:3306/dd?charset=utf8")
import sqlalchemy
#create_engine的用法可以用help(sqlalchemy.engine)看下。
pd.io.sql.to_sql(data1,"biao3",connection1,schema="dd",if_exists="replace")
type(connection1)
Out[26]: sqlalchemy.engine.base.Engine
-------------------------------------------------------------------------------------
create_engine类的意思可以看看下面的帮助文件
help(sqlalchemy.engine)
-----------------------------------------------------------------------------------
engine = create_engine("mysql://scott:tiger@hostname/dbname",
encoding='latin1', echo=True)
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,
the URL can be an instance of :class:`~sqlalchemy.engine.url.URL`.
------------------------------------------------------------------------------------
执行完上面的导入命令,发现数据已经正确导入了
------------------------------------------------------------------------------------------








暂无数据