Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Access list of dictionaries
#5
I don't understand why people want to create an sql database from Python. It can be done with Python, but it's a one-off thing. Just create the database cars in phpMyAdmin.

After making the database, and creating the table or tables you want within that database, then you can add columns to a particular table, manipulate, retrieve and display sets of data from the database tables.

In this particular case, I would create a database cars and a table car_details. The table car_details should have a unique key, best as column 1 called id. The next column should contain the names of the manufacturers, call that column make. Then a column model, the name of the vehicle. Then as many other columns as you wish, such as wheels, colour, doors, engine, price, whatever. You can always add more columns to a table, or INSERT a new make of vehicle plus details.

I use pymysql, not sqllite3, but getting data from the database is pretty much the same:

def mysqlRemoteCW(num_wheels, colour): 
    # To connect remote MySQL database 
    conn = pymysql.connect( 
        host='123.456.789.123', 
        user='myuser',  
        password = 'topsecret', 
        db='cars', 
        )   
    # results as tuple of tuples    
    #cur = conn.cursor()
    # results as list of dictionaries format column_name, value
    cur = conn.cursor(pymysql.cursors.DictCursor)
    # Example Select query 
    sql = f"SELECT make, name, wheels, colour FROM car_details WHERE  wheels < {num_wheels} AND colour = %s
    cur.execute(sql, (colour,))
    output = cur.fetchall() 
    # To close the connection 
    conn.close()
    return output
The dictionary output is unnecessary and repetative, because you should know the column names you want to get. I would be happy with a tuple of tuples!

If you set num_wheels = 1 you may not find many cars to suit that criterion!
Reply


Messages In This Thread
Access list of dictionaries - by britesc - Jul-24-2023, 10:52 AM
RE: Access list of dictionaries - by deanhystad - Jul-24-2023, 02:55 PM
RE: Access list of dictionaries - by britesc - Jul-24-2023, 03:00 PM
RE: Access list of dictionaries - by deanhystad - Jul-24-2023, 07:23 PM
RE: Access list of dictionaries - by Pedroski55 - Jul-26-2023, 05:00 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Sort a list of dictionaries by the only dictionary key Calab 2 693 Apr-29-2024, 04:38 PM
Last Post: Calab
  function that returns a list of dictionaries nostradamus64 2 1,832 May-06-2021, 09:58 PM
Last Post: nostradamus64
  convert List with dictionaries to a single dictionary iamaghost 3 2,926 Jan-22-2021, 03:56 PM
Last Post: iamaghost
  Creating a list of dictionaries while iterating pythonnewbie138 6 3,375 Sep-27-2020, 08:23 PM
Last Post: pythonnewbie138
  Help accessing elements of list of dictionaries Milfredo 6 2,953 Sep-07-2020, 01:32 AM
Last Post: Milfredo
  Accessing values in list of dictionaries pythonnewbie138 2 2,195 Aug-02-2020, 05:02 PM
Last Post: pythonnewbie138
  how does .join work with list and dictionaries gr3yali3n 7 3,396 Jul-07-2020, 09:36 PM
Last Post: bowlofred
  access dictionary with keys from another and write values to list redminote4dd 6 3,346 Jun-03-2020, 05:20 PM
Last Post: DeaD_EyE
  creating a list of dictionaries from API calls AndrewEnglsh101 5 3,184 Apr-03-2020, 02:21 PM
Last Post: AndrewEnglsh101
  Access list items in Python kamaleon 2 2,429 Dec-31-2019, 11:10 AM
Last Post: kamaleon

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020