Python學習筆記_Mysql數據庫、Excel

1、操做mysql數據庫
mysql

import pymysqlsql

# 1.連上數據庫:帳號,密碼,ip,端口號,數據庫數據庫

# 2.創建遊標(去數據庫拿東西的工人)app

# 3.執行sqlide

# 4.獲取結果函數

# 5.關閉遊標fetch

# 6.鏈接關閉spa

coon = pymysql.connect(excel

    host='xxx.xxx.xxx.xxx',user='xxx',passwd='123456',port=3306,db='xxx',charset='utf8')  ##port必須是int類型,charset必須是utf8,而不是utf-8code

cur = coon.cursor()  #創建遊標

cur.execute('select * from stu;')

cur.execute('insert into stu (id,name,sex) value (33,"6j","女")')

coon.commit()  ##必需要commit

res = cur.fetchall() #獲取全部返回的結果

print(res)

cur.close()  #關閉遊標

coon.close()  #關閉數據庫

 

定義數據庫函數

 1 def my_db(host,user,passwd,db,sql,port=3306,charset='utf-8'):
 2 
 3     import pymysql
 4 
 5     coon =pymysql.connect(
 6 
 7         user=user,passwd=passwd,host=host,port=port,
 8 
 9         db=db,charset=charset
10 
11     )
12 
13     cur = coon.cursor()
14 
15     cur.execute(sql)
16 
17     if sql.strip()[:6].upper()=='SELECT':
18 
19         res = cur.fetchall()
20 
21     else:
22 
23         coon.commit()
24 
25         res = 'OK'
26 
27     cur.close()
28 
29     coon.close()
30 
31     return res
View Code

 Tips:

1. 能夠這樣定義變量:一次性的定義多個變量

2. 操做數據庫執行sql時,若是查詢條件是字符串,先用佔位符%s佔位,而後在後面寫上字符串,以下所示:

res = my_db('select password from liujing where username = "%s" and password = "%s";'%(username_login,m_pwd_login))

3. 指定sql返回結果是字典類型:創建遊標的時候,能夠指定遊標類型返回的就是一個字典。mydb返回的是一個list,list內容是字典。

cur = coon.cursor(cursor=pymysql.cursors.DicCursor)

4. fetchall()  #獲取到這個sql執行的所有結果,它把數據庫表裏面的每一行數據放到一個list裏面

fetchone() #  獲取到這個sql執行的一條結果,它返回的就只有一條數據

若是sql語句執行的結果是多條數據的時候,那就用fetchall()

若是你能肯定sql執行的結果是一條數據的時候,那就用fetchone()

5. 要動態獲取到標的字段 cur.descrption能獲取到表結構,進而獲取表的字段

fileds = [filed[0] for filed in cur.description]

6.enumerate([list,list2])  #循環的時候,可以直接獲取到下標和該下標所表明的值

fileds = ['id','name','sex','addr','gold','score']

for index,filed in enumerate(fileds):

    print(index,filed)    #index在循環的時候會自動加一

  

 

 

2、操做excel

寫excel

 1 import xlwt
 2 
 3 book = xlwt.Workbook() #新建一個excel
 4 
 5 sheet = book.add_sheet('sheet1') # 加sheet頁
 6 
 7 sheet.write(0,0,'姓名')  #第一行,第一列,寫入的內容
 8 
 9 sheet.write(0,1,'年齡')  #第一行,第二列,寫入的內容
10 
11 sheet.write(0,2,'性別')  #第一行,第三列,寫入的內容
12 
13 book.save('stu.xls')  #結尾必定要用xls

 

讀excel

 1 import xlrd
 2 book = xlrd.open_workbook('app_student.xls')  #打開excel
 3 sheet = book.sheet_by_index(0) #根據sheet頁的順序選擇sheet頁
 4 sheet2 = book.sheet_by_name('sheet1') #根據sheet頁名稱選擇sheet頁
 5 print(sheet.cell(0,0).value)  #獲取sheet頁裏面第一行第一列的值
 6 print(sheet.cell(1,0).value)  #獲取sheet頁裏面第二行第一列的值
 7 print(sheet.row_values(0)) #獲取第一行一整行的內容,放到了list裏面
 8 print(sheet.row_values(1)) #獲取第二行一整行的內容,放到了list裏面
 9 print(sheet.nrows) #獲取excel中共有多少行
10 for i in range(sheet.nrows):   ##循環獲取到每行的數據
11     print(sheet.row_values(i))  ##打印每一行的數據
12 print(sheet.ncols) #獲取excel裏面總共有多少列
13 print(sheet.col_values(0))#獲取一整列的內容,放到了list裏面
14 for i in range(sheet.ncols):##循環獲取到每列的數據
15     print(sheet.col_values(i))  #打印每一列的數據

 

修改excel

 1 import xlrd
 2 from xlutils import copy
 3 book = xlrd.open_workbook('app_student.xls') #先用xlrd模塊,打開一個excel
 4 new_book = copy.copy(book) #經過xlutils這個模塊裏面的copy方法,複製一個excel
 5 sheet = new_book.get_sheet(0)  #經過sheet頁的前後獲取某sheet頁
 6 
 7 ##將app_student.xls的字段名稱改成lis = ['編號','名字','性別','年齡','地址','班級','手機號','金幣']裏面的字段名稱
 8 # sheet.write(0,0,'編號') #若是字段不少的話,這樣寫須要不少行代碼
 9 # sheet.write(0,1,'名字')
10 lis = ['編號','名字','性別','年齡','地址','班級','手機號','金幣']
11 # col=0
12 # for l in lis:
13 #     sheet.write(0,col,l)
14 #     col+=1
15 for col,filed in enumerate(lis):
16     sheet.write(0,col,filed)
17 new_book.save('app_student.xls')
相關文章
相關標籤/搜索