MySQL(15) 與python互動的增刪改查

2021-08-21 04:52:17 字數 1464 閱讀 7288

1.

增加、刪除、修改資料

建立test.py檔案,向學生表中插入一條資料、刪除一條資料、修改一條資料

#encoding=utf-8

import pymysql

try:

conn=pymysql.connect(host='localhost',port=3306,db='test1',user='root',passwd='mysql',charset='utf8')

#通過connection類建立連線物件conn

cs1=conn.cursor()

#通過物件conn物件建立游標物件cs1

count=cs1.execute("insert into students(sname) values('張良')")   #增加

count=cs1.execute("delete from students where id=6")          #刪除

count=cs1.execute("update students set sname='劉邦' where id=7") #修改

print(count)

conn.commit()

cs1.close()

#關閉游標物件

conn.close()

#關閉連線物件

except exception as e:

print(e.message)

#捕獲異常並輸出

2.查詢資料

建立testselect.py檔案,查詢一條學生資訊

#encoding=utf8

import pymysql

try:

conn=pymysql.connect(host='localhost',port=3306,db='test1',user='root',passwd='mysql',charset='utf8')

cur=conn.cursor()

#查詢一行資料

cur.execute('select * from students where id=3')

result=cur.fetchone()

print(result)

#查詢多行資料

cur.execute('select * from students')

result=cur.fetchall()

print(result)

cur.close()

conn.close()

except exception as e:

print(e.message)

3.crud操作的含義

crud是指在做計算處理時的增加(create)、讀取查詢(retrieve)、更新(update)和刪除(delete)幾個單詞的首字母簡寫,所以crud操作即為增刪改查操作。

MYSQL儲存過程與8842020函式交替

二 儲存過程與儲存函式 語法如下 create procedure sp name proc parameter characteristic routine body create function sp name func parameter returns type characteristi...

搭建集群,配置機器,建立集群,與python互動

總結 三個 件的配置區別在port pidfile cluster config file三項 redis server 7000.conf redis server 7001.conf redis server 7002.conf 檢視程序如下圖 總結 三個 件的配置區別在port pidfile...

學python 15 MySQL基本介紹

一,基本概念 1,常用的兩種引擎 1 innodb a,支援acid,簡單地說就是支援事務完整性 一致性 b,支援行鎖,以及類似oracle的一致性讀,多使用者併發 c,獨有的聚集索引主鍵設計方式,可大幅提公升併發讀寫效能 d,支援外來鍵 e,支援崩潰資料自修復 innodb設計目標是處理大容量資料...