1、执行原生SQL
from sqlalchemy import create_engine, text # 创建engine对象 engine = create_engine("sqlite:///demo.db", echo=False) with engine.connect() as con: # 先删除persons表 con.execute(text('drop table if exists persons')) # 创建一个persons表,有自增长的id和name,age con.execute(text( "CREATE TABLE persons (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,name VARCHAR (32),age REAL,datetime TEXT NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%S+08:00','now','localtime')))")) # 插入两条数据到表中 con.execute(text('insert into persons(name,age) values("张三","20")')) con.execute(text('insert into persons(name,age) values("李四","30")')) # 执行查询操作 results = con.execute(text('select * from persons')) # 获取结果 # datares = results.fetchall() # 将结果转换为字典格式 data = [dict(zip(results.keys(), result)) for result in results.fetchall()] print(data)
标签:engine,execute,SQLAlchemy,text,results,persons,模块,con From: https://www.cnblogs.com/xwupiaomiao/p/17685192.html