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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python操作windows库_python大佬养成计划----win下对数据库的操作

發(fā)布時間:2025/3/15 python 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python操作windows库_python大佬养成计划----win下对数据库的操作 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

數(shù)據(jù)庫

PyMySQL 是在 Python3.x 版本中用于連接 MySQL 服務器的一個庫,Python2中則使用mysqldb。

win系統(tǒng)安裝mysql,詳見鏈接描述

數(shù)據(jù)庫常見命令

mysql -uroot -psheen

登陸數(shù)據(jù)庫

show databases;

顯示所有的數(shù)據(jù)庫

create database sheen;

創(chuàng)建新的數(shù)據(jù)庫sheen

use sheen;

進入數(shù)據(jù)庫sheen

show tables;

顯示sheen里的所有數(shù)據(jù)庫表

create table star(name varchar(30),age int);

創(chuàng)建新的數(shù)據(jù)庫表star,

desc star;

查看數(shù)據(jù)庫表的格式

insert into star VALUES('user1',10);

插入值

select * from star;

顯示star數(shù)據(jù)庫表中的所有內(nèi)容

update star set age=11 where name='user1';

更新star數(shù)據(jù)庫表中user1的年齡為11

delete from star where name='user1';

刪除star數(shù)據(jù)庫表中的user1

drop table star;

刪除star數(shù)據(jù)庫表

drop database sheen;

刪除數(shù)據(jù)庫sheen

連接數(shù)據(jù)庫

此處,保證你有一個名為'sheen'的數(shù)據(jù)庫

import pymysql

#這里注意python2需要導入數(shù)據(jù)庫模塊為mysqldb

#python3需要導入數(shù)據(jù)庫模塊為pymysql

#1.連接數(shù)據(jù)庫

conn = pymysql.connect(host='localhost',user='root',passwd='sheen',

charset='utf8',autocommit=True) #指定操作主機,指定用戶和密碼,編碼格式為'utf8'時,中文才可以顯示,autocommit自動提交對數(shù)據(jù)庫的操作

#2.創(chuàng)建一個游標,用來向數(shù)據(jù)庫發(fā)送指令

cur = conn.cursor()

#3.實現(xiàn)對數(shù)據(jù)庫的增刪改查

##3.1選擇需要操作的數(shù)據(jù)庫

conn.select_db('sheen')

#3.2對數(shù)據(jù)庫內(nèi)容的增刪改查

try:

#添加新的數(shù)據(jù)庫表

# add_table = 'create table star(name varchar(30),age int)' #創(chuàng)建數(shù)據(jù)庫表,內(nèi)容有名字、年齡

# cur.execute(add_table) #執(zhí)行數(shù)據(jù)庫操作命令

#給數(shù)據(jù)庫表添加值

# insert_sqli1 = 'insert into star VALUES ("user3", 100);' #給數(shù)據(jù)庫表更新值

# insert_sqli2 = 'insert into star VALUES ("user4", 100);' #給數(shù)據(jù)庫表更新值

# cur.execute(insert_sqli1)

# cur.execute(insert_sqli2)

#刪除數(shù)據(jù)庫表中的內(nèi)容

# del_table_info = 'delete from star where name="user3"'

# cur.execute(del_table_info)

#批量對數(shù)據(jù)實現(xiàn)增刪改

# users=[("user"+str(i),i)for i in range(100)]

# insert_sqli3 = 'insert into star VALUES (%s,%s);'

# cur.executemany(insert_sqli3,users) #批量添加信息,cur.executemany()

#查看數(shù)據(jù)庫表的信息

select_sql = 'select * from star;'

result = cur.execute(select_sql)

print('查看語句的返回結(jié)果:',result) #返回信息數(shù)目,查看語句的返回結(jié)果: 100

#查看數(shù)據(jù)庫表的內(nèi)容

# cur.fetchone類似與文件的操作f.readline, 每次只讀取一條記錄;

print("此條信息:",cur.fetchone())

print("此條信息:",cur.fetchone())

print("此條信息:",cur.fetchone())

# cur.fetchmany, 類似于f.readlines, 返回的是一個元組;

print("查看5條信息",cur.fetchmany(5)) #從游標位置向后

#cur.fetchall返回的是一個元組;

print("第一次查找所有信息:",cur.fetchall())

cur.scroll(0,mode='absolute') #移動游標位置到數(shù)據(jù)庫表頭

print("第二次查找所有信息:",cur.fetchall())

cur.scroll(-10,mode='relative') #移動游標位置到數(shù)據(jù)庫表倒數(shù)第10個的位置

print("最后10個信息:",cur.fetchall())

except Exception as e:

print("Failed:",e)

else:

print("Success")

# 4. 先關閉游標

cur.close()

# 5. 關閉數(shù)據(jù)庫連接

conn.close()

創(chuàng)建數(shù)據(jù)庫表并添加值

刪除指定值

批量管理

查看

獲取數(shù)據(jù)庫表信息

獲取表得字段名和表頭

字段名是指在以關系模型為數(shù)據(jù)結(jié)構的二維表中每一列的標識。就是數(shù)據(jù)庫表的結(jié)構。

表頭是可以用來索引的鍵值。

import pymysql

conn = pymysql.connect(host='localhost',user='root',passwd='sheen',

charset='utf8',autocommit=True,db='sheen')

with conn: #安全管理器

print("is_open:",conn.open) #Return True if the connection is open

cur = conn.cursor()

res = cur.execute('select * from star;')

desc = cur.description #返回表得格式列內(nèi)容(('name', 253, None, 30, 30, 0, True), ('age', 3, None, 11, 11, 0, True))

print("表得描述:",desc)

print("表頭:",','.join([item[0] for item in desc]))

cur.close()

銀行轉(zhuǎn)賬

原賬號向目標賬號轉(zhuǎn)賬,數(shù)據(jù)寫進數(shù)據(jù)庫內(nèi)。

做此實驗前,保證你有數(shù)據(jù)庫transinfo,里面有數(shù)據(jù)庫表bankdata,數(shù)據(jù)庫表中有賬號數(shù)據(jù)和金額數(shù)據(jù)。

import pymysql

class Trans_money(object):

def __init__(self,source_id,target_id,count):

self.source = source_id

self.target = target_id

self.count = count

self.conn = pymysql.connect(host='localhost',user='root',passwd='sheen',

charset='utf8',db='transinfo') #建立數(shù)據(jù)庫連接

self.cur = self.conn.cursor() #建立游標

def transfer_money(self):

"""

轉(zhuǎn)賬方法:

# 1. source_id帳號是否存在;

# 2. target_id帳號是否存在;

# 3. 是否有足夠的錢

# 4. source_id扣錢

# 5. target_id加錢

# 6. 提交對數(shù)據(jù)庫的操作

"""

self.check_account(self.source) # 1. source_id帳號是否存在;

self.check_account(self.target) # 2. target_id帳號是否存在;

self.enough_money(self.source,self.count) # 3. 是否有足夠的錢

try:

self.reduce_source(self.count) # 4. source_id扣錢

self.increase_source(self.count) # 5. target_id加錢

self.conn.commit() # 6. 提交對數(shù)據(jù)庫的操作

except Exception as e:

self.conn.rollback() #撤銷對于數(shù)據(jù)庫的更改操作, 回滾

else:

print("轉(zhuǎn)賬成功")

def check_account(self,account): #判斷原賬戶是否存在

check_sql = 'select * from bankdata where account=%s;' %(account)

res = self.cur.execute(check_sql)

if res ==1:

return True

else:

print("%s此賬號不存在" %(account))

def enough_money(self,account,money): #判斷原賬戶是否有足夠的錢

affirm_sql = 'select money from bankdata where account=%s;' %(self.source)

re = self.cur.execute(affirm_sql) #返回1,游標位置在money

exist_money = self.cur.fetchone()[0] #查看金額數(shù)目

print("您的賬號有%s元" %(exist_money))

if exist_money>=money:

return True

else:

raise Exception("您的賬號%s沒有足夠的金額,當前余額為%s" %(self.source,exist_money))

def reduce_source(self,money): #扣錢函數(shù)

try:

update_sql = 'update bankdata set money=money-%s where account=%s;' %(money,self.source)

self.cur.execute(update_sql)

except Exception as e:

print("Failed:",e)

def increase_source(self,money): #加錢函數(shù)

try:

update_sql = 'update bankdata set money=money+%s where account=%s;' %(money,self.target)

self.cur.execute(update_sql)

except Exception as e:

print("Failed:",e)

if __name__=='__main__':

tran = Trans_money('6107001','6107002',500)

tran.transfer_money()

前期準備

執(zhí)行結(jié)束

數(shù)據(jù)庫結(jié)果顯示

創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎

總結(jié)

以上是生活随笔為你收集整理的python操作windows库_python大佬养成计划----win下对数据库的操作的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。