标签:ring 数据对象 coding update create port 错误 enc mysql
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column,Integer,String #区分大小写
from sqlalchemy.orm import sessionmaker
engine=create_engine("mysql+mysqlconnector://root:root@localhost/quey",encoding=‘utf-8‘,echo=True)
base=declarative_base()
class user(base):
tablename = ‘users‘ #表名
id = Column(Integer, primary_key=True)
name = Column(String(32))
password = Column(String(64))
class class_name(base):
tablename = ‘class‘ # 表名
id = Column(Integer, primary_key=True)
name = Column(String(32))
password = Column(String(64))
password1 = Column(String(64))
base.metadata.create_all(engine) #创建表结构
Session_class=sessionmaker(bind=engine) ##创建与数据库的会话,class,不是实例
Session = Session_class()
result = Session.query(user).first()
print(type(result.id),type(result.name),type(result.password))
print("第一条查询结果:" + str(result.id),result.name,result.password)
result = Session.query(user).filter_by(id=2).first()
print("ID值查询结果:" + str(result.id),result.name,result.password)
result = Session.query(user).filter(user.id>1).limit(5).offset(10).all()
for i in result:
print(i.id,i.name,i.password)
result = Session.query(user).get(3)
print(result.name)
news = user(name="我是",password="新增测试标题")
Session.add(news)
Session.commit()
Session.query(user).filter(user.id==7).update({"password":"修改之后的标题"})
Session.commit()
python+ sqlalchemy实现orm创建表和查询操作
标签:ring 数据对象 coding update create port 错误 enc mysql
原文地址:https://www.cnblogs.com/zhangyu-zhj/p/13198476.html