私はPythonを使用してMySQLDBに取り組んでいます。「システム」という名前のテーブルを作成しました。データをテーブルに挿入したい。
マイテーブル:idTypeidタイプ
列。テーブルへの入力として、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のテーブルに挿入する必要があります
subs2 = Typed.split(',')
#this splits the data with comma separated and stores into the variable
上記の期待される出力のように、サブ2データをテーブルに挿入したいと思います。前もって感謝します。
簡単な方法は、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
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加