2018年5月13日笔记
Posted Karlkiller
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2018年5月13日笔记相关的知识,希望对你有一定的参考价值。
- SQL语句复习
创建一个库 create database DB_name; 授权一个用户 grant all privileges on *.* to ‘user1‘@‘%‘ identified by ‘qwe123‘; 创建表 create table table_name(column_name type not null); 查询 select * from tabel_name where condition1 and condition2; 增加 insert into table_name (id, name, age, sex, grander) values (1, ‘ling‘, 25, ‘M‘, 99), (2, ‘ajing‘, 45, ‘F‘, 88); 改 update table_name set column_name=xx where condition 删除 delete from table_name where condition drop table table_name 联合查询 select a.id, b.name from A a join B b on a.id=b.tid 创建索引 create index idx_库名_表名_列名1_列名2 (列名1, 列名2) 查看sql是否走索引 explain select * from table_name where column_name==‘xxx‘
- 连接数据库复习
1 # python2使用mysqldb 2 # python3使用pymysql 3 4 import pymysql 5 6 # 1.创建连接connection和游标cursor 7 conn = pymysql.connect(host="192.168.2.1", port=3306, user="root", passwd="qwe123", db="test") 8 cus = conn.cursor() 9 10 # 2.执行SQL 11 sql = "select * from Student;" 12 cus.execute(sql) 13 cus.fetchone() 14 cus.fetchmany(size=n) 15 cus.fetchall() 16 17 # 3.关闭游标cursor和连接connection 18 cus.close() 19 conn.close()
- SQLAlchemy复习
1 # 导入 2 from sqlalchemy import create_engine, MetaData, Table, Column, Integer, String 3 from sqlalchemy.orm import sessionmaker 4 from sqlalchemy.ext.declarative import declarative_base 5 6 # 1.创建引擎engine,‘数据库类型+数据库驱动名称://用户名:口令@机器地址:端口号/数据库名‘ 7 engine = create_engine("mysql+pymysql://username:[email protected]:port/db_name") 8 9 # 2.创建会话session 10 DBsession = sessionmaker(bind=engine) 11 session = DBsession() 12 13 # 3.创建表 14 metadata = MetaData(engine) 15 student = Table("Student", metadata, 16 Column("id", Integer, primary_key=True), 17 Column("name", String(50)) 18 ) 19 metadata.reate_all() # 若不存在同名Table,则创建 20 21 # 4.增加 22 # 4.1 创建一个模型类 23 Base =declarative_base() # 创建对象的基类 24 class Student(Base): # 定义一个Student类来表示一张student表,多个表就定义多个class 25 __tablename__ = "student" 26 id = Column(Integer, primary_key=True) 27 name = Column(String(50)) 28 # 4.2 导入模型类,实例化该类 29 student1 = Student(id=1001, name="Karl") # !!! 别忘了"形参=XXX" !!! 30 student2 = Student(id=1002, name="Tom") 31 student3 = Student(id=1003, name="Jack") 32 # 4.3 通过add实例来添加记录 33 session.add(student1) 34 session.add_all([student2, student3]) 35 36 # 5.查询 37 38 # 5.1 filter 与 filer_by 的区别 39 40 # filter可以使用> <等,表示列必须用 表.列 的形式,等于用 ==. 41 session.query(Student).filter(Student.id>1000) 42 # filter不支持组合查询 43 session.query(Student).filter(Student.id>1000).filter(name=="Tom") 44 45 # filter_by可直接写列,不支持> <,等于用 == 46 # filter_by可支持组合查询 47 session.query(Student).filter(id==1000 and name=="Jack") 48 49 # 5.2 模糊查询 50 # SQL语句的模糊查询 51 #select * from student where name like ‘%ling%‘; 52 # python语句的模糊查询 53 session.query(Student).filter(Student.name.like("%ling%")) 54 55 # 5.3 获取数据 56 one() tuple 57 all() list 58 # 在查询中,不写one()或all(),获取的结果就是SQL语句 59 60 # 6.更新 61 # 6.1 先查出来 62 std1 = session.query(Student).filter(Student.id>1000) 63 # 6.2 更新类的属性值 64 std1.name = "testname" 65 # 6.3 commit提交下 66 session.commit() 67 68 # 7.删除 69 # 先查出来,然后调用delete()方法,左后commit() 70 session.delete(std1) 71 72 # 8.统计、分组、排序 73 # 8.1 统计 74 session.query().filter().count() 75 session.query().filter().groupby() 76 session.query().filter().orderby(student.id.desc)
- 将对数据库的操作进行封装
1 from sqlalchemy import create_engine, Integer, String, Column 2 from sqlalchemy.orm import sessionmaker 3 from sqlalchemy.ext.declarative import declarative_base 4 5 6 7 Base = declarative_base() 8 class Student(Base): 9 __tablename__ = ‘student‘ 10 id = Column(Integer, primary_key=True) 11 name = Column(String(50)) 12 age = Column(Integer) 13 address = Column(String(100)) 14 15 # 增 16 def insert(session): 17 student1 = Student(id=1004, name=‘ling‘, age=28, address=‘shanxi‘) 18 session.add(student1) 19 session.commit() 20 21 # 删 22 def delete(session): 23 session.query(Student).filter(Student.id == 1001).delete() 24 session.commit() 25 26 # 改 27 def update(session): 28 student1 = session.query(Student).filter(Student.id == 1001).one() 29 student1.name = ‘test123‘ 30 session.commit() 31 student2 = session.query(Student).filter(Student.id == 1001).one() 32 print(student2.name) 33 34 # 统计 35 def count(session): 36 numnber = session.query(Student).filter().count() 37 print("total student is {0}".format(numnber)) 38 39 # 分组 40 def groupBy(session): 41 groupByAge = session.query(Student).group_by(Student.age).all() 42 print(groupByAge) 43 for i in groupByAge: 44 print(i.id, i.name, i.age, i.address) 45 46 # 排序 47 def orderBy(session): 48 orderByAge = session.query(Student).order_by(Student.age.desc()).all() 49 for x in orderByAge: 50 print(x.id, x.name, x.age, x.address) 51 52 53 54 def main(): 55 engine = create_engine(‘mysql+pymysql://xiang:[email protected]/sqlalchemy‘) 56 DBsession = sessionmaker(bind=engine) 57 session = DBsession() 58 # insert(session) 59 # update(session) 60 # delete(session) 61 # count(session) 62 # groupBy(session) 63 orderBy(session) 64 65 66 if __name__ == ‘__main__‘: 67 main()
以上是关于2018年5月13日笔记的主要内容,如果未能解决你的问题,请参考以下文章