无标题无名氏No.64561936 只看PO
2024-12-01(日)23:16:07 ID:Ps5R9MG 回应
各位大佬们好,请问一下在Python中已经连接MySQL数据库但是无法存入数据是什么原因呢?终端可以打印需要存入的字符串但是用PHPmyadmin打开数据库发现什么都存不进去。。。可以请大佬们看看吗?( ´∀`)稍后附上代码
无标题无名氏No.64562024
2024-12-01(日)23:23:13 ID: Ps5R9MG (PO主)
import pymysql.cursors
import math
connection=pymysql.connect(host='localhost', port=3306, user='root', password="123456", db='兵推电算程序', charset='utf8mb4')
#,cursorclass=pymysql.cursors.DictCursortry:
cursor = connection.cursor()sql = "select * from 团级战斗队"result = cursor.execute(sql)result = cursor.fetchall()
basic_AM_atk = 0.0basic_NAM_atk = 0.0basic_AM_def = 0.0basic_NAM_def = 0.0basic_mov = 20.0plus_times = 0count = 0while count != 1 : #循环选择团级单位
for data in result:temp_data = str(data0[0,1])print(f"步兵作战攻击数值为:" + temp_data)
temp_data = str(data1[0,2])print(f"装甲战防护数值为:" + temp_data)temp_data = str(data1[0,3])print(f"步兵作战防护数值为:" + temp_data)temp_data = str(data0[0,4])print(f"基础机动数值为:" + temp_data + "公里")temp_data = str(data5[0,5])print(f"部队类型编号为:" + temp_data)temp_data = str(data6[0,6])print(f"单位名称为:" +temp_data + "\n")
#ipv = input("是否显示详细信息?[Y/n]")#if ipv == Y :
# print(f"装甲战攻击数值为:" + data0[0,0])
#开始组建大编制部队
temp_choice0 = input("是否选择该单位组建大编制单位?[Y/n]\n")if temp_choice0 == 'Y' :plus_times = plus_times + 1 #团级战斗队数量
basic_AM_atk = basic_AM_atk + data0[0,0]
basic_NAM_atk = basic_NAM_atk + data0[0,1]basic_AM_def = basic_AM_def + data2[0,2]basic_NAM_def = basic_NAM_def + data0[0,3]plus_times = str(plus_times)print(f"当前团级单位数量为:" + plus_times)plus_times = int(plus_times)basic_AM_atk = str(basic_AM_atk)print(f"当前装甲战攻击数值为:" + basic_AM_atk)basic_AM_atk =
float(basic_AM_atk)basic_NAM_atk = str(basic_NAM_atk)print(f"当前步兵作战攻击数值为:" + basic_NAM_atk)basic_NAM_atk = float(basic_NAM_atk)basic_AM_def = str(basic_AM_def)print(f"当前装甲战防护数值为:" + basic_AM_def)basic_AM_def = float(basic_AM_def)basic_NAM_def = str(basic_NAM_def)
print(f"当前步兵作战防护数值为:" + basic_NAM_def)basic_NAM_def = float(basic_NAM_def)print("\n")temp_choice1 = input("是否再次进行团级单位选择?[Y/n]") #判定是否需要再次选取if temp_choice1 == 'n' :count = 1regiment = str(plus_times)print(f"您所组建的部队包含" + regiment + "个团级单位")
#储存模块
basic_AM_atk = float(basic_AM_atk)basic_NAM_atk = float(basic_NAM_atk)basic_AM_def = float(basic_AM_def)basic_NAM_def = float(basic_NAM_def)basic_mov = float(basic_mov)new_unit_name = input("请输入新组建部队的名字:\n")data_variable = [basic_AM_atk,basic_NAM_atk,basic_AM_def,basic_NAM_def,basic_mov,new_unit_name]
#sql_input = "INSERT INTO 已经组建的单位 VALUES (basic_AM_atk,basic_NAM_atk,basic_AM_def,basic_NAM_def,,new_unit_name"sql_input = "INSERT INTO 已经组建的单位 (装甲战攻击,步兵作战攻击,装甲战防护,步兵作战防护,机动基础数值,单位名称) VALUES (%s,%s,%s,%s,%s,%s)"result = cursor.execute(sql_input,data_variable)result = cursor.fetchall()for recept in result :print(recept)
#检测输入模块
query = "SELECT * FROM 已经组建的部队"cursor.execute(query)result = cursor.fetchall()for row in result :print(row)except pymysql.Error as e :print("数据库连接失败")#Exception : print("查询失败")cursor.close()connection.close()
#草稿
无标题无名氏No.64768788
2024-12-22(日)02:17:33 ID: S4oF0u3
先看看是sql语句问题还是MySQL连接问题。你这个是连接问题。 连接问题 有几点常见问题1.连接地址账号密码字符集等配置项问题2.防火墙问题3.驱动和MySQL版本不匹配