exists的用法pythonsqlalchemy中create_engine⽤法
⽤法
engine = create_engine('')
dialect -- 数据库类型
driver -- 数据库驱动选择
username -- 数据库⽤户名
password -- ⽤户密码
host 服务器地址
port 端⼝
database 数据库
engine = create_engine(')
pg8000
engine = create_engine(')</pre>
More notes on connecting to PostgreSQL at .
MySQL
The MySQL dialect uses mysql-python as the default DBAPI. There are many MySQL DBAPIs available, including MySQL-connector-python and OurSQL:
<pre ># default
engine = create_engine(')
mysql-python
engine = create_engine(')
MySQL-connector-python
engine = create_engine(')
Oracle
create_engine(')
MSSQL
engine = create_engine(')
engine = create_engine(')</pre>
下⾯mysql作为例⼦
yconnect = create_engine('mysql+mysqldb://root:password@host:port/db?charset=utf8')
pd._sql(DataResultDF,'tablename', yconnect, schema='db', if_exists='append')
创建表结构
使⽤ Schema Type/SQL Expression Language/Engine/ConnectionPooling/Dialect 进⾏数据库操作。Engine使⽤Schema Type创建⼀个特定的结构对象,之后通过SQL Expression Language将该对象转换成SQL语句,然后通过 ConnectionPooling 连接数据库,再然后通过 Dialect 执⾏SQL,并获取结果。
from sqlalchemy import create_engine, Table, Column, Integer, /
String, MetaData, ForeignKey
import MySQLdb
#创建数据库连接
engine = create_engine("mysql+mysqldb://liuyao:liuyao@121.42.195.15:3306/liuyao", max_overflow=5)
# 获取元数据
metadata = MetaData()
# 定义表
user = Table('user', metadata,
Column('id', Integer, primary_key=True),
Column('name', String(20)),
)
color = Table('color', metadata,
Column('id', Integer, primary_key=True),
Column('name', String(20)),
)
#将dataframe 添加到 tmp_formidinfo 如果表存在就添加,不存在创建并添加
pd._sql(DataResultDF,'tmp_formidinfo',engine, schema='liuyao', if_exists='append')
# 执⾏sql语句
"INSERT lor(id, name) VALUES ('1', 'liuyao');"
)
result = ute('select * from color')
print(result.fetchall())
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论