A simple ORM provides elegant API for Python-MySQL operation
from data_handler import Database
db_config = {
'host': 'localhost',
'port': 3306,
'user': 'root',
'password': '123456',
'database': 'test'
}
Database.connect(**db_config)
from data_handler import Model, Field
class TestModel(Model):
db_table = 'test'
a = Field()
b = Field()
test = TestModel()
test.a = 5
test.b = 'john'
test.save()
for r in TestModel.where(a=5, b='john').select():
print r.a
print r.b
print TestModel.where(a=5, b='john').count()
TestModel.where(a=5, b='john').update(a=1)
from data_handler import execute_raw_sql
results = execute_raw_sql('select b, count(*) from test where b = %s group by b;', (1,))
for val, cnt in results:
print val, cnt