1 Star 0 Fork 15

taikun928 / python-dbhelper

forked from fangzheng / python-dbhelper 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
BSD-3-Clause

python-dbhelper

介绍

Python数据库帮助类(支持Hive、MySQL、Oracle、SqlServer)

软件架构

Python版本:Python3.7

安装教程

  1. git clone https://gitee.com/fangzheng0518/python-dbhelper.git
  2. cd python-dbhelper
  3. pip install -r requirements.txt
  4. python3 setup.py install

注意:

如果第3步报错: ImportError: No module named Cython.Distutils

则需要先安装Cython 安装命令pip install Cython 然后再继续执行第3步pip install -r requirements.txt

API使用说明

1. helper.select(sql,param=None,size=None)

方法参数:

  1. sql : 查询的SQL语句,参数使用%s占位
  2. param: SQL的参数
  3. size: 期望返回的数据条数, 为空则返回所有数据, 例如:返回前10条(size=10) 返回结果: 返回SQL查询的结果
2. helper.execute(sql,param=None)

方法参数:

  1. sql : 查询的SQL语句,参数使用%s占位
  2. param: SQL的参数

返回结果: 返回DML影响的行数

sql = 'DELETE TABLE user where id=1'
users = helper.execute(sql=sql)

示例


# 1、MySQL
from dbhelper.mysql.mysqlhelper import MySQLHelper
dbconfg = {
    'host':'127.0.0.1',
    'port':3306,
    'username':'root',
    'password':'****',
    'db':'test',
    'charset':'utf8'
}
helper = MySQLHelper(dbconfg)

# 判断表是否存在
print(helper.table_is_exist('tablename'))

# 不带参数查询
sql = "select * from user"
users = helper.select(sql=sql)
print(users)

# 不带参数查询前10条
sql = "select * from user"
users = helper.select(sql=sql,size=10)
print(users)

# 带参数查询
sql = "select * from user where id=%s"
users = helper.select(sql=sql,param=(1,))
print(users)

# DML语句:INSERT UPDATE DELETE
sql = "delete from user where id=10"
# sql = "INSERT INTO user (username , password , email , sal) VALUES ('test','分钟都干啥的','test@163.com',5000);"
users = helper.execute(sql=sql)
print(users)



# 2、Hive
from dbhelper.hive.hivehelper import HiveHelper
dbconfg = {
    'host': '127.0.0.1',
    'port': 10000,
    'db':'test',
    'username': 'test'
}
helper = HiveHelper(dbconfg)
sql = "select * from user" # Hive sql结尾不能有;分号
users = helper.select(sql=sql)
print(users)


# 3、SqlServer
from dbhelper.sqlserver.sqlserverhelper import SqlServerHelper
dbconfg = {
    'host': '127.0.0.1',
    'port': 2121,
    'db':'test',
    'username': 'test',
    'password': 'test'
}
helper = SqlServerHelper(dbconfg)
sql = "select * from user"
users = helper.select(sql=sql)
print(users)


# 4、Oracle
from dbhelper.oracle.oraclehelper import OracleHelper
dbconfg = {
    'host': '127.0.0.1',
    'port': 1521,
    'db':'orcl',
    'username': 'test',
    'password': 'test'
}
helper = OracleHelper(dbconfg)
sql = "select * from user"
users = helper.select(sql=sql)
print(users)

参与贡献

  1. Fork 本仓库
  2. 新建 Feat_xxx 分支
  3. 提交代码
  4. 新建 Pull Request
BSD 3-Clause License Copyright (c) 2019, fangzheng All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

简介

Python数据库帮助类(支持Hive、MySQL、Oracle、SqlServer) 展开 收起
Python
BSD-3-Clause
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/taikun928/python-dbhelper.git
git@gitee.com:taikun928/python-dbhelper.git
taikun928
python-dbhelper
python-dbhelper
master

搜索帮助

344bd9b3 5694891 D2dac590 5694891