import pymysql
数据库工具类 db_util.py
class db_util:
# 构造函数
def __init__(self, host='12.31.120.104', user='root', pwd='123456',
db_name='bugly'):
self.host = host
self.user = user
self.pwd = pwd
self.db_name = db_name
self.db = None
self.cur = None
def connect_db(self):
"""
连接数据库
"""
try:
self.db = pymysql.connect(self.host, self.user, self.pwd, self.db_name,
charset='utf8')
except pymysql.Error as e:
print("pymysql Error:%s" % e)
return False
self.cur = self.db.cursor()
return True
def close(self):
"""
关闭数据库
"""
if self.db and self.cur:
self.cur.close()
self.db.close()
return True
def execute(self, sql, params=None):
"""
执行sql语句
"""
self.connect_db()
try:
if self.db and self.cur:
# 正常逻辑,执行sql,提交操作
self.cur.execute(sql, params)
self.db.commit()
except:
print('execute failed, sql = ', sql)
self.db.rollback()
# self.close()
return False
return True
def fetch_all(self, sql, params=None):
"""
查询数据
"""
self.execute(sql, params)
return self.cur.fetchall()
mysql_test.py 在gradle中使用gradle命令传参数调用
import time
import sys
import db_util
def insert_record(self, app_id, repo_name, app_version):
"""
插入数据
:param app_id:
:param repo_name:
:param app_version:
"""
try:
self.execute("SELECT * FROM %s WHERE app_id = %d AND repo_name = '%s' AND app_version = '%s'" %('bugly_app_repo', app_id, repo_name, app_version))
if self.cur.rowcount == 0:
time_array = time.localtime(int(time.time()))
style_time = time.strftime("%Y-%m-%d %H:%M:%S", time_array)
success = self.execute("INSERT INTO %s (app_id, app_version, build_version, ctime, mtime, repo_name) values('%s', '%s', '%s', '%s', '%s', '%s')" %
('bugly_app_repo', app_id, app_version, app_version,style_time, style_time, repo_name))
print('insert_record success! , ', success)
else:
print('record exists!')
except Exception as e:
print('insert_record error!, ' , e)
finally:
self.close()
if __name__ == '__main__':
print('__name__')
# app_name, repo_name, app_version
if len(sys.argv) == 4:
app_name = sys.argv[1]
repo_name = sys.argv[2]
app_version = sys.argv[3]
print(app_name, repo_name, app_version)
db_util = db_util()
sql = 'SELECT * FROM bugly_app_repo'
db_util.execute(sql)
print('size = ', db_util.cur.rowcount)
# 项目主表是存在的
sql = "SELECT id FROM bugly_app_info WHERE NAME ='%s'" % (app_name)
results = db_util.fetch_all(sql)
if results is not None and len(results) > 0:
for r in results:
app_id = r[0];
if app_id > 0:
insert_record(db_util, app_id, repo_name, app_version)
else:
print('not exist')
else:
print('please pass parameters(app_name, repo_name, app_version)')
将excel表格数据导入到数据库 excel_read.py
import xlrd
from db_util import db_util
"""
操作 bugly_symbol_info 表
"""
def handle_bugly_symbol_info(db_util, sheet):
# db_util = db_util() // 报错
for i in range(sheet.nrows):
if i != 0:
symbol_name = sheet.row(i)[2].value
repo_name = sheet.row(i)[1].value
result = select_symbol_info_record(db_util, symbol_name, repo_name)
if result is False:
insert_symbol_info_record(db_util, symbol_name, repo_name)
db_util.close()
# SELECT count(*) FROM bugly_symbol_info WHERE symbol_name like '%com.%';
# DELETE FROM bugly_symbol_info WHERE symbol_name like '%com.%';
def select_symbol_info_record(self, symbol_name, repo_name):
sql = "SELECT * FROM bugly_symbol_info WHERE symbol_name ='%s' AND repo_name ='%s'" % (symbol_name, repo_name)
results = self.fetch_all(sql)
if results is not None and len(results) > 0:
return True
else:
return False
def insert_symbol_info_record(self, symbol_name, repo_name):
sql = "INSERT INTO bugly_symbol_info(symbol_name, repo_name) VALUES('%s', '%s')" % (symbol_name, repo_name)
result = self.execute(sql)
if result is True:
print('insert_symbol_info_record success')
else:
print('insert_symbol_info_record failed')
"""
操作 bugly_repo_info 表
"""
def handle_bugly_repo_info(db_util, sheet):
start_index = 176 # 主键非自增长
for i in range(sheet.nrows):
if i != 0:
gerrit_url = sheet.row(i)[3].value
repo_name = sheet.row(i)[1].value
result = select_repo_info_record(db_util, start_index)
if result is False:
insert_repo_info_record(db_util, start_index, repo_name, gerrit_url)
start_index = start_index + 1
db_util.close()
# SELECT count(*) FROM bugly_repo_info WHERE url like '%mobile_android%';
# DELETE FROM bugly_repo_info WHERE url like '%mobile_android%';
def select_repo_info_record(self, start_index):
sql = "SELECT * FROM bugly_repo_info WHERE id = %d" % (start_index)
results = self.fetch_all(sql)
if results is not None and len(results) > 0:
return True
else:
return False
def insert_repo_info_record(self, repo_id, repo_name, gerrit_url):
sql = "INSERT INTO bugly_repo_info(id, platform, name, url, type) VALUES(%d, 1, '%s', '%s', 1)" % (repo_id, repo_name, gerrit_url)
result = self.execute(sql)
if result is True:
print('insert_repo_info_record success')
else:
print('insert_repo_info_record failed')
"""
从 bugly_repo_info 导入数据到 bugly_app_repo 表
"""
# DELETE FROM bugly_app_repo WHERE gerrit_url like '%mobile_android%';
def handle_bugly_app_repo(db_util):
# 将(app_info_id 为 2)的 repo_info 插入到 app_repo表中
sql = "SELECT id, url FROM bugly_repo_info WHERE url like '%%%s%%'" %'mobile_android'
results = db_util.fetch_all(sql)
if results is not None and len(results) > 0:
for result in results:
sql = "INSERT INTO bugly_app_repo (app_id, app_version, build_version, repo_id, gerrit_url) VALUES(%d, '%s', '%s', %d, '%s')" \
%(2, '1.7.0', '1.7.0', result[0], result[1])
db_util.execute(sql)
print('insert bugly_app_repo success')
"""
操作 bugly_biz_repo 数据
"""
# DELETE FROM bugly_biz_repo WHERE gerrit_url like '%mobile_android%'
def handle_bugly_biz_repo(db_util, sheet):
b_name_ids = {}
for i in range(sheet.nrows):
if i != 0:
business_name = sheet.row(i)[0].value
lib_name = sheet.row(i)[1].value
sql = "SELECT id FROM bugly_business_info WHERE business = '%s' AND platform = 1" % (business_name)
ids = db_util.fetch_all(sql)
if ids is not None and len(ids) == 1:
for bid in ids:
b_name_ids[lib_name] = bid[0]
# print(b_name_ids)
# {'im': 14, 'lib_utils': 2, 'lib_log': 2, 'httpservice': 2, 'lib_xlog': 14, 'lib_svcmanager': 2,
# 'immodel': 14, 'mars': 14, 'lib_ui': 2, 'lib_imageloader': 2, 'lib_download': 2, 'basewebview': 2,
# 'analytics-sdk': 2, 'pay': 2, 'analytics-visual-mapping': 2, 'lib_abtest': 2, 'lib_sp': 2, 'push': 14,
# 'lib_router': 2, 'lib_performance_debug': 2, 'common_lib': 2, 'plugin_framework': 2, 'LJVRRecord': 22,
# 'MP3Recorder': 14, 'lib_vr': 22, 'VRLoadingViewLib': 22, 'lib_rtc': 22, 'beike': 4, 'beike_customer': 4,
# 'beike_mid': 4, 'beike_im_plugin': 4}
# 将(app_info_id 为 2)的 repo_info 插入到 biz_repo 表中
sql = "SELECT id, url, name FROM bugly_repo_info WHERE url like '%%%s%%'" % 'mobile_android'
results = db_util.fetch_all(sql)
if results is not None and len(results) > 0:
for result in results:
sql = "INSERT INTO bugly_biz_repo (biz_id, repo_id, gerrit_url) VALUES(%d, %d, '%s')" \
% (b_name_ids.get(result[2]), result[0], result[1])
db_util.execute(sql)
print('insert handle_bugly_biz_repo success')
# 更新build_version
# UPDATE bugly_app_repo
# SET build_version = 1070
# WHERE
# app_id = 2
# AND app_version = '1.7.0'
if __name__ == '__main__':
workbook = xlrd.open_workbook(u'biz_aar_gerrit.xlsx')
sheet_names= workbook.sheet_names()
# 0 汇总 1 ke
sheet = workbook.sheet_by_name(sheet_names[1]);
"""
操作 bugly_symbol_info 表
"""
# db_util = db_util()
# handle_bugly_symbol_info(db_util, sheet)
"""
操作 bugly_repo_info 表
"""
# db_util = db_util()
# handle_bugly_repo_info(db_util, sheet)
"""
从 bugly_repo_info 导入数据到 bugly_app_repo 表
"""
# db_util = db_util()
# handle_bugly_app_repo(db_util)
"""
操作 bugly_biz_repo 数据
"""
# db_util = db_util()
# handle_bugly_biz_repo(db_util, sheet)