Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
easy_db is a high-level Python library designed to simplify working with databases. The "DataBase" class handles connecting to various types of databases while providing simple methods for common tasks. The underlying database connection and cursor can be used when more precise control is desired.
Currently Supported Databases:
Before easy_db:
import pyodbc
import os
conn = pyodbc.connect(
r'Driver={Microsoft Access Driver (*.mdb, *.accdb)};' +
r'DBQ=' + os.path.abspath('MyDatabase.accdb') + ';')
cursor = conn.cursor()
cursor.execute('SELECT * FROM test_table;')
data = cursor.fetchall()
columns = [col[0] for col in cursor.description]
table_data = [dict(zip(columns, row)) for row in data]
# table_data -> [{'column1': value1, 'column2': value2}, {...}, ...]
Using easy_db:
import easy_db
db = easy_db.DataBase('MyDatabase.accdb')
table_data = db.pull('test_table')
# table_data -> [{'column1': value1, 'column2': value2}, {...}, ...]
Let's first connect to a SQLite database.
import easy_db
db = easy_db.DataBase('test_sqlite3_db.db')
Now let's see what tables are available in this database.
tables = db.table_names()
Table columns and types are simple to investigate.
print(db.columns_and_types('example_table'))
Let's pull all of the data from a table. We could start with something like "SELECT * ...", but this is way more fun:
data = db.pull('example_table')
Note that the table/query data is returned as a list of dictionaries with column names as dictionary keys.
import pandas
df = pandas.DataFrame(data)
Now perhaps we have an Access database and would like to pull in a table from our SQLite database. easy_db makes this simple and gracefully handles the nuances of dealing with the different databases.
db = easy_db.DataBase('test_sqlite3_db.db')
db_2 = easy_db.DataBase('test_access_db.accdb')
db_2.copy_table(db, 'example_table')
The DataBase object can be used as a context manager for running custom SQL. The cursor is provided and the connection runs .commit() and .close() implicitly after the "while" block.
with db as cursor:
cursor.execute('DELETE * FROM example_table;')
db = easy_db.DataBase(...)
db.pull('tablename')
db.pull_where('tablename', 'sql_condition')
db.pull_where_id_in_list('tablename', 'id_column', match_values_list)
db.append('tablename', new_table_rows) # new_table_rows is a list of dicts
db.update('tablename', 'match_column', 'match_value', 'update_column', 'update_value')
db.delete_duplicates('tablename')
db.table_names()
db.query_names() # for Access
db.columns_and_types('tablename')
db.key_columns('tablename')
db.size # property with size of database in GB
db.compact_db # compact & repair Access db or vacuum SQLite db
db.create_table('tablename', columns_and_types)
db.drop_table('tablename')
db.copy_table(other_db_with_tablename, 'tablename')
db.add_column('tablename', 'column')
db.drop_column('tablename', 'column')
db.create_index('tablename', 'column')
with db as cursor:
cursor.execute('SELECT * FROM tablename;') # execute any SQL statement
db.execute('SELECT * FROM tablename;')
MIT
FAQs
Easy Python database interaction
We found that easy-db demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.