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的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 指针:调用自定义排序函数sort,对输入
- 下一篇: 指针:自定义函数sumDiff(),调用