#導(dǎo)包
import pymysql
class Shu():
#有參構(gòu)造
def __init__(self,id,name,price,author,publish):
self.id=id
self.name = name
self.price = price
self.author = author
self.publish=publish
#無(wú)參構(gòu)造
def __init__(self):
pass
con=pymysql.connect(host='127.0.0.1',user='root',password='123456',database='books')
#用于輸送SQL語(yǔ)句執(zhí)行對(duì)數(shù)據(jù)庫(kù)的操作
cur=con.cursor()
# 增
# result=cur.execute("insert into book values(11,'哈哈哈','33','馮麗娟','北京出版社')")
# if result > 0:
# print("插入成功")
# else:
# print("插入失敗")
# 刪
# result=cur.execute("delete from book where id=4")
# print(result)
# 改
# result=cur.execute("update book set price=10 where price=20")
#
# if result > 0:
# print("修改成功")
# else:
# print("修改失敗")
# 查
result=cur.execute("select * from book")
string=cur.fetchall()[0]#查詢?nèi)?
shu =Shu()
shu.id=string[0]
shu.name=string[1]
shu.price=string[2]
shu.author=string[3]
shu.publish=string[4]
print(shu.id,shu.name,shu.price,shu.author,shu.publish)
Python 對(duì)csv進(jìn)行操作
#python如何導(dǎo)入csv
import csv
with open('book.csv','r',encoding="utf-8") as file:
#讀取全部
# vv=csv.reader(file)
# for i in vv:
# print(i)
#讀取一列
vv = csv.reader(file)
for i in vv:
print(i[1])
Python 對(duì)Excle進(jìn)行操作
import xlrd#導(dǎo)包
data=xlrd.open_workbook('book.xls')#將表 book.xls到處在桌面挑庶,xlrd中的open_workbook-取excel表格內(nèi)容
print(data)#<xlrd.book.Book object at 0x03783670>
sheet=data.sheets()[0]
hang=sheet.nrows#行數(shù)
print(hang)
lie=sheet.ncols#列數(shù)
print(lie)
#輸出表格中所有橫豎列的內(nèi)容
for i in range(hang):
for k in range(lie):
print(sheet.cell(i,k))