Python 边做边学 8.3 工具类--数据库工具(DbUtil)
2017-05-30 14:25
501 查看
原文连接:http://blog.csdn.net/tomorrow13210073213/article/category/6931287
之前项目一直在用mysql,所以此处也使用mysql做数据存储;
http://blog.csdn.net/nuli888/article/details/51960571
文章开头“python3.x 使用pymysql操作MySQL,python2.x使用mysqldb操作mysql”告诉我们“python3.x 使用pymysql操作MySQL”;用pycharm下载“pymysql”包之后,就可以使用了;
“pymysql”操作数据库与其他编程语言类似:
建立连接
获取游标
执行语句
提交结果
关闭游标
关闭链接
其他方法类似,构建各表数据库操作基础类的的时候,再详细描述各方法的用法;
以上就是我们用到的数据库操作工具类;
以上内容仅供练习,学习使用;
数据持久化
采集到的数据需要保存起来,这是个demo项目,选用什么方式做数据持久化并不是十分重要,重要的是把数据存起来;之前项目一直在用mysql,所以此处也使用mysql做数据存储;
搜索“python3 操作mysql”
搜索“python3 操作mysql”,找到一篇比较靠谱的:http://blog.csdn.net/nuli888/article/details/51960571
文章开头“python3.x 使用pymysql操作MySQL,python2.x使用mysqldb操作mysql”告诉我们“python3.x 使用pymysql操作MySQL”;用pycharm下载“pymysql”包之后,就可以使用了;
“pymysql”操作数据库与其他编程语言类似:
建立连接
获取游标
执行语句
提交结果
关闭游标
关闭链接
DbUtil.py代码
import pymysql from lufaxin.csdn.util import CfgUtil __host = CfgUtil.get_db("host") __user = CfgUtil.get_db("user") __passwd = CfgUtil.get_db("passwd") __db = CfgUtil.get_db("db") __port = int(CfgUtil.get_db("port")) __charset = CfgUtil.get_db("charset") def execute(sql_str): if sql_str is None: raise Exception("参数不能为空:sql_str") if len(sql_str) == 0: raise Exception("参数不能为空:sql_str") try: conn = pymysql.connect(host=__host, user=__user, passwd=__passwd, db=__db, port=__port, charset=__charset) cur = conn.cursor() # 获取一个游标 cur.execute(sql_str) data = cur.fetchall() conn.commit() cur.close() # 关闭游标 conn.close() # 释放数据库资源 return data except Exception as e: raise e # 插入数据,返回数据主键 def execute_insert(insert_str, data): if insert_str is None: raise Exception("参数不能为空:sql_str") if len(insert_str) == 0: raise Exception("参数不能为空:sql_str") try: conn = pymysql.connect(host=__host, user=__user, passwd=__passwd, db=__db, port=__port, charset=__charset) cur = conn.cursor() # 获取一个游标 cur.execute(insert_str, data) data = cur.fetchall() # last_id = cur.lastrowid last_id = conn.insert_id() conn.commit() cur.close() # 关闭游标 conn.close() # 释放数据库资源 return last_id except Exception as e: raise e # 更新数据,返回更新条数 def execute_update(update_str, data): if update_str is None: raise Exception("参数不能为空:update_str") if len(update_str) == 0: raise Exception("参数不能为空:update_str") try: conn = pymysql.connect(host=__host, user=__user, passwd=__passwd, db=__db, port=__port, charset=__charset) cur = conn.cursor() # 获取一个游标 count = cur.execute(update_str, data) conn.commit() cur.close() # 关闭游标 conn.close() # 释放数据库资源 return count except Exception as e: raise e # 执行带参数的查询,返回查询结果 def execute_select(select_str, data): if select_str is None: raise Exception("参数不能为空:sql_str") if len(select_str) == 0: raise Exception("参数不能为空:sql_str") try: conn = pymysql.connect(host=__host, user=__user, passwd=__passwd, db=__db, port=__port, charset=__charset) cur = conn.cursor() # 获取一个游标 cur.execute(select_str, data) data = cur.fetchall() conn.commit() cur.close() # 关闭游标 conn.close() # 释放数据库资源 return data except Exception as e: raise e # 执行带参数的删除 def execute_delete(select_str, data): if select_str is None: raise Exception("参数不能为空:sql_str") if len(select_str) == 0: raise Exception("参数不能为空:sql_str") try: conn = pymysql.connect(host=__host, user=__user, passwd=__passwd, db=__db, port=__port, charset=__charset) cur = conn.cursor() # 获取一个游标 cur.execute(select_str, data) data = cur.fetchall() conn.commit() cur.close() # 关闭游标 conn.close() # 释放数据库资源 return data except Exception as e: raise e
数据库链接
数据库连接信息来自配置文件:__host = CfgUtil.get_db("host") __user = CfgUtil.get_db("user") __passwd = CfgUtil.get_db("passwd") __db = CfgUtil.get_db("db") __port = int(CfgUtil.get_db("port")) __charset = CfgUtil.get_db("charset")
创建连接
通过“pymysql.connect()”方法创建数据库连接:conn = pymysql.connect(host=__host, user=__user, passwd=__passwd, db=__db, port=__port, charset=__charset)
获取游标
cur = conn.cursor() # 获取一个游标
执行语句
cur.execute(sql_str) data = cur.fetchall()
提交结果
conn.commit()
关闭连接,游标
cur.close() # 关闭游标 conn.close() # 释放数据库资源
参数传递
如上面定义的“execute”方法所示,可以只传递一个参数,那就是要执行的sql语句;这种方式执行简单操作可以,但执行复杂插入,更新等操作会比较复杂;所以有了下面几个方法,用来执行不同的操作(增删改查),不再一一介绍;调用方法
from lufaxin.csdn.util import DbUtil ...... sql = "select " + cols_str + " from " + table_name + " where " + t_pk + " = " + str(pk) sel_data = DbUtil.execute(sql) ...... sql = "delete from " + table_name + " where " + pk_name + " = " + str(pk) del_data = DbUtil.execute(sql)
其他方法类似,构建各表数据库操作基础类的的时候,再详细描述各方法的用法;
以上就是我们用到的数据库操作工具类;
以上内容仅供练习,学习使用;
相关文章推荐
- Python 边做边学 8.5 工具类--MD5工具(Md5Util)
- Python 边做边学 9.6 数据库操作--查询条件解析工具(QCondition)
- Python 边做边学 9.8 数据库操作--数据库执行入口工具(MapperExecute)
- Python 边做边学 8.2 工具类--配置文件工具(CfgUtil)
- Python 边做边学 8.7 工具类--缓存工具(RedisUtil)
- Python 边做边学 8.1 工具类--HTTP工具
- Python 边做边学 9.7 数据库操作--查询基础工具(BaseMapper)
- Python 边做边学 8.6 工具类--时间工具(TimeUtil)
- Python 边做边学 8.4 工具类--HTML解析工具(HtmlSoupUtil)
- Python 边做边学 9.2 数据库操作--定义数据字段(DbColumn)
- DBUtil数据库工具封装
- python链接数据库工具类
- DBUtil数据库操作工具类
- Python 工具类与工具函数 —— pair
- 利用Python数据可视化工具plotly从数据库读取数据制作本地图表应用实例
- python操作数据库工具类
- DBUtil 数据库工具类
- Qt数据库之数据库访问工具 DBUtil
- 关于数据库连接操作的工具类DBUtil.java
- java中常用的工具类(三)连接数据库的综合类,DES加密和解密,Base64,ExcelUtil工具类,