python43 作业知识点补充 Python的pymysql的操作

昨日内容:
 
 一. 外键的变种  (*************)
  
  唯一索引
  
   unique('name') : 此列是不能重复的
   联合唯一索引:
    unique('name', 'age') : 这两列的值不能重复
  
  一对多
   department:
    id    depart_name
     1      公关部
     2      xxx 部
    
   user:
    id     username   depart_id (外键的约束)
     1       root        1
     2       root2       2
     3       root3       1
  
  一对一
   
   看业务的需求
    user:
     id     username   depart_id (外键的约束)
      1       root        1
      2       root2       2
      3       root3       1
    
    blog:
     id      url        user_id (唯一 + 外键约束)
     1       /root/       1
     2       /root3/      3
   
  多对多
   
   user:
    
    id     username  
     1       root       
     2       root2      
     3       root3  
   
   
   host:
    id      hostname
     1       c1.com
     2       c2.com
     3       c3.com
    
   
   user2host:
    
    id      uid    hid  (联合唯一 + 外键)
     1       1      1
     2       1      2
     3       3      1
     4       3      3
   
   
  
 
 二. 数据行的详细操作
  
  
  增:
   insert into t1 (name, age) values ('lxxx', 12);
   insert into t1 (name, age) values ('lxxx', 12), ('xxxxx', 13), ('xxxxxx', 13);
   insert into t1 (name, age) select name, age from t2;
   
  删:
   delete from t1;
   delete from t1 where name='xxx' and age!=12;
   
  更新:
   update  t1 set name='xxx', age=123 where name='xxxx' and age=13;
   
  查询:
   
   通配符:
    like name '李'
    % : 匹配所有
    _ : 匹配一个字符
   
   限制取数据:
    limit 从第几(索引)条开始取, 去多少条数据
    
    分页: (***********)
     page = 1,2,3,4,5...n
     offset = 10
     
     limit (page-1)*offset, offset;
   
   排序:
    order by 列名 asc(升序), desc(降序)
   
   
   分组:
    将数据按照某一列进行分组
    
    group by +  聚合函数 (count/sum()/avg()/max()/min())
    
    having
    
   连表:
    left join :
     左边的全部显示
     
    right join:
     右边的全部显示
    
    left join

今日内容

1.作业补漏知识点

2.安装Pymysql(Python操作mysql)

详情

1.作业补漏知识点

#cmd执行
可用快捷应用程序:
navcate formysql
1.distint去重
使用方法:全大写使用 DISTINT
案例:
select DISTINCT student.sid,student.sname from score
left join student
on score.student_id=student.sid where score.number>60;
2.自连接:自己连接自己,---现象原因表的框架不合理
(给一个表名起两个别名,方便连接)
select s1.sid,s1.course_id from score as s1,score as s2
where s1.coure_id!=s2.coure_id and s1.number=s2.number;
3.tee:重定向导入某一个文件
#cmd SQL语句指令
例:
tee D:/a.text
    #将下面创建所有的内容全部导入该路径(D:/a.text)

2.安装Pymysql(Python操作mysql)

#安装:
#cmd执行命令:
------:pip3 install pymysql
   
   
#连接
#pycharm操作
import pymysql
#连接服务器
conn=pymysql.connect(host='localhost',user='root',password='llx20190411',database='db1',charset='utf8')
#游标--方便拿出资料
cursor=conn.cursor(cursor=pymysql.cursors.DicCursor)
#输入执行SQL语句
sql=input("sql:")
cursor.execute(sql)
#*****删除和更新(修改)的时候,需要“事物”提交
conn.commit()
#取一行的内容
res1=cursor.fetchone()
print(res1)
#取n行的内容可设置
res2=cursor.ferchmany(n)
print(res2)
#取表里的全部内容
res3=cursor.ferchall()
print(res3)
'''
#游标--方便拿出资料
cursor=conn.cursor(cursor=pymysql.cursors.DicCursor)#设置条件后
#取表里的全部内容
res3=cursor.ferchall()#结果是列表里套字典
'''
cursor.close()#断开游标
conn.close()#断开连接

#注意
'''
ps:
  a.conn,cursor用完了需要关闭连接
  b.查询的时候,ferchone,ferchmany,ferchall,
  默认返回是元组(游标cursor=conn.cursor()的时候)
  需要返回字典的话:
 cursor=conn.cursor(cursor=pymysql.cursors.DictCursor)
  c.删除和更新的时候需要在execute之后,添加conn.commit()
  '''
'''
例:
#输入执行SQL语句
sql=input("sql:")
cursor.execute(sql)
#*****删除和更新(修改)的时候,需要“事物”提交
conn.commit()
'''

猜你喜欢

转载自www.cnblogs.com/llx--20190411/p/11025667.html
今日推荐