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')