Simulating relational data (or databases) in Python?

I often use Python to prototype ideas for other platforms, for easy sorting. Now I want to play with some ideas related to relational data in a database.

What is the best way to represent a database in Python? A set of tuples? Using a module such as SQLite?

I am looking for simple solutions in Python. If the solution is too "databasey", I also urinate my prototyping in the database itself.

UPDATE: I would not use Python with a database (I don’t even have a specific database), I just want to think with codes about questions such as "If I have X relational data, can I answer Y questions and solve Z problems ? "

+4
source share
2 answers

Either sqllite3 or a Relational Mapper object, such as SQLAlchemy .

sqllite3 very easy to configure and works just like a SQL database (for prototyping purposes, of course, not for production).

SQLAlchemy is a great way to work with a real database engine (which could be sqllite3), as if it were a collection of objects. For example, creating a table looks something like this (from the tutorial ):

 from sqlalchemy import * db = create_engine('sqlite:///tutorial.db') metadata = BoundMetaData(db) users = Table('users', metadata, Column('user_id', Integer, primary_key=True), Column('name', String(40)), Column('age', Integer), Column('password', String), ) users.create() 

Similarly, it has simple functions and classes that allow you to insert elements, create relationships, and everything else you can do in the database.

ETA: I understand that you are not actually using Python with the database. However, working with ORM, such as SQLAlchemy, allows you to treat relational data as objects. It's about as simple as any form of prototyping, with the added benefit of being closer to what you end up doing.

+6
source

The defacto SQLite relational database, which can be used with the simple import sqlite3 . You can see the documentation for future reference.

Alternatively, if you do not want to go this route, you can use the dictionary for your pairs (key, value). This can be done like this:

 d = {} d['key'] = 'value' 
+2
source

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


All Articles