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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

python创建数据库表空间_Python 操作 mysql

發布時間:2025/3/15 数据库 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python创建数据库表空间_Python 操作 mysql 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

"""python 操作mysql時,默認開啟事務,必須在增刪改之后

提交數據,才會真正對數據庫發生變化,默認默認是回滾

提交數據: conn.commit()

回滾數據: conn.rollback()

execute 一次插入一條

executemany 一次插入多條"""

importpymysql#1.創建mysql 鏈接

conn = pymysql.connect(host="127.0.0.1",user="root",password="123456",database="db0826")#查詢數據,默認是元組,可以設置返回的類型為字典 pymysql.cursors.DictCursor

cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) #cursor=pymysql.cursors.DictCursor################ (1) 增

"""sql = "insert into t1(first_name,last_name,age,sex,money) values(%s,%s,%s,%s,%s)"

# 一次插入一條

res = cursor.execute(sql, ("宋","云杰",30,0,15000) )

print(res) # 1

# 獲取最后插入這條數據的id號(針對單條數據插入)

print(cursor.lastrowid) # 3

res = cursor.executemany( sql, [ ("高","云峰",50,1,16000) , ("戈","隆",80,1,17000) , ("袁","偉倬",120,0,130000) , ("劉","欣慰",150,0,18000) ] )

print(res) # 插入的條數

# 針對于多條數據,搜最后的id 可以通過倒序查詢id

sql = "select id from t1 order by id desc limit 1"

res = cursor.execute(sql)

print(res)

# 獲取最后一個id號

res = cursor.fetchone()

print(res)"""

################### 刪

"""sql = "delete from t1 where id = %s"

res = cursor.execute(sql , (3,))

print(res)

if res:

print("刪除成功")

else:

print("刪除失敗")"""

################## 改

"""sql = "update t1 set first_name = %s where id = %s"

res = cursor.execute(sql,("王",4))

print(res)

if res:

print("修改成功")

else:

print("修改失敗")"""

#################### 查

"""fetchone fetchmany fetchall 都是基于上一條數據往下查詢."""sql= "select * from t1"res=cursor.execute(sql)print(res) #總條數

#(1) 獲取一條數據 fetchone#{'id': 4, 'first_name': '王', 'last_name': '云杰', 'age': 30, 'sex': 0, 'money': 15000.0}

res =cursor.fetchone()print(res)#(2) 獲取多條數據 fetchmany

data = cursor.fetchmany() #默認搜索的的是一條數據

print(data)

data= cursor.fetchmany(3)print(data)"""[

{'id': 6, 'first_name': '高', 'last_name': '云峰', 'age': 50, 'sex': 1, 'money': 16000.0},

{'id': 7, 'first_name': '戈', 'last_name': '隆', 'age': 80, 'sex': 1, 'money': 17000.0},

{'id': 8, 'first_name': '袁', 'last_name': '偉倬', 'age': 120, 'sex': 0, 'money': 130000.0}

]"""

for row indata :#print(row)

first_name = row["first_name"]

last_name= row["last_name"]

age= row["age"]if row["sex"] ==0:

sex= "女性"

else:

sex= "男性"money= row["money"]print("姓:{},名:{},年齡:{},姓名:{},收入:{}".format(first_name,last_name,age,sex,money) )#(3) 獲取所有數據 fetchall

"""data = cursor.fetchall()

print(data)"""

#(4) 自定義搜索查詢的位置

sql= "select * from t1 where id >= 20"res=cursor.execute(sql)print(res)"""# 1.相對滾動 (正數相對于當前位置往后滾,負數反之.)

cursor.scroll(3,mode="relative")

res = cursor.fetchone()

print(res)

cursor.scroll(3,mode="relative")

res = cursor.fetchone()

print(res)

# 27 往前滾

cursor.scroll(-2,mode="relative")

res = cursor.fetchone()

print(res)"""

#2.絕對滾動 , 永遠基于第一條數據的位置進行移動

cursor.scroll(0,mode="absolute")print(cursor.fetchone())

cursor.scroll(1,mode="absolute")print(cursor.fetchone())

cursor.scroll(3,mode="absolute")print(cursor.fetchone())#往前滾沒有數據,超出范圍error

"""cursor.scroll(-1,mode="absolute")

print(cursor.fetchone())"""

#在進行增刪改查時,必須提交數據,才會產生影響.

conn.commit()

cursor.close()

conn.close()

總結

以上是生活随笔為你收集整理的python创建数据库表空间_Python 操作 mysql的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。