0

I have below dictionary values. Example:

dic = {'ID1': ['Name1', 'Name2'],
       'ID2': ['Name1', 'Name3'],
       'ID3': ['Name2']}

I want to print address of every matched names from above dict values into below format.

Example Output:

['ID1', 'adress1' ,'adress2'],
['ID2', 'adress1' ,'adress3'],
['ID3', 'adress2']

Below is my example code of database table that has "Emp_ID", "Emp_Name", "Emp_Address". The dict values are "Emp_Names", that should be match emp_Names and get the Adresses in to the list as Example out.

import sqlite3


conn=sqlite3.connect('emp.db')
conn.execute("""CREATE TABLE IF NOT EXISTS branch
        (Emp_ID TEXT UNIQUE,
    Emp_Name    TEXT UNIQUE,
    Emp_Address TEXT
    )""")

conn.execute("""INSERT OR IGNORE INTO branch(Emp_ID, Emp_Name, Emp_Address) \
    VALUES('Emp1', 'Name1', 'adress1')""")

conn.execute("""INSERT OR IGNORE INTO branch(Emp_ID, Emp_Name, Emp_Address) \
    VALUES('Emp2', 'Name2', 'adress2')""")

conn.execute("""INSERT OR IGNORE INTO branch(Emp_ID, Emp_Name, Emp_Address) \
    VALUES('Emp3', 'Name3', 'adress3')""")

conn.execute("""INSERT OR IGNORE INTO branch(Emp_ID, Emp_Name, Emp_Address) \
    VALUES('Emp4', 'Name4', 'adress4')""")

conn.execute("""INSERT OR IGNORE INTO branch(Emp_ID, Emp_Name, Emp_Address) \
    VALUES('Emp5', 'Name5', 'adress5')""")

conn.commit()
conn.close()

For example 'ID1' (column) should not to be changed. And 'name1' should take its address and 'name2'should take its address and all matched names should take their addresses. Which statements are useful for my scenario. Is it possible? If possible how to to?

0

Have you tried implementing row factory? https://docs.python.org/3/library/sqlite3.html#sqlite3.Connection.row_factory

More details here: https://xbuba.com/questions/3300464/how-can-i-get-dict-from-sqlite-query

2
  • I think your above suggestion is to use get dictionary from SQLite. But I want to get the data from SQLite using my example dictionary values. – Mano Oct 31 '20 at 6:08
  • 1
    And I have edited my Question. – Mano Oct 31 '20 at 6:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.