python的dbutil

news/2024/7/19 10:08:53 标签: python, 爬虫, 数据库

目录机构如下:

 

 dbutil代码如下:

#!/usr/bin/python
# -*- coding:utf-8 -*-

import configparser
import pymysql

class 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

 

转载于:https://www.cnblogs.com/zipon/p/7590601.html


http://www.niftyadmin.cn/n/785365.html

相关文章

windows安装Hbase

一、下载安装包 官网:http://hbase.apache.org/ 下载地址见文末 安装路径E:\software\hbase-1.3.1 二、修改配置文件 1、修改:E:\software\hbase-1.3.1\conf\hbase-env.cmd(linux环境是修改hbase-env.sh) (1&…

2023年“华数杯”国际大学生数学建模B题收集数据汇总

2023华数杯已经如期开赛,本次比赛作为美赛的模拟赛,赛题和比赛时间都和美赛高度相似。对于本次比赛的B题而言,数据是很重要的,但是由于关于社会稳定性的相关数据过大,或者下载方式很困难等等,大家的收集数据…

关于Hibernate懒加载问题的最终解决方案

看到一篇Hibernate懒加载的文章,所以转载,原地址如下: http://tuoxie007.iteye.com/blog/334853 Hibernate的强大之处之一是懒加载功能,可以有效的降低数据库访问次数和内存使用量。但用的不好就会出现org.hibernate.LazyInitiali…

windows安装Hive

1、环境变量设置 HIVE_HOME D:\DesignSoftware\hive\apache-hive-2.1.0-bin PATH %HIVE_HOME%\bin; 2、hive-site.xml配置 <configuration><!-- WARNING!!! This file is provided for documentation purposes ONLY! --><!-- WARNING!!! Any changes yo…

StratoIO WebPrinter的下载与安装步骤介绍

Strato WebPrinter下载与安装 注&#xff1a;本文档链接可直接分享给最终用户作为安装手册之用 1.下载安装文件 我们提供最新版的通用安装程序&#xff0c;标准版和高级版用户可导航至 http://webprint.stratoio.com/download 点击“高速下载”按钮下载&#xff0c;如下图所示。…

大数据预测房价趋势

大数据预测房价趋势 数据挖掘步骤大概分为以下&#xff1a;1、数据采集2、数据清洗3、数据分析4、显示数据 还是按这4个步骤&#xff0c; 第一&#xff0c;我们用爬虫采集某网的数据&#xff0c;得到房价20180811.txt文件&#xff0c;这里是以广州城市为例。数据中有些有地铁…

一起艳学IntelliJ IDEA

一起艳学IntelliJ IDEA前言1、主题、字体、文件编码2、配置tomcat3、搭建java项目4、搭建java web项目前言 很多新手或者老手都用eclipse或者myeclipse&#xff0c;但是近一年spingboot简单的开发崛起&#xff0c;sping全家桶的IDEA油然而生&#xff0c;大家不得不用idea来编辑…

Jzoj3547 MEX

有一个长度为n的数组{a1,a2,...,an}。m次询问&#xff0c;每次询问一个区间内最小没有出现过的自然数。 这是一个经典的主席树的题目&#xff0c;对于每个节点i开一颗线段树存储[1,i]区间内每个数最后出现的位置&#xff0c;那么查找的时候直接再树上类似平衡树找最小即可 #pra…