我正在使用 python 开发 MySQL DB。我创建了一个名为“系统”的表。 我想将数据插入表中。
我的 table : id Typeid 类型
列。 我从 API 获取 JSON 格式的数据作为表的输入。 即 {"Typeid":"1","types":"a,b,c,d,e"}
我想通过以下方式将数据存储到我的表中:
id Typeid Typed
1 1 a
2 1 b
3 1 c
4 1 d
5 1 e
我希望这可以在 python 脚本中完成。 我有用于将数据插入表的脚本。首先检查表中是否存在数据,然后将数据插入表中。
def POSTSubsystem():
try:
#Fetching the json data sent from the client application and storing it into a variable.
std_json = request.json
#storing Model_Name json value into a variable.
Type_name = std_json['Type_name']
Typed= std_json['Typed']
#Sql Query to check for where the data exists or not
check_query = "SELECT Typeid,(SELECT COUNT(*) FROM Types_data_table WHERE Typed="+'"'+str(Typed)+'"'+") as `Sub_COUNT` FROM Types WHERE Type_name="+'"'+str(Type_name)+'"'+";"
#Fetching the data and storing it into data frame from the function
results = sql_connection(check_query,"retrive")
print(results)
#checking if the Data frame contains any value
if len(results) != 0:
model_id = results.iloc[0]['Typeid']
#checking the results contain any vlaue to it. Here >1 implies number of row/Count of row that query retrives
if results.iloc[0]['Sub_COUNT'] >= 1:
#send message if the model already exists in the data base
resp = jsonify('Subsystem already exists!')
return resp
else:
#query to insert the new data received json value from client into database
query = "INSERT INTO Types_data_table (Typeid,Typed) VALUES("+ str(Typeid)+',"'+str(Typed)+'"'+");"
#fetching the response from the server
resu = sql_connection(query,"inst")
#sending the response to client
resp = jsonify('Subsystem added successfully!')
return resp
else:
x = '{"Message":"No Data Available For this model"}'
return (x)
except mysql.connector.ProgrammingError as err:
return err.msg
仅当数据是单个字符串时才有效。 但希望在Python中将数据拆分并插入到Table中
subs2 = Typed.split(',')
#this splits the data with comma separated and stores into the variable
我想将 sub2 数据插入到表中,就像上面写的预期输出一样。 提前致谢。
最佳答案
简单的方法是向此函数添加 for
循环:
def POSTSubsystem():
try:
#Fetching the json data sent from the client application and storing it into a variable.
std_json = request.json
#storing Model_Name json value into a variable.
Type_name = std_json['Type_name']
Typed= std_json['Typed']
subs2 = Typed.split(',')
for ids in subs2 :
#Sql Query to check for where the data exists or not
check_query = "SELECT Typeid,(SELECT COUNT(*) FROM Types_data_table WHERE Typed="+'"'+str(ids)+'"'+") as `Sub_COUNT` FROM Types WHERE Type_name="+'"'+str(Type_name)+'"'+";"
#Fetching the data and storing it into data frame from the function
results = sql_connection(check_query,"retrive")
print(results)
#checking if the Data frame contains any value
if len(results) != 0:
model_id = results.iloc[0]['Typeid']
#checking the results contain any vlaue to it. Here >1 implies number of row/Count of row that query retrives
if results.iloc[0]['Sub_COUNT'] >= 1:
#send message if the model already exists in the data base
resp = jsonify('Subsystem already exists!')
#return resp
else:
#query to insert the new data received json value from client into database
query = "INSERT INTO Types_data_table (Typeid,Typed) VALUES("+ str(Typeid)+',"'+str(ids)+'"'+");"
#fetching the response from the server
resu = sql_connection(query,"inst")
#sending the response to client
resp = jsonify('Subsystem added successfully!')
#return resp
else:
resp = '{"Message":"No Data Available For this model"}'
#return (x)
return resp
except mysql.connector.ProgrammingError as err:
return err.msg
关于Python With MySQL 用于拆分 json 数据并将多行插入到表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59463991/