python sqlalchemy操作mysql数据库(ORM方式)

查询

使用sql语句查询:

python 复制代码
from urllib import parse
from sqlalchemy import create_engine, Table, Column, Integer, String, MetaData

con = create_engine('mysql+pymysql://root:root@localhost:3306/testdb?charset=utf8')  
# pd.read_sql(sql,con) 

result = con.execute("SELECT * FROM testtable")

# 处理查询结果
for row in result:
    print(row)
con.dispose()

使用ORM方式查询:

python 复制代码
from urllib import parse
from sqlalchemy import create_engine, Table, Column, Integer, String, MetaData

metadata = MetaData()
employees = Table('testtable', metadata,
    Column('id', Integer, primary_key=True),
    Column('name', String),
    Column('email', String),
)

engine  = create_engine('mysql+pymysql://root:root@localhost:3306/testdb?charset=utf8')  
# pd.read_sql(sql,con) 

s = employees.select()

conn = engine.connect()
result = conn.execute(s)

# 处理查询结果
for row in result:
    print(row)

conn.close()
engine.dispose()

插入:

python 复制代码
from urllib import parse
from sqlalchemy import create_engine, Table, Column, Integer, String, MetaData

metadata = MetaData()
employees = Table('testtable', metadata,
    Column('id', Integer, primary_key=True),
    Column('name', String),
    Column('email', String),
)

engine  = create_engine('mysql+pymysql://root:root@localhost:3306/testdb?charset=utf8')  
# pd.read_sql(sql,con) 

ins = employees.insert().values(id=11, name="nameereee", email="kkkkkkkkkk")

# 执行插入语句
conn = engine.connect()
conn.execute(ins)

# 关闭连接
conn.close()
engine.dispose()

删除:

python 复制代码
from sqlalchemy import create_engine, MetaData, Table, Column, Integer, String
engine  = create_engine('mysql+pymysql://root:root@localhost:3306/testdb?charset=utf8')  

metadata = MetaData()
employees = Table('testtable', metadata,
    Column('id', Integer, primary_key=True),
    Column('name', String),
    Column('email', String),
)

conn = engine.connect()
stmt = employees.delete().where(employees.c.name == 'nameereee')
conn.execute(stmt)

s = employees.select()
conn.execute(s).fetchall()

更新:

python 复制代码
from sqlalchemy import create_engine, MetaData, Table, Column, Integer, String
engine  = create_engine('mysql+pymysql://root:root@localhost:3306/testdb?charset=utf8')  

metadata = MetaData()
employees = Table('testtable', metadata,
    Column('id', Integer, primary_key=True),
    Column('name', String),
    Column('email', String),
)

conn = engine.connect()
stmt=employees.update().where(employees.c.id=='3').values(name='Kapoor')
conn.execute(stmt)

s = employees.select()
conn.execute(s).fetchall()
相关推荐
robin_suli22 分钟前
Spring事务的传播机制
android·java·spring
鸿蒙布道师1 小时前
鸿蒙NEXT开发对象工具类(TS)
android·ios·华为·harmonyos·arkts·鸿蒙系统·huawei
Harrison_zhu2 小时前
Ubuntu18.04 编译 Android7.1代码报错
android
CYRUS STUDIO4 小时前
Unidbg Trace 反 OLLVM 控制流平坦化(fla)
android·汇编·算法·网络安全·逆向·ollvm
扫地的小何尚5 小时前
NVIDIA工业设施数字孪生中的机器人模拟
android·java·c++·链表·语言模型·机器人·gpu
顾林海6 小时前
深度解析ArrayList工作原理
android·java·面试
安静的海岸_AI6 小时前
Android端WIFI/流量共存技术方案
android
_一条咸鱼_6 小时前
Android Compose 框架进度指示器深入剖析(五十二)
android
张风捷特烈7 小时前
Flutter 伪 3D 绘制#02 | 地平面与透视
android·flutter
每次的天空7 小时前
Kotlin 作用域函数:apply、let、run、with、also
android·开发语言·kotlin