Click here to Skip to main content
14,366,562 members

How to insert all the nested dictionary values in the SQL table in a single row

Vishal MuraliKrishnan asked:

Open original thread
Writing the Nested Dictionaries in SQL Table. Code is getting written.

Since I'm dealing with nested dictionaries. So Main Dicionary (User_ID value) is getting writted one row and other Nested Dictionary values are getting written in another row..

I want to write all the values in the single row.

Let me know any suggestions to write the values in single row.

Code - Output( Userdefined Values)
('1101', {'Name': 'Test_User', 'Age': Test_Age, 'Occupation': 'Test_Occupation'})

I want to write all these nested dictionary values in the single row in SQL table.

SQL Output (For Example)
User_ID  User_name    User_Age    User_Occupation


         Test_User    Test_Age    Test_Occupation

Thanks for your time in Advance !!

What I have tried:

Part of code use to write the values into SQL Table :

for k,v in user_details.items():
            user_col = k
            cursor.execute('INSERT INTO Details (User_ID) VALUES ("%s")' % (user_col))

            cols = v.keys()
            vals = v.values()

            sql = "INSERT INTO Details ({}) VALUES ({})".format(
            ', '.join(cols),
            ', '.join(['%s'] * len(cols)));

            cursor.execute(sql, list(vals))
Tags: Python, Python3.6


When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the The Code Project Open License (CPOL).

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100