SQLAlchemy-03-1
from sqlalchemy
import create_engine
from sqlalchemy
.sql
.expression
import text
from sqlalchemy
import MetaData
, Table
from sqlalchemy
import Integer
, String
, Text
, DateTime
from sqlalchemy
import Column
, ForeignKey
创建引擎
uri
= 'mysql+pymysql://root:root@127.0.0.1:3306/user_system?charset=utf8'
engine
= create_engine
(uri
, echo
=True)
构建元数据
meta
= MetaData
(bind
=engine
)
获取表
tb_user
= Table
('tb_user', meta
, autoload
=True, autoload_with
=engine
)
Insert
ins
= tb_user
.insert
().values
(username
='ruirui', password
='123456')
print(str(ins
))
print(ins
.compile().params
)
print(ins
.compile().string
)
conn
= engine
.connect
()
result
= conn
.execute
(ins
)
if result
.is_insert
:
print(result
.inserted_primary_key
)
ins
= tb_user
.insert
()
print(str(ins
))
result
= conn
.execute
(ins
, username
='aaron', password
='hillel')
data
= [
{'username': 'swartz', 'password' : '1234567'},
{'username': 'gates', 'password' : '3456789'},
{'username': 'bill', 'password' : '111222333'}
]
result
= conn
.execute
(ins
, data
)
print(result
.rowcount
)
conn
.close
()
销毁引擎
engine
.dispose
()
SQLAlchemy-03-2
from sqlalchemy
import create_engine
from sqlalchemy
.sql
.expression
import text
from sqlalchemy
import MetaData
, Table
from sqlalchemy
import Integer
, String
, Text
, DateTime
from sqlalchemy
import Column
, ForeignKey
创建引擎
uri
= 'mysql+pymysql://root:root@127.0.0.1:3306/user_system?charset=utf8'
engine
= create_engine
(uri
, echo
=True)
构建元数据
meta
= MetaData
(bind
=engine
)
获取表
tb_user
= Table
('tb_user', meta
, autoload
=True, autoload_with
=engine
)
Delete
dlt
= tb_user
.delete
()
print(str(dlt
))
dlt
= tb_user
.delete
().where
(
tb_user
.columns
['id'] >= 10
)
print(str(dlt
))
dlt
= tb_user
.delete
().where
(
tb_user
.columns
['password'].startswith
('1')
)
print(str(dlt
))
dlt
= tb_user
.delete
().where
(
tb_user
.columns
['password'].like
('%3%')
).where
(
tb_user
.columns
['password'].notlike
('%9%')
).where
(
tb_user
.columns
['id'] < 10
)
print(str(dlt
))
conn
= engine
.connect
()
result
= conn
.execute
(dlt
)
print(result
.rowcount
)
conn
.close
()
显示SQL语句
def structure_sql(sql_str_or_stmt
, dialect_obj
=None, sql_params
=None, return_obj
=False):
'''
构造SQL语句
参数:
sql_str_or_stmt: 原始(Raw)SQL字符串或Statement(Select、Insert、Update、Delete)对象
dialect_obj: 数据库专用术语对象
sql_params: 参数
return_obj: 是否返回编译对象(默认否,返回字符串)
refer: https://stackoverflow.com/questions/5631078/sqlalchemy-print-the-actual-query#answer-45551136
'''
stmt
= sql_str_or_stmt
if isinstance(stmt
, str):
stmt
= text
(stmt
)
if bool(sql_params
):
if hasattr(stmt
, 'bindparams'):
stmt
= stmt
.bindparams
(**sql_params
)
if dialect_obj
is None:
if bool(stmt
.bind
):
dialect_obj
= stmt
.bind
.dialect
else:
raise ValueError
('参数 [dialect_obj] 未指定')
full_sql
= stmt
.compile(
dialect
=dialect_obj
,
compile_kwargs
={"literal_binds": True}
)
return full_sql
if return_obj
else full_sql
.string
销毁引擎
engine
.dispose
()
SQLAlchemy-03-3
from sqlalchemy
import create_engine
from sqlalchemy
.sql
.expression
import text
from sqlalchemy
import MetaData
, Table
from sqlalchemy
import Integer
, String
, Text
, DateTime
from sqlalchemy
import Column
, ForeignKey
创建引擎
uri
= 'mysql+pymysql://root:root@127.0.0.1:3306/user_system?charset=utf8'
engine
= create_engine
(uri
, echo
=True)
构建元数据
meta
= MetaData
(bind
=engine
)
获取表
tb_user
= Table
('tb_user', meta
, autoload
=True, autoload_with
=engine
)
Update
upt
= tb_user
.update
()
print(str(upt
))
upt_nd
= tb_user
.update
().where
(
tb_user
.columns
.id.between
(8, 9)
)
print(str(upt_nd
))
upt
= tb_user
.update
().values
(username
='新来的')
print(str(upt
))
upt
= tb_user
.update
().values
(username
='Python', password
='py666789')
print(str(upt
))
new_data
= {
'username': 'new数据',
'password': '123456'
}
upt
= tb_user
.update
().where
(
tb_user
.columns
['id'] > 10
).values
(**new_data
)
print(str(upt
))
conn
= engine
.connect
()
result
= conn
.execute
(upt
)
result
= conn
.execute
(upt_nd
, password
='6ge654321')
print(result
.rowcount
)
conn
.close
()
销毁引擎
engine
.dispose
()
SQLAlchemy-03-4
from sqlalchemy
import create_engine
from sqlalchemy
.sql
.expression
import text
from sqlalchemy
import MetaData
, Table
from sqlalchemy
import Integer
, String
, Text
, DateTime
from sqlalchemy
import Column
, ForeignKey
创建引擎
uri
= 'mysql+pymysql://root:root@127.0.0.1:3306/user_system?charset=utf8'
engine
= create_engine
(uri
, echo
=True)
构建元数据
meta
= MetaData
(bind
=engine
)
获取表
tb_user
= Table
('tb_user', meta
, autoload
=True, autoload_with
=engine
)
Select
sel
= tb_user
.select
()
print(str(sel
))
sel
= tb_user
.select
().where
(
tb_user
.columns
.id.between
(8, 9)
)
print(str(sel
))
conn
= engine
.connect
()
result
= conn
.execute
(sel
)
if result
.returns_rows
:
print(result
.fetchone
())
print(result
.closed
)
N
= 2
print(result
.fetchmany
(N
))
print(result
.fetchall
())
result
.close
()
result
= conn
.execute
(sel
)
print(result
.first
())
print(result
.closed
)
conn
.close
()
销毁引擎
engine
.dispose
()
转载于:https://www.cnblogs.com/cp9648/p/10415738.html
转载请注明原文地址: https://mac.8miu.com/read-11936.html