代码拉取完成,页面将自动刷新
同步操作将从 fangzheng/python-dbhelper 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
Python数据库帮助类(支持Hive、MySQL、Oracle、SqlServer)
Python版本:Python3.7
git clone https://gitee.com/fangzheng0518/python-dbhelper.git
cd python-dbhelper
pip install -r requirements.txt
python3 setup.py install
注意:
如果第3步报错: ImportError: No module named Cython.Distutils
则需要先安装Cython
安装命令pip install Cython
然后再继续执行第3步pip install -r requirements.txt
方法参数:
方法参数:
返回结果: 返回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)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。