#获取数据库 from sqlalchemy import create_engine db = create_engine("sqlite:///:memory:", echo=True) #创建表 db.execute(""" create table users( userid char(10), username char(50) )""" ) # 插入记录 resultProxy = db.execute("""insert into users (userid,username) values('user1','tony')""") resultProxy.rowcount #return rows affected by an UPDATE or DELETE statement # 查询 resultProxy = db.execute("""select * from users""") resultProxy.rowcount #return rows affected by an UPDATE or DELETE statement. resultProxy.scalar() # 可以返回一个标量查询的值 #ResultProxy 类是对Cursor类的封装(在文件sqlalchemyenginease.py), #ResultProxy 类有个属性cursor即对应着原来的cursor. #ResultProxy 类有很多方法对应着Cursor类的方法, 另外有扩展了一些属性/方法. #resultProxy.fetchall() #resultProxy.fetchmany() #resultProxy.fetchone() #resultProxy.first() #resultProxy.returns_rows #True if this ResultProxy returns rows. resultProxy.close() # resultProxy 用完之后, 需要close