Skip to main content

MySQL chained operation of Python development

Project description

前言

Python开发的mysql链式操作,ABuilder告别繁琐的模型定义,节省开发时间,几乎没有任何要求,导入直接使用。与常规模型不同ABuilder不需要预先定义表字段、字段类型、字段长度等繁琐的设置,当然那样做有它的优点这里就不说了~,各具所长取舍看个人。ABuilder支持入直接使用,简单、快速、便捷

快速开始

  • 安装 a-sqlbuilder
pip3 install a-sqlbuilder
  • 设置数据配置文件(参照数据库配置文件说明)
  • 开始使用
from ABuilder.ABuilder import ABuilder

model = ABuilder()
data = model.table('tar_user').field("username,id").where({"username": ["like", "%M-萌%"]}).limit(0, 1).query()

使用文档

第三方库要求

  • pymysql
  • logging

数据库配置文件

我们需要开发者在项目更目录中加入database.py 数据库配置文件

确保from database import database能获取到数据库配置项

配置文件实例:

class Config(object):
    pass

class Proconfig(Config):
    pass


class Devconfig(Config):
    debug = True
    DATABASE_URI = 'mysql+pymysql://root:root@127.0.0.1:3306/target'
    data_host = '127.0.0.1' 
    data_pass = 'root'
    data_user = 'root'
    database = 'target'
    data_port = 3306
    charset = 'utf8mb4'


database = Devconfig

支持函数

目前项目只支持一些简单用法具体如下

  • table 查询表
  • where where条件
  • where_or 设置or条件
  • field 查询字段
  • limit 查询条数
  • group 分组
  • order 排序
  • join 连表查询
  • first 查询单条
  • query 查询多条
  • pluck 查询单个字段
  • insert 插入
  • update 修改
  • delete 删除
  • select 执行原生查询
  • commit 事物提交
  • rollback 事物回滚
  • get_last_sql 获取执行sql
  • get_insert_id 获取插入id

table

设置查询表

from ABuilder.ABuilder import ABuilder
data = ABuilder().table('user').limit(0,1).query()
print(data)

where

设置where条件

from Amo.ABuilder import ABuilder
ABuilder().table('user').where({"id":["=",3]}).first()

多个where条件

ABuilder().table('user').where({"id":['<=',10],"sex":["=","男"]}).query()
# 或则多个where拼接
ABuilder().table('user').where({"id":['<=',10]}).where({"sex":["=","男"]}).query()

比较符号支持 =,<>,<,>,<=,>=,in,like

where_or

设置or条件 where_or使用方法与where大同小异

from Amo.ABuilder import ABuilder
ABuilder().table('user').where_or({"id":["=",3]}).query()

比较符号支持 =,<>,<,>,<=,>=,in,like

field

设置查询字段

ABuilder().table("user").field("user_id,sex,user_name").query()
# 举例count使用
ABuilder().table("user").field("count(*) as count").query()

不设置field默认查询全部字段 *,查询字段使用,隔开,支持count(),sum()

注意:不支持类似DATE_FORMAT(NOW(),'%m-%d-%Y')这类带有%的函数

limit

查询数

ABuilder().table("user").limit(0,10).query()

limit 第一个参数为开始查询位置,第二个参数为获取多少条

group

分组

# 按照性别分组
ABuilder().table("user").field("count(*) as count").group('sex').query()

# 多个分组使用
ABuilder().table("user").field("count(*) as count").group('sex').group('age').query()
# sql为:select count(*) as count from user group by sex,age

order

排序

ABuilder().table("user").order("user_id","desc").query()

# 多个排序值
ABuilder().table("user").order("user_id","desc").order("sex",'asc').query()
# sql为:select * from tar_user order by user_id desc,sex asc

order by先后顺序:遵循从左到右

join

连表查

ABuilder().table('user as u').field('u.id,b.name').join('book b', 'u.id=b.user_id','INNER').where({"u.id": ['=', 1]}).query()

# sql为:select u.id,b.name from user as u INNER JOIN book b on u.id=b.user_id where u.id = 1

join三个参数说明

  • 表名
  • 列表条件 支持 and,or
  • join类型:INNER,LEFT,RIGHT,FULL 默认INNER

支持多个连表,拼接多个join即可。例如:table('table as t').join('table1 t1','t1.user_id=t.id').join('table2 t2','t1.id=t2.book_id')

first

查询单条记录

find = ABuilder().table('user').where({"id":["=",3]}).first()
print(find)

query

查询多条记录

data = ABuilder().table('user').where({"id":["in",(1,2,3,4)]}).query()
print(data)

pluck

查询单个字段

user_id = ABuilder().table('user').where({"username":["=",'张三']}).pluck('id')
print(user_id)

insert

插入数据

model = ABuilder()
state = model.table("user").insert({"username":"张三","sex":'男',"age":18})
if state:
    print("添加成功!自增id:%" % model.get_insert_id)
else:
    print("添加失败")

update

修改记录

state = ABuilder().table("user").where({"username":["=","张三"]}).update({"age":25})
if state:
    print('修改成功')
else:
    print('修改失败')

delete

删除记录

state = ABuilder().table("user").where({"username":["=","张三"]}).delete()
if state:
    print('删除成功')
else:
    print('删除失败')

select

执行原生sql

model = ABuilder()
# 第一种方式
model.select("SELECT username,id FROM user where id=%s", [1])
# 第二种方式
model.select("SELECT username,id FROM user where id=1")

commit,rollback

事物操作

model = ABuilder()
state = model.table("user").insert({"username":"张三","sex":'男',"age":18})
if state:
    state = model.table("book").insert({"book_name":'书本昵称',"user_id":model.get_insert_id})
if state:
    # 成功则提交事物
    model.commit()
else:
    # 失败则回滚事物
    model.rollback()

操作事物注意事项:请勿实例多个ABuilder否则一部分事物在回滚操作时回滚失败

def fun1():
    model = ABuilder()
    state = model.table("user").insert({"username":"张三","sex":'男',"age":18})
    if state:
        state = fun2()

    if state:
        model.commit()
    else:
        model.rollback()

def fun2():
    model = ABuilder()
    return  model.table("book").insert({"book_name":'书本昵称',"user_id":model.get_insert_id})

fun1()

这样如果fun2()返回失败的是失败状态回滚的只是fun1()执行的sql却无法回滚fun2()执行的sql,正确做法如下:

def fun1():
    model = ABuilder()
    state = model.table("user").insert({"username":"张三","sex":'男',"age":18})
    if state:
        state = fun2(model)

    if state:
        model.commit()
    else:
        model.rollback()

def fun2(model):
    return  model.table("book").insert({"book_name":'书本昵称',"user_id":model.get_insert_id})

fun1()

get_last_sql

获取最后一条执行sql

 model = ABuilder()
 find = model.table('user').where({"id":["=",3]}).first()
 print(model.get_last_sql)

获取sql注意事项:输出的sql对于字符串没有加上引号,导致拷贝到数据库管理工具里无法执行,处理办法:给予字符串加上单引号或双引号即可,后面维护会优化(抱歉)

get_insert_id

获取插入自增id

model = ABuilder()
model.table("user").insert({"username":"张三","sex":'男',"age":18})
print(model.get_insert_id)

class继承方式使用示例

from ABuilder.ABuilder import ABuilder
class UserModel(ABuilder):

    def __init__(self):
        self.table_name = 'user'

    def user_info(self,user_id):
        info = self.table(self.table_name).field("user_id,user_name").where({"user_id":user_id}).first()
        print(self.get_last_sql)
        return info

    def login(self):
        pass

userInfo = UserModel().user_info(user_id=1)
print(userInfo)

案例项目

目标记账+

'目标记账+'

记账+。记账与目标结合,随时随地记录每一笔交易,人情来往,多人记账,每日记账实时统计与目标距离,即时了解资金概况资金流向

感谢

项目初期是非常第一个简单版本,如有问题,写法不规范的欢迎反馈,千言万语,你懂的,我就不说了

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

a-sqlbuilder-0.0.2.tar.gz (7.9 kB view details)

Uploaded Source

Built Distribution

a_sqlbuilder-0.0.2-py3-none-any.whl (9.6 kB view details)

Uploaded Python 3

File details

Details for the file a-sqlbuilder-0.0.2.tar.gz.

File metadata

  • Download URL: a-sqlbuilder-0.0.2.tar.gz
  • Upload date:
  • Size: 7.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.0

File hashes

Hashes for a-sqlbuilder-0.0.2.tar.gz
Algorithm Hash digest
SHA256 3fa78b1ef0acace2ea4219754244e416ee24ad26c40f80e4e90aa9eec386fdbe
MD5 544ede5fb854e46f3a5e25a69c93f99f
BLAKE2b-256 5c948c4662f517554d2f522ad3aa786fe8cbfcd1d81c315b05287ff3f48b3f9a

See more details on using hashes here.

File details

Details for the file a_sqlbuilder-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: a_sqlbuilder-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 9.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.0

File hashes

Hashes for a_sqlbuilder-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 1695bf43f6b2e280eb78e0564412077b226d0369746a17ec25761a6e43bed764
MD5 f095b35a7454b3c7ce6fc753e31fb700
BLAKE2b-256 b64a65d0f9e998c5d25971c41fb95ea078a7d09d41d39048ae91a40a27fbbfd1

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page