1

I am working on MySQL DB using python . I have created a table named "Systems". I want to insert the data to table.

My Table : id Typeid Types

Columns . I get the data from my API in JSON format as input to the table . i.e {"Typeid":"1","types":"a,b,c,d,e"}

I want to store the data into my table in the following way:

id  Typeid  Typed
1    1       a
2    1       b
3    1       c
4    1       d
5    1       e

I want to this to be done in python script. I have script for inserting the data to table. First to check if the data exists into the table later insert the data to table.

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

This works only if the data is single string . But want the data to be split and inserted to Table in Python

subs2 = Typed.split(',')
#this splits the data with comma separated and stores into the variable

I want to insert sub2 data into the table like expected output written above. Thanks in advance.

1 Answer 1

1

Simple way is to add for loop to this function:

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.