Python数据库操作Orator-orm

It is inspired by the database part of the Laravel framework, but largely modified to be more pythonic.


参考 PHP排名第一的web框架 Laravel framework

https://orator-orm.com/

pip install orator 

3种查询方式 代码实现

# -*- coding: utf-8 -*-

from orator import DatabaseManager
import logging

# 开启查询日志,打印sql
logging.basicConfig(level=logging.DEBUG)


config = {
    'mysql': {
        'driver': 'mysql',
        'host': 'localhost',
        'database': 'data',
        'user': 'root',
        'password': '123456',
        'prefix': '',
        'log_queries': True
    }
}

db = DatabaseManager(config)

# query
results = db.select('select * from student where id = %s', [11])
print(results)

# query builder
user = db.table('student').first()
print(user)

# orm
from orator import Model

Model.set_connection_resolver(db)


class Student(Model):
    __table__ = 'student'


user = Student.find(11)
print(user.name)



上一篇:程序员面试必备之排序算法汇总(下)


下一篇:与文件操作相关的函数