Junior — Middle
What operations and tasks did you perform with the database in your work?
sobes.tech AI
Answer from AI
In working with databases in Python, the following operations are usually performed:
- Connecting to the database: Using libraries such as
sqlite3,psycopg2for PostgreSQL, or ORMs like SQLAlchemy. - Creating and modifying schemas: Creating tables, indexes, changing the database structure.
- CRUD operations: Creating (INSERT), reading (SELECT), updating (UPDATE), and deleting (DELETE) data.
- Queries with filtering and sorting: Retrieving data with conditions, sorting, aggregation.
- Transactions: Ensuring data integrity through commit and rollback.
- Query optimization: Using indexes, optimizing queries for better performance.
Example of a simple query using sqlite3:
import sqlite3
conn = sqlite3.connect('example.db')
cursor = conn.cursor()
cursor.execute('CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, name TEXT)')
cursor.execute('INSERT INTO users (name) VALUES (?)', ('Alice',))
conn.commit()
cursor.execute('SELECT * FROM users')
print(cursor.fetchall())
conn.close()
Depending on the project, tasks may include migrations, working with ORM, setting up replication, and backups.