赞
踩
语法:
create database db;
示例:
创建test数据库
create database test;
示例:
创建admin(密码为123456)的用户,并授权,带可授权权限的
grant all privileges on *.* to 'admin'@'%' identified by '123456' with grant option;
示例:
创建student表,包含id(int类型),name(varchar),age(int类型),sex(枚举)
create table student(id int not null auto_increment primary_key,name varchar(50),age int,sex enum('F','G'));
update 表名 set 字段名=新值 where 条件1;
示例:
将zhangsan的年龄修改为22
update student set age=22 where name='zhangsan';
语法:
delete from 表名 where 条件1;
示例:
删除name为test03数据
delete from student where name='test03';
语法:
explain sql语句;
示例:
explain select * from student;
2.过滤filter_by()
one()返回的结果是元组tuple()
all()返回的结果是list,list里面包含若干个元组tuple
统计年龄为22的人数
from sqlalchemy import create_engine, Column, String, Enum, Integer
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
base=declarative_base()
class Student(base):
__tablename__='student'
id=Column(Integer,primary_key=True)
name=Column(String(50))
age=Column(Integer)
sex=Column(Enum('F','G'))
def select(session):
number = session.query(Student).filter(Student.age==22).count()
print(number)
def main():
engine = create_engine('mysql+pymysql://root:123456@127.0.0.1/test')
# 创建session工厂
DBSession = sessionmaker(bind=engine)
# 创建一个session
session = DBSession()
#查询
select(session)
if __name__ == '__main__':
main()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。