AI应用开发相关目录
本专栏包括AI应用开发相关内容分享,包括不限于AI算法部署实施细节、AI应用后端分析服务相关概念及开发技巧、AI应用后端应用服务相关概念及开发技巧、AI应用前端实现路径及开发技巧
适用于具备一定算法及Python使用基础的人群
- AI应用开发流程概述
- Visual Studio Code及Remote Development插件远程开发
- git开源项目的一些问题及镜像解决办法
- python实现UDP报文通信
- python实现日志生成及定期清理
- Linux终端命令Screen常见用法
- python实现redis数据存储
- python字符串转字典
- python实现文本向量化及文本相似度计算
- python对MySQL数据的常见使用
文章目录
一、MySQL数据库的安装与使用
安装
详见:
https://blog.csdn.net/weixin_39289696/article/details/128850498
概括为:
离线安装包下载(msi文件,几百MB),Service only,检测mv C++
2019插件是否安装并完成安装,一路next,配置port,密码验证方式(Authentication
Method)这一步很重要(第一个是强密码校验,mysql推荐使用最新的数据库和相关客户端,MySQL8换了加密插件,所以如果选第一种方式,很可能导致你的navicat等客户端连不上mysql8),设置密码,需要牢记,因为后面要用这个密码连接数据库,用户名为root,最终更改mysql名称(mysql、mysql80等),服务器文件权限(Server
File Permissions)
使用
使用Navicat可轻松实现对数据的链接、库操作(增删改)、表操作(增删改查)。
但AI应用开发往往需要基于以下三点需求需要对MySQL进行代码操作:
1.表层面的数据自动化处理
2.表层面的数据批量处理
3.python实现的算法与其他技术栈(C++QT、Java Web等)实现的系统数据交互
二、代码示例
存储数据
cpp
def insert_mysql(word):
mysql_path = os.path.join(os.getcwd(), "data", "mysql_df1500hdb_config.json")
with open(mysql_path, 'r', encoding='utf-8') as f:
mid_json = json.load(f)
db = pymysql.connect(host=mid_json['host'],
port=mid_json['port'],
user=mid_json['user'],
password=mid_json['password'],
database=mid_json['database'])
cursor = db.cursor()
insert_code = "INSERT INTO df_his_weatherpv (name,value1,value2,value3,value4,value5,value6,evt_time) VALUES (%s,%s,%s,%s,%s,%s,%s,%s);"
cursor.execute(insert_code, (
word.get('name'), word.get('value1'), word.get('value2'), word.get('value3'), word.get('value4'),
word.get('value5'), word.get('value6'), str(timestamp2time(word.get('time')))))
# 保留操作
db.commit()
# 关闭连接
db.close()
拿取数据
cpp
# 为温度、湿度、气压三个无需vdm分解的数据
def get_feature(now_time_str, des_code):
table_ls = get_dbtable(now_time_str)
if len(table_ls) == 1:
mysql_path = os.path.join(os.getcwd(), "data", "mysql_df1500hdb_config.json")
with open(mysql_path, 'r', encoding='utf-8') as f:
mid_json = json.load(f)
db = pymysql.connect(host=mid_json['host'],
port=mid_json['port'],
user=mid_json['user'],
password=mid_json['password'],
database=mid_json['database'])
cursor = db.cursor()
sql_code = "SELECT * FROM {table} WHERE name = '{des}' AND evt_time >= DATE_SUB('{time}', INTERVAL 2 DAY) AND evt_time < '{time}';".format(
time=now_time_str,
table=table_ls[0],
des=des_code)
cursor.execute(sql_code)
data = pd.DataFrame(cursor.fetchall())
# 关闭连接
db.close()
# print(data)
data.columns = ['id', 'name', 'value', 'ect_time', 'status']
data = data.sort_values('ect_time')
times = data['ect_time'].tolist()
values = data['value'].tolist()
mdh_indexs_dict = generate_26_mdhs(extract_mdh(now_time_str))
mdhs = [extract_mdh(i) for i in times]
for i in range(len(mdhs)):
try:
mdh_indexs_dict[mdhs[i]].append(i)
except:
pass
flag = 0
out_ls = []
for mdh_dict_v in mdh_indexs_dict.values():
if mdh_dict_v != []:
flag += 1
out_ls.append(np.mean(values[min(mdh_dict_v):max(mdh_dict_v) + 1]))
else:
out_ls.append(None)
if flag == 24:
return out_ls
elif flag == 23 or flag == 22:
return deal_none(out_ls)
else:
return []
else:
mysql_path = os.path.join(os.getcwd(), "data", "mysql_df1500hdb_config.json")
with open(mysql_path, 'r', encoding='utf-8') as f:
mid_json = json.load(f)
db = pymysql.connect(host=mid_json['host'],
port=mid_json['port'],
user=mid_json['user'],
password=mid_json['password'],
database=mid_json['database'])
cursor = db.cursor()
sql_code = "SELECT * FROM {table} WHERE name = '{des}' AND evt_time >= DATE_SUB('{time}', INTERVAL 2 DAY) AND evt_time < '{time}';".format(
time=now_time_str,
table=table_ls[0],
des=des_code)
cursor.execute(sql_code)
data1 = pd.DataFrame(cursor.fetchall())
sql_code = "SELECT * FROM {table} WHERE name = '{des}' AND evt_time >= DATE_SUB('{time}', INTERVAL 2 DAY) AND evt_time < '{time}';".format(
time=now_time_str,
table=table_ls[1],
des=des_code)
cursor.execute(sql_code)
data2 = pd.DataFrame(cursor.fetchall())
db.close()
data = pd.concat([data1, data2], ignore_index=True)
data.columns = ['id', 'name', 'value', 'ect_time', 'status']
data = data.sort_values('ect_time')
times = data['ect_time'].tolist()
values = data['value'].tolist()
mdh_indexs_dict = generate_26_mdhs(extract_mdh(now_time_str))
mdhs = [extract_mdh(i) for i in times]
for i in range(len(mdhs)):
try:
mdh_indexs_dict[mdhs[i]].append(i)
except:
pass
flag = 0
out_ls = []
for mdh_dict_v in mdh_indexs_dict.values():
if mdh_dict_v != []:
flag += 1
out_ls.append(np.mean(values[min(mdh_dict_v):max(mdh_dict_v) + 1]))
else:
out_ls.append(None)
if flag == 24:
return out_ls
elif flag == 23 or flag == 22:
return deal_none(out_ls)
else:
return []
删除、修改等操作与上述类似,即用即查即可
三、总结
完结,撒花!