Python之旅.第十章.mysql.

來源:互聯網
上載者:User

標籤:last   提交   comm   user   增刪改   ctc   AC   相對   upd   

一、      上節課複習

select concat_ws(":",name,age,sex,post) as info from emp;    # egon:male:18

 

二、      子查詢(一個問題一個問題解決)

把一個查詢語句用括弧括起來,當做另外一條查詢語句的條件去用,稱為子查詢

 

select name from emp where dep_id = (select id from dep where name="技術");  #子查詢

select emp.name from emp inner join dep on emp.dep_id = dep.id where dep.name="技術";  #鏈表

 

#查詢平均年齡在25歲以上的部門名

select name from dep where id in (select dep_id from emp group by dep_id having avg(age) > 25);   #子查詢

select dep.name from emp inner join dep on emp.dep_id = dep.id group by dep.name having avg(age) > 25;  #鏈表

 

#查看不足2人的部門名(子查詢得到的是有人的部門id)

select * from emp where exists (select id from dep where id > 3); #exists用法,當()返回True時,外層查詢語句將進行查詢;當傳回值為False時,外層查詢語句不進行查詢(empty set)

 

#查詢每個部門最新入職的那位員工

select t1.id,t1.name,t1.post,t1.hire_date,t2.post,t2.max_date from emp as t1 inner join (select post,max(hire_date) as max_date from emp group by post) as t2 on t1.post = t2.post where t1.hire_date = t2.max_date;

 

三、      IDE工具(navicat)介紹

1、         ER圖表(Entity-Relationship)

2、         模型;匯出sql

3、         查詢; 格式美化sql

 

四、      pymysql模組(安裝與查詢)

1、安裝pymysql(python專用的mysql用戶端通訊端)

pip3 install pymysql

 

2、mysql 查詢

import pymysql

 

conn=pymysql.connect(

    host=‘127.0.0.1‘,

    port=3306,

    user=‘root‘,

    password=‘jxtz0927‘,

    database=‘db40‘,

    charset=‘utf8‘    #防止亂碼

)

cursor=conn.cursor(pymysql.cursors.DictCursor) #將產生的結果以字典的形式顯示 [{‘Tables_in_db40‘: ‘department‘}, {‘Tables_in_db40‘: ‘employee‘}]

# cursor=conn.cursor()  # ((‘department‘,), (‘employee‘,))

 

# rows=cursor.execute(‘show tables;‘)  #rows 為受影響的行數

rows=cursor.execute(‘select * from class;‘)

print(rows)   #2

 

print(cursor.fetchone())    #一條

print(cursor.fetchmany(2))  #幾條

print(cursor.fetchall())    #全部

 

cursor.scroll(3,‘absolute‘)  #mode=‘absolute‘,絕對模式,從最開始位置向後移三條

cursor.scroll(1,‘relative‘)  #mode=‘relative‘,相對模式,從當前位置向後移一條

 

cursor.close()

conn.close()

 

五、      pymysql模組(防止sql注入問題)

1、錯誤做法, 自行對字串進行拼接,引發sql注入問題 (name= egon‘ --asdfg; name=xxxx‘or 1=1--asdfg)

import pymysql

 

conn=pymysql.connect(

    host=‘127.0.0.1‘,

    port=3306,

    user=‘root‘,

    password=‘123‘,

    database=‘db42‘,

    charset=‘utf8‘

)

 

cursor=conn.cursor(pymysql.cursors.DictCursor)

 

inp_user=input(‘使用者名稱>>:‘).strip() #inp_user=""

inp_pwd=input(‘密碼>>:‘).strip() #inp_pwd=""

sql="select * from user where username=‘%s‘ and password=‘%s‘" %(inp_user,inp_pwd) #自行拼接,引發sql注入問題

print(sql)

 

rows=cursor.execute(sql)

if rows:

    print(‘登入成功‘)

else:

    print(‘登入失敗‘)

 

cursor.close()

conn.close()

 

2、在服務端防止sql注入問題:不要自己拼接字串,讓pymysql模組去拼接

import pymysql

 

conn=pymysql.connect(

    host=‘127.0.0.1‘,

    port=3306,

    user=‘root‘,

    password=‘123‘,

    database=‘db42‘,

    charset=‘utf8‘

)

cursor=conn.cursor(pymysql.cursors.DictCursor)

 

inp_user=input(‘使用者名稱>>:‘).strip() #inp_user=""

inp_pwd=input(‘密碼>>:‘).strip() #inp_pwd=""

sql="select * from user where username=%s and password=%s"

print(sql)

 

rows=cursor.execute(sql,(inp_user,inp_pwd))

if rows:

    print(‘登入成功‘)

else:

    print(‘登入失敗‘)

 

cursor.close()

conn.close()

 

六、      pymysql模組(增刪改)

import pymysql

 

conn=pymysql.connect(

    host=‘127.0.0.1‘,

    port=3306,

    user=‘root‘,

    password=‘123‘,

    database=‘db42‘,

    charset=‘utf8‘

)

cursor=conn.cursor(pymysql.cursors.DictCursor)

 

sql=‘update user set username="alexSB" where id=2‘

rows=cursor.execute(sql) #改資料

print(rows)

print(cursor.lastrowid) 

 

sql=‘insert into user(username,password) values(%s,%s)‘

rows=cursor.executemany(sql,[(‘lwz‘,‘123‘),(‘evia‘,‘455‘),(‘lsd‘,‘333‘)])  #一次插入多行記錄

print(rows)

print(cursor.lastrowid)  #顯示插到哪行了(id)

 

conn.commit()   # 只有commit提交才會完成真正的修改

cursor.close()

conn.close()

Python之旅.第十章.mysql.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.