首页 技术 正文
技术 2022年11月6日
0 收藏 934 点赞 1,043 浏览 1762 个字

目录机构如下:

python的dbutil

dbutil代码如下:

#!/usr/bin/python
# -*- coding:utf-8 -*-import configparser
import pymysqlclass dbutil:
# dbsection为配置文件中的section
def __init__(self,dbsection):
self._conn=self.dbConn(dbsection)
if(self._conn):
self._cursor=self._conn.cursor()
def dbConn(self,dbsection):
#读取db.ini文件
cf = configparser.ConfigParser()
cf.read("../config/db.ini")
dbhost = cf.get(dbsection,"host")
dbport = cf.getint(dbsection,"port")
dbuser = cf.get(dbsection,"user")
dbpassword = cf.get(dbsection,"password")
dbname = cf.get(dbsection,"dbname")
dbcharset = cf.get(dbsection,"charset") #开始数据库连接
try:
conn = pymysql.Connect(host=dbhost,port=dbport,user=dbuser,passwd=dbpassword,db=dbname,charset=dbcharset)
except:
print("连接失败 host=%s,port=%d,user=%s,dbpassword=%s,db=%s"%(dbhost,dbport,dbuser,dbpassword,dbname))
conn=False
return conn # 获取查询结果集
def fetch_all(self, sql):
res = ''
if (self._conn):
try:
self._cursor.execute(sql)
res = self._cursor.fetchall()
except Exception as data:
res = False
print("query database exception, %s" % data)
return res # 执行更新语句
def update(self, sql):
flag = False
if (self._conn):
try:
self._cursor.execute(sql)
self._conn.commit()
flag = True
except Exception as data:
flag = False
print("update database exception, %s" % data)
return flag # 关闭数据库连接
def close(self):
if (self._conn):
# print(type(self._conn)=='object')
try:
# if (type(self._cursor) == 'object'):
self._cursor.close()
# if (type(self._conn) == 'object'):
self._conn.close()
except Exception as data:
print("close database exception, %s,%s,%s" % (data, type(self._cursor), type(self._conn)))if __name__=="__main__":
dbutil=dbutil("biz_mysql")
res=dbutil.fetch_all("select * from wm_people limit %d"%(2))
dbutil.close()
print(len(res))
for dd in res:
print(dd[0])

对应的db.ini如下

[biz_mysql]
host = 10.15.17.xxx
port = 3306
user = xxx
password = 123456
dbname = xxx
charset = utf8[data_mysql]
host = 10.15.17.xxx
port = 3306
user = biz
password = 123456
dbname = xxx
charset = utf8
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,119
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,591
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,436
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,207
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,843
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,928