數據庫: Python連接數據庫實現增 刪 改 查 操作


 <p><iframe name="ifd" src="https://mnifdv.cn/resource/cnblogs/LearnMysql" frameborder="0" scrolling="auto" width="100%" height="1500"></iframe></p>

 

 

准備好測試的數據庫

  IP: 47.92.31.46

  用戶名: yang

  密碼:    11223344.

  數據庫名字 :  databasetest

  表格:  userinfo

  

 

  這是我雲端電腦安裝的數據庫,大家都可以連接測試

  我設置了權限,只可以增刪改查數據

下載包

  

 

 

  

 

  輸入 pymysql

 

  

 

 

  

 

 

 

 

  

 

 

 

 

  

 

 

 

 

  

 

 

 

 

 

 

新建文件

  

 

 

  

 

 

連接數據庫

  

import pymysql #數據庫


db = pymysql.connect(host="47.92.31.41",user="yang",password="11223344.",database="databasetest",charset="utf8",use_unicode=True)
#只要執行下來就說明連接上了
print("成功連接數據庫")




db.close();#關閉連接

 

 

 

 

 

 

 

 

插入數據

 

  一,插入數據,id是1,用戶名是yang 密碼是11223344

 

  "insert into userinfo "+ " values(1,"+"'"+ "yang" +"'"+ "," +"'"+"11223344"+"'"+")";

 

  insert into userinfo : 插入數據到 userinfo 表格 

 

  1 :id的值,自動遞增

 

  yang : 用戶名

 

  11223344 :密碼

 

  注:一般插入字符串型數據需要在數據兩邊加   '    '

 

  也就是  '數據'

 

  

 

sql = "insert into userinfo " + " values(1," + "'" + "yang" + "'" + "," + "'" + "11223344" + "'" + ")";

cursor = db.cursor()
cursor.execute(sql)
db.commit() # 提交數據

 

 

 

 

 

 

 

 

 

 

運行測試

 

  

 

 

 

 

 

 

 

插入數據(第二種)

 

  

 

sql = "insert into userinfo "+ " values(NULL,"+"'"+ "yangyang" +"'"+ "," +"'"+"00000000"+"'"+")";

 

 

 

 和上面相比 value(NULL.....)

 

設置第一個字段是空,意思是不填寫第一個字段(默認就會自動遞增)

 

 

 

 

 

 

 

 

 

 

 

運行測試

 

 

 

  

 

 

 

 

 

總結上兩種插入數據方式

 

  values里面的值依次填到表格中,如果數據不夠,后面的就不插入數據

 

  

 

 

 

 

 

 

 

插入數據(第三種)

 

  

 

sql = "insert into userinfo (username,password)"+ "values(" +"'"+ "yangyang" +"'"+ "," +"'"+"11111111"+"'"+")";

 

 

 

(username,password) 后面的values里面的數據對應插到哪個字段里面

 

yangyang 插入到 username 字段里面

 

11111111 插入到 password 字段里面

 

 

 

 

 

 

 

 

 

運行測試

 

 
        

 

  

 

刪除數據

 

sql = "delete from userinfo "+" where "+"username = "+"'"+"yang" +"'";

 

刪除 userinfo表格中 username字段中是 yang的數據

 

意思就是刪除,下面這條數據

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

運行測試

 

 

 

  

 

 

 

 

 

 

 

sql = "delete from userinfo "+" where "+"username = "+"'"+"yangyang" +"'"+ " and " +"password ="+"'"+"00000000"+"'";

 

刪除 userinfo表格中 username字段中是 yangyang,同時password字段中是 00000000 的數據

 

 

 

 

 

 

 

 

 

其實就是刪除這個

 

 

 

  

 

 

 

 

 

 

 

運行測試

 

 

 

  

 

 

 

 

 

 

 

 

 

 

 

修改數據

 

  

 

sql = "update userinfo set password="+"'"+"888888"+"'" +"where username="+"'"+"yangyang"+"'";

 

 

 

 找到userinfo表格中username字段值是yangyang的

 

然后把 password字段的值修改為 888888

 

其實就是把11111111修改為888888

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

運行測試

 

 

 

  

 

 

 

 

 

 

 

 

 

當然也可以直接設置

 

sql = "update userinfo set password="+"'"+"888888"+"'";

 

 

 

 

 

查詢數據

 

sql = "select *from userinfo";#查詢表格中的所有數據

try:
cursor = db.cursor()
cursor.execute(sql)
# 獲取所有記錄列表
results = cursor.fetchall()
for row in results:
print (row[0],row[1],row[2])
except:
print ("error")

 

 

 

 

 測試

 

  

 

 

 

 

 

 

 

 為直觀看到下面的測試增加一個用戶名和密碼

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

查詢表格中的所有username字段的值

 

復制代碼
sql = "select username from userinfo";#查詢表格中的所有username字段的值
try:
cursor = db.cursor()
cursor.execute(sql)
# 獲取所有記錄列表
results = cursor.fetchall()
for row in results:
print (row[0])
except:
print ("error")
復制代碼

 

 
        

 

 

 

 運行測試

 

  

 

 

 

查詢表格中的密碼是666666的用戶名

 

 

 

復制代碼
  sql = "select username from userinfo where password = 666666";//查詢表格中的密碼是666666的用戶名
復制代碼

 

 

 

運行測試

 

  

 

 

 

//查詢表格中password是666666的所有數據

 

復制代碼
sql = "select *from userinfo where password = 666666";#查詢表格中password是666666的所有用戶信息
try:
cursor = db.cursor()
cursor.execute(sql)
# 獲取所有記錄列表
results = cursor.fetchall()
for row in results:
print (row[0],row[1],row[2])
except:
print ("error")
復制代碼

 

 

 

運行測試

 

  

 

 

補充

  若想讓數據庫支持斷線重連

  

                try:
                    cursor.execute(sql)
                    db.commit()  # 提交數據
                except Exception as e:
                    try:
                        db.ping();
                    except Exception as e:
                        db.connect();
                        cursor.execute(sql)
                        db.commit()  # 提交數據

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM