python3 django mysql 增删改查
使用Python的django框架连接数据库,操作数据库代码:
import logging from django.db import connection LOG = logging.getLogger("boss") def dictfetchall(cursor): "Return all rows from a cursor as a dict" desc = cursor.description if desc == None: return [] columns = [col[0] for col in desc] # for row in cursor.fetchall(): # rows.append(row) return [ dict(zip(columns, row)) for row in cursor.fetchall() ] def dictfetone(cursor): desc = cursor.description if desc == None: return None columns = [col[0] for col in desc] row = cursor.fetchone() if row == None: return None return dict(zip(columns,row)) def fetchall(sql,params=[]): cursor =connection.cursor() cursor.execute(sql,params) ret = dictfetchall(cursor) return ret def fetchone(sql,params=[]): cursor =connection.cursor() cursor.execute(sql,params) ret = dictfetone(cursor) cursor.close() return ret def executeDb(sql,params=[]): cursor =connection.cursor() ret = cursor.execute(sql,params) cursor.close() return ret
在代码中看到 cursor执行完之后,执行close。想到diango的mysql连接是不是没有做连接池?带着这个疑问上网查看,发现果然有这方面的文档。后来看官网时发现:
django最新版本已经包含了连接池,通过修改配置控制,官方文档:https://docs.djangoproject.com/en/1.9/ref/databases/
证明最好还是看官方文档。
转载自:http://blog.csdn.net/kelindame/article/details/52443972?locationNum=2&fps=1#
如果觉得对您有帮助,麻烦您点一下推荐,谢谢!
好记忆不如烂笔头
好记忆不如烂笔头