Python之MySQL基本操作

2021-10-06 21:02:08 字數 4353 閱讀 1143

import pymysql
# 開啟資料庫鏈結

conn = pymysql.connect(

'localhost'

, user=

'root'

, password=

'root'

, db=

'testdb'

)# 游標

cursor = conn.cursor(

)

# # 建立資料庫

# cursor.execute("create database if not exists pythondb default charset utf8mb4")

# # 刪除資料庫

# cursor.execute("drop database pythondb")

# 建立 user表

cursor.execute(

'drop table if exists user'

)sql =

"""create table if not exists `user` (

`id` int not null auto_increment,

`name` varchar(255) not null,

`age` int(11) not null,

primary key (`id`))

engine=innodb default charset=utf8mb4"""

cursor.execute(sql)

0
# 插入單條

# way 1

inserted_data = cursor.execute(

"insert into user (id, name, age) values (1, 'zhf', 25)"

)# way 2

sql =

"insert into user values(%s, %s, %s)"

cursor.execute(sql,(2

,'wjq',25

))# 插入多條

sql =

"insert into user values(%s, %s, %s)"

cursor.executemany(sql,[(

3,'tom',25

),(4

,'tim'

,'26'),

(5,'k20',20

)])conn.commit(

)

# 查詢單條, 返回 tuple

cursor.execute(

"select * from user"

)res = cursor.fetchone(

)print

(res)

(1, 'zhf', 25)
# 查詢多條, 返回 ((), (), ..., ())

cursor.execute(

"select * from user"

)res = cursor.fetchmany(2)

print

(res)

((1, 'zhf', 25), (2, 'wjq', 25))
# 查詢所有資料, 返回 ((), (), ..., ())

cursor.execute(

"select * from user"

)res = cursor.fetchall(

)for r in res:

print

(r)

(1, 'zhf', 25)

(2, 'wjq', 25)

(3, 'tom', 25)

(4, 'tim', 26)

(5, 'k20', 20)

# 更新單條

cursor.execute(

"select * from user where name='zhf'"

)res = cursor.fetchone(

)print

('更新前資料:'

, res)

cursor.execute(

"update user set age=24 where name='zhf'"

)# 跟新資料

conn.commit(

)cursor.execute(

"select * from user where name='zhf'"

)res = cursor.fetchone(

)print

('更新後資料:'

, res)

更新前資料: (1, 'zhf', 25)

更新後資料: (1, 'zhf', 24)

# 更新多條

cursor.execute(

"select * from user where name in ('zhf', 'wjq')"

)for res in cursor.fetchall():

print

('更新前資料:'

, res)

sql =

"update user set age=%s where name=%s"

cursor.executemany(sql,[(

18,'wjq'),

(25,'zhf')]

)# 更新資料

conn.commit(

)cursor.execute(

"select * from user where name in ('zhf', 'wjq')"

)for res in cursor.fetchall():

print

('更新後資料:'

, res)

更新前資料: (1, 'zhf', 24)

更新前資料: (2, 'wjq', 25)

更新後資料: (1, 'zhf', 25)

更新後資料: (2, 'wjq', 18)

# 刪除單條

cursor.execute(

"delete from user where id=5"

)conn.commit(

)print

('刪除後資料:'

)cursor.execute(

"select * from user"

)for res in cursor.fetchall():

print

(res)

刪除後資料:

(1, 'zhf', 25)

(2, 'wjq', 18)

(3, 'tom', 25)

(4, 'tim', 26)

# 刪除2條資料

sql=

"delete from user where name=%s"

cursor.executemany(sql,[(

'tom'),

('tim')]

)conn.commit(

)print

('刪除後資料:'

)cursor.execute(

"select * from user"

)for res in cursor.fetchall():

print

(res)

刪除後資料:

(1, 'zhf', 25)

(2, 'wjq', 18)

# 刪除所有資料

cursor.execute(

"delete from user"

)conn.commit(

)print

('刪除所有資料:'

)cursor.execute(

"select * from user"

)res = cursor.fetchall(

)print

(res)

刪除所有資料:

()

cursor.close(

)# 關閉游標

conn.close(

)# 關閉鏈結

python模組之heapq模組(堆)基本操作

1 匯入模組 import heapq 匯入模組2 heapq 提供的常用方法 heapq.heapify head 將陣列轉化成堆 刪除堆頂,也就是最小值 往堆中增元素 heapq.nlargest n,head 查堆中最大的n個數 heapq.nsmallest n,head 查堆中最小的n個數...

php操作mysql命令 Mysql基本操作命令

登陸資料庫mysql uroot p123 建立資料庫createdatabase資料庫 檢視所有資料庫showdatabases 開啟資料庫use資料庫名 刪除資料庫drop database資料庫名 建立表create table表名 列名資料型別,列名資料型別,檢視當前資料庫下所有表show ...

python的操作步驟 python基本操作

一 python介紹 1 什麼是python python 是乙個高層次的結合了解釋性 編譯性 互動性和面向 物件的指令碼語言。2 什麼是物件導向 這意味著python支援物件導向的風格或 封裝在 物件的程式設計技術。3 什麼是解釋型別 這意味著開發過程中沒有了編譯這個環節。4 什麼是動態型別 5 ...