Python database

My question is how to create a simple database in python. My example:

User = { 'Name' : {'Firstname', 'Lastname'}, 'Address' : {'Street','Zip','State'}, 'CreditCard' : {'CCtype','CCnumber'}, } 

Now I can update this user information just fine, but how do I

  • Add users to this data structure. Dictionary dictionary?
  • Allow users to have more than one credit card, from several to one.

Thanks for any answers, I tried to wrap my head around this a bit

+6
source share
5 answers

You may be interested in SQLAlchemy . It makes an actual database, such as SQLite or MySQL, more like Python classes.

+10
source

Most of the key-value stores I've seen are either a dictionary of dictionaries (like mango *) or a dictionary of strings (like redis). There are pros and cons for both approaches. I do not know what you want, so I will give the simplest answer: go with the list of dictionaries:

 Users = [] Users.append({ 'Name' : {'Firstname', 'Lastname'}, # Are these sets? 'Address' : {'Street','Zip','State'}, 'CreditCard' : {'CCtype','CCnumber'}, }) 

* OK, to be honest, Mongo is more like a dictionary of magic strings. This is json, but the search is handled internally, so you can think of it as a dictionary of arbitrary (but json-serializable) data structures.

+5
source

You can create a simple database in Python with sqlite , here is a basic tutorial .

+4
source

1. Add users to this data structure. Dictionary dictionary?

List of dictionaries.

 [{'name': ...}, {'name': ...}] 

2. Allow users to have more than one credit card, a many-to-one relationship

List.

 {'name': ..., 'creditcard': [['type', 'number'], ['type', 'number']]} 
+4
source

Create a class User to store information. A class can have the creditcards attribute, which is a list instances of the CreditCard class that are added to instances of the class either when they are created, or later using a class method that you can define. Other methods may update this (and other) attributes as necessary when called. It was called Object Oriented Programming (OOP).

Instances can be stored in memory in instances of various built-in container classes, such as list and dict s. They can be saved to disk and retrieved later using pickle , shelve , json and sqlite3 , to name a few.

You are not trying to do what has not been done before - this is good news, because it means that you have a lot of ready-made software at your disposal, but you will need to read about them in the documentation and maybe ask a few more questions before learn to use them, then you can start to answer questions .; -)

Welcome to Python!

+4
source

Source: https://habr.com/ru/post/917052/