Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Database

Tags:

python

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

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

Now I can update this User's information just fine, but how do I

  1. Add more users to this data structure. A dictionary of dictionaries?
  2. Allow users to have more than one credit card, a many to one relationship

Thanks for any responses, I've been trying to wrap my head around this for awhile

like image 703
Dylan Pierce Avatar asked May 30 '12 20:05

Dylan Pierce


People also ask

Which is best database for Python?

SQLite. SQLite is probably the most straightforward database to connect to with a Python application since you don't need to install any external Python SQL modules to do so. By default, your Python installation contains a Python SQL library named sqlite3 that you can use to interact with an SQLite database.

Is Python good for database?

The Python programming language has powerful features for database programming. Python supports various databases like SQLite, MySQL, Oracle, Sybase, PostgreSQL, etc. Python also supports Data Definition Language (DDL), Data Manipulation Language (DML) and Data Query Statements.

Does Python have its own database?

One of them is SQLite. SQLite is an embedded, file-based relational database management system (RDBMS) that can be used in our Python applications without having to install any additional software. Instead, we only need to import the built-in Python library sqlite3 to use this database.

Is SQL used in Python?

SQL, which stands for structured query language, is a programming language in which the user queries relational databases. Data scientists use SQL in Python in a variety of instances, dictated by the use case at hand or by personal preference.


2 Answers

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

like image 166
Mike DeSimone Avatar answered Sep 29 '22 10:09

Mike DeSimone


Most key-value stores I've seen are either a dictionary of dictionaries (like mongo*) or a dictionary of strings (like redis). There are pros and cons to both approaches. I don't know what you want, so I'll give the simplest answer: Go with a list of dictionaries:

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

*OK, to be fair, Mongo is more like a dictionary of magic strings. It's json, but the lookups are handled internally, so you can treat it like a dictionary of arbitrary (but json-serializable) data structures.

like image 40
kojiro Avatar answered Sep 29 '22 11:09

kojiro