UI自动化+Android+服务器上数据进行操作+mysql的增删改查

我们都知道我们的数据都在数据库上

我们同样要对我们的数据进行操作,我们怎么搞呢

我们首先要知道mysql.db这个东西,这是我们链接数据库的包

然而我们发现这个东西一直报错,经过编者的仔细寻找,终于解决了

这里:我只说mac本的方法

第一步我们需要下载一个brew,打开终端输入命令:ruby -e "$(curl —insecure -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

第二步输入命令:brew install mysql

第三步命令:pip install MySQL-python==1.2.5

完成后我们打开我们的Python就不会报错了

我们开始我们的增删改查的操作附上代码:

select.py

#-*-coding:utf-8-*-
#导入mysql包
import MySQLdb
#打开数据库连接
#1.host主机的ip
#2.user数据库用户名
#3.passwd数据库密码
#4.db数据库名字
#5.port数据库端口号默认3306
#如果设置允许远程连接数据库,在mysql库里面,user表host字段内容改成%,同时刷新数据库flush privileges
host="39.107.102.34"
#数据库用户名
user="root"
#数据库密码
password="123456"
#数据库名字
database='qiezi'
#数据库端口号
port=3306
db=MySQLdb.connect(host,user,password,database,port)
#获取curse
curse=db.cursor()
#1.执行sql语句,返回所有的列使用*
# sql="select * from qiezzilogin where userId=1"
# curse.execute(sql)
# #获取列表
# persons=curse.fetchall()
# #for循环打印
# for person in persons:
#     print person[0],person[1],person[2],person[3]
#2.模糊查询所有,关键字段like ,这种情况下索引是起作用的,但是如果%放在前面索引就不会起作用的,索引会无效
sql="select * from qiezzilogin where username like 'zhangsan3000%'"
curse.execute(sql)
persons=curse.fetchall()
for person in persons:
    print person[0],person[1],person[2],person[3]



#3.返回指定的列表,比如返回userId username
# sql="select userId,username from qiezzilogin where userId=1"
# curse.execute(sql)
# persons=curse.fetchall()
# for person in persons:
#     print person[0],person[1]

#4.模糊查询倒叙返回 关键字desc 顺序asc #需要配合使用 order by 需要倒叙或者顺序的字段DESC|ASC 注意默认是升序返回 #倒叙 # sql="select * from qiezzilogin where username like 'zhangsan3000%'order by username DESC " #同时满足两个条件的查询语句,同时返回数据 sql="select * from qiezzilogin where username='zhangsan1' and userId=2" #只满足一个条件就返回数据 # sql="select * from qiezzilogin where username='zhangsan1' or userId=1 order by userId DESC " staus=curse.execute(sql) print staus persons=curse.fetchall() for person in persons: print person[0],person[1],person[2],person[3] 

insert.py
 

#-*- coding:utf-8 -*-

# 导入mysql
import MySQLdb

# host
host = '39.107.102.34'

# 用户名
user = 'root'

# 密码
pwd = '123456'

# 数据库
database = '1509E'

# 端口号
port = 3306

# 链接数据库
db = MySQLdb.connect(host,user,pwd,database,port)

# 或许游标
course = db.cursor()
#增加的sql语句
#格式 insert into 表明(增加的字段的顺序,也可以不写)values(字段)
sql="insert into apps values(0,'李四','www.baidu.com','CN')"
# sql="insert into qiezzilogin(username,password,token) VALUES ('李四1','123456','22222222222')"
try:
    #执行增加
    status=course.execute(sql)
    print status
    #提交
    db.commit()
except:
    #回滚
    db.rollback()
#查询出来
sql1="select * from apps where app_name='李四'"
course.execute(sql1)
#查询数据
persons=course.fetchall()
for person in persons:
    print person[0],person[1],person[2],person[3]

#关闭数据库
db.close()













delete.py
 
#-*- coding:utf-8 -*-  # 导入mysql import MySQLdb # host host = '39.107.102.34'  # 用户名 user = 'root'  # 密码 pwd = '123456'  # 数据库 database = 'qiezi'  # 端口号 port = 3306  # 链接数据库 db = MySQLdb.connect(host,user,pwd,database,port) # 或许游标 course = db.cursor() #删除userId=0的用户名 #delete from 表 where需要删除的条件 sql="delete from qiezzilogin where userId=1" try: #执行删除语句  statue=course.execute(sql) #1代表删除成功,0删除失败  print statue db.commit() except: #如果没有删除成功,执行回滚  db.rollback() #关闭数据库 db.close() update.py  
#-*- coding:utf-8 -*-  # 导入mysql import MySQLdb # host host = '39.107.102.34'  # 用户名 user = 'root'  # 密码 pwd = '123456'  # 数据库 database = 'qiezi'  # 端口号 port = 3306  # 链接数据库 db = MySQLdb.connect(host,user,pwd,database,port) # 或许游标 course = db.cursor() #修改的关键字update 表名set 需要修改的字段='值'where 字段=该谁 # sql="update qiezzilogin set password=654321 where userId=1" #同时更新多个字段 sql="update qiezzilogin set password=123456,token='111111111111'where userId=1" try: #执行修改  statue=course.execute(sql) #修改有返回值,0代码修改失败,1代表修改成功  print statue db.commit() except: #如果修改发生异常,执行回滚  db.rollback() #查询出来看一下 sql_find="select * from qiezzilogin where userId=1" course.execute(sql_find) #返回数据 persons=course.fetchall() for person in persons: print person[0],person[1],person[2],person[3] db.close() 下面我们拓展点基础之外的数据库操作 union这个名词是两表合并默认就是去重,加个all就是不去重,这里我们需要在我们的数据库里创建两个表,表里的数据自己写(你可以去Python菜鸟教程里进行参考,哈哈哈)  union.py 
#-*- coding:utf-8 -*-  # 导入mysql import MySQLdb # host host = '39.107.102.34'  # 用户名 user = 'root'  # 密码 pwd = '123456'  # 数据库 database = '1509E'  # 端口号 port = 3306  # 链接数据库 db = MySQLdb.connect(host,user,pwd,database,port) # 或许游标 course = db.cursor() # #两表合并注意UNION默认去重 All表示不去重 # sql="select id,country,url from Websites UNION select id,country,url from apps" # #执行sql语句 # course.execute(sql) # #获取数据 # places=course.fetchall() # #使用for循环遍历 # for place in places: # print place[0],place[1],place[2] #分组查询 关键字段 group by需要分组查询的字段 后面与having配置需要的条件 #查询 根据城市分组查询,并输出数量大于2的 #复合函数,统计数量的字段COUNT(字段) sql="select country,COUNT(country) from Websites group by country HAVING count(country)>=2" #执行sql course.execute(sql) #获取 citys=course.fetchall() for city in citys: print city[0],city[1] 左连接,右连接,内连接的使用 join.py 
#-*- coding:utf-8 -*-  # 导入mysql import MySQLdb # host host = '39.107.102.34'  # 用户名 user = 'root'  # 密码 pwd = '123456'  # 数据库 database = '1509E'  # 端口号 port = 3306  # 链接数据库 db = MySQLdb.connect(host,user,pwd,database,port) # 或许游标 course = db.cursor() # #1.左连接 取左表所有的记录,取右表和左表相同的记录 # sql="select a.runoob_id,a.runoob_title,a.runoob_author,a.submission_date,b.runoob_count from runoob_tbl a LEFT JOIN " \ # "tcount_tbl b on a.runoob_author = b.runoob_author" #执行sql语句 # #2.右连接: 取右表所有的记录,取左边和右表相同的记录 # sql="select a.runoob_id,a.runoob_title,b.runoob_author,a.submission_date,b.runoob_count from runoob_tbl a RIGHT JOIN " \ # "tcount_tbl b on a.runoob_author = b.runoob_author" #3.内连接 :取两表相同的记录 sql="select a.runoob_id,a.runoob_title,b.runoob_author,a.submission_date,b.runoob_count from runoob_tbl a inner JOIN " \ "tcount_tbl b on a.runoob_author = b.runoob_author"  course.execute(sql) persons=course.fetchall() for person in persons: print person[0],person[1],person[2],person[3],person[4] 

 avg平均值在数据库的调用
avg.py

#-*- coding:utf-8 -*-

# 导入mysql
import MySQLdb

# host
host = '39.107.102.34'

# 用户名
user = 'root'

# 密码
pwd = '123456'

# 数据库
database = '1509E'

# 端口号
port = 3306

# 链接数据库
db = MySQLdb.connect(host,user,pwd,database,port)

# 或许游标
course = db.cursor()
# 1: 求每一个同学的平均成绩 group by name ,并且大于 80
# 2: 求各科的平均成绩
# 3: 求全部的平均成绩

#1.
# sql="select name,AVG(score) from score group by name having AVG(score)>80"
#2.
# sql="select subject,AVG(score) from score group by subject"
#3.
sql="select AVG(score) from score"
course.execute(sql)
students=course.fetchall()
for student in students:
    # print student[0],student[1]
    print student[0]




数据库的建表参考菜鸟教程。。。



 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值