日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 运维知识 > 数据库 >内容正文

数据库

MySQL -Naivacat工具与pymysql模块

發(fā)布時(shí)間:2025/3/15 数据库 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 MySQL -Naivacat工具与pymysql模块 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Navicat

在生產(chǎn)環(huán)境中操作MySQL數(shù)據(jù)庫(kù)還是推薦使用命令行工具mysql,但在我們自己開發(fā)測(cè)試時(shí),可以使用可視化工具Navicat,以圖形界面的形式操作MySQL數(shù)據(jù)庫(kù)。

官網(wǎng)下載:https://www.navicat.com/en/products/navicat-for-mysql

網(wǎng)盤下載:https://pan.baidu.com/s/1bpo5mqj

需要掌握的基本操作

掌握:

1. 測(cè)試+鏈接數(shù)據(jù)庫(kù)

2. 新建庫(kù)

3. 新建表,新增字段+類型+約束

4. 設(shè)計(jì)表:外鍵

5. 新建查詢

6. 備份庫(kù)/表

?

?

#注意:

批量加注釋:ctrl+?鍵

批量去注釋:ctrl+shift+?鍵

?

pymysql模塊

一 介紹

之前我們都是通過(guò)MySQL自帶的命令行客戶端工具mysql來(lái)操作數(shù)據(jù)庫(kù),那如何在python程序中操作數(shù)據(jù)庫(kù)呢?這就用到了pymysql模塊,該模塊本質(zhì)就是一個(gè)套接字客戶端軟件,使用前需要事先安裝

pip3 install pymysql

or

python -m pip install pymysql

?

二 鏈接、執(zhí)行sql、關(guān)閉(游標(biāo))

1 import pymysql 2 3 usr = input('user>>:').strip() 4 pwd = input('password>>:').strip() 5 6 #建立鏈接 7 conn = pymysql.connect( 8 host = 'localhost', 9 port = 3306, 10 user = 'root', 11 db = 'db10', 12 charset = 'utf8' 13 ) 14 15 #拿到游標(biāo) 16 cursor = conn.cursor() 17 18 #執(zhí)行sql語(yǔ)句: 19 sql = 'select * from userinfo where user="%s" and pwd="%s"'%(usr,pwd) #注意%s需要加引號(hào) 20 print(sql) 21 rows=cursor.execute(sql) 22 print(rows) 23 24 cursor.close() 25 conn.close() 26 27 if rows: 28 print('登錄成功') 29 else: 30 print('登錄失敗')

三 execute()之sql注入

注意:符號(hào)--會(huì)注釋掉它之后的sql,正確的語(yǔ)法:--后至少有一個(gè)任意字符

根本原理:就根據(jù)程序的字符串拼接name='%s',我們輸入一個(gè)xxx' -- haha,用我們輸入的xxx加'在程序中拼接成一個(gè)判斷條件name='xxx' -- haha'

最后那一個(gè)空格,在一條sql語(yǔ)句中如果遇到select * from t1 where id > 3 -- and name='egon';則--之后的條件被注釋掉了 #1、sql注入之:用戶存在,繞過(guò)密碼 egon' -- 任意字符#2、sql注入之:用戶不存在,繞過(guò)用戶與密碼 xxx' or 1=1 -- 任意字符

解決方法:

# 原來(lái)是我們對(duì)sql進(jìn)行字符串拼接 # sql="select * from userinfo where name='%s' and password='%s'" %(user,pwd) # print(sql) # res=cursor.execute(sql)#改寫為(execute幫我們做字符串拼接,我們無(wú)需且一定不能再為%s加引號(hào)了) sql="select * from userinfo where name=%s and password=%s" #!!!注意%s需要去掉引號(hào),因?yàn)閜ymysql會(huì)自動(dòng)為我們加上 res=cursor.execute(sql,[user,pwd]) #pymysql模塊自動(dòng)幫我們解決sql注入的問(wèn)題,只要我們按照pymysql的規(guī)矩來(lái)。

?四 增、刪、改:conn.commit()

import pymysql #鏈接 conn=pymysql.connect(host='localhost',user='root',password='123',database='egon') #游標(biāo) cursor=conn.cursor()#執(zhí)行sql語(yǔ)句 #part1 # sql='insert into userinfo(name,password) values("root","123456");' # res=cursor.execute(sql) #執(zhí)行sql語(yǔ)句,返回sql影響成功的行數(shù) # print(res)#part2 # sql='insert into userinfo(name,password) values(%s,%s);' # res=cursor.execute(sql,("root","123456")) #執(zhí)行sql語(yǔ)句,返回sql影響成功的行數(shù) # print(res)#part3 sql='insert into userinfo(name,password) values(%s,%s);' res=cursor.executemany(sql,[("root","123456"),("lhf","12356"),("eee","156")]) #執(zhí)行sql語(yǔ)句,返回sql影響成功的行數(shù) print(res)conn.commit() #提交后才發(fā)現(xiàn)表中插入記錄成功 cursor.close() conn.close()

五 查:fetchone,fetchmany,fetchall

import pymysql #鏈接 conn=pymysql.connect(host='localhost',user='root',password='123',database='egon') #游標(biāo) cursor=conn.cursor()#執(zhí)行sql語(yǔ)句 sql='select * from userinfo;' rows=cursor.execute(sql) #執(zhí)行sql語(yǔ)句,返回sql影響成功的行數(shù)rows,將結(jié)果放入一個(gè)集合,等待被查詢# cursor.scroll(3,mode='absolute') # 相對(duì)絕對(duì)位置移動(dòng) # cursor.scroll(3,mode='relative') # 相對(duì)當(dāng)前位置移動(dòng) res1=cursor.fetchone() res2=cursor.fetchone() res3=cursor.fetchone() res4=cursor.fetchmany(2) res5=cursor.fetchall() print(res1) print(res2) print(res3) print(res4) print(res5) print('%s rows in set (0.00 sec)' %rows)conn.commit() #提交后才發(fā)現(xiàn)表中插入記錄成功 cursor.close() conn.close()''' (1, 'root', '123456') (2, 'root', '123456') (3, 'root', '123456') ((4, 'root', '123456'), (5, 'root', '123456')) ((6, 'root', '123456'), (7, 'lhf', '12356'), (8, 'eee', '156')) rows in set (0.00 sec) '''

五 獲取插入的最后一條數(shù)據(jù)的自增ID

import pymysql conn=pymysql.connect(host='localhost',user='root',password='123',database='egon') cursor=conn.cursor()sql='insert into userinfo(name,password) values("xxx","123");' rows=cursor.execute(sql) print(cursor.lastrowid) #在插入語(yǔ)句后查看conn.commit()cursor.close() conn.close()

?

轉(zhuǎn)載于:https://www.cnblogs.com/lukechenblogs/p/8807356.html

總結(jié)

以上是生活随笔為你收集整理的MySQL -Naivacat工具与pymysql模块的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。