加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

pymysql操作数据库优化

发布时间:2020-12-17 00:03:05 所属栏目:Python 来源:网络整理
导读:h1 id="autoid-0-0-0"pymysql操作数据库优化 我们之前使用pymysql操作数据库的操作都是写死在视图函数中的,并且很多都是重复的代码。 我们可以优化一下,把重复的代码提取出来,写成函数: span style="color: #008000;"# span style="color: #008000;" 定

<h1 id="autoid-0-0-0">pymysql操作数据库优化

我们之前使用pymysql操作数据库的操作都是写死在视图函数中的,并且很多都是重复的代码。

我们可以优化一下,把重复的代码提取出来,写成函数:

<span style="color: #008000;">#<span style="color: #008000;"> 定义一个数据库相关的配置项
DB_CONFIG =<span style="color: #000000;"> {
<span style="color: #800000;">"<span style="color: #800000;">host<span style="color: #800000;">": <span style="color: #800000;">"<span style="color: #800000;">127.0.0.1<span style="color: #800000;">"<span style="color: #000000;">,<span style="color: #800000;">"<span style="color: #800000;">port<span style="color: #800000;">": 3306<span style="color: #000000;">,<span style="color: #800000;">"<span style="color: #800000;">user<span style="color: #800000;">": <span style="color: #800000;">"<span style="color: #800000;">root<span style="color: #800000;">"<span style="color: #000000;">,<span style="color: #800000;">"<span style="color: #800000;">passwd<span style="color: #800000;">": <span style="color: #800000;">"<span style="color: #800000;">root1234<span style="color: #800000;">"<span style="color: #000000;">,<span style="color: #800000;">"<span style="color: #800000;">db<span style="color: #800000;">": <span style="color: #800000;">"<span style="color: #800000;">mysite<span style="color: #800000;">"<span style="color: #000000;">,<span style="color: #800000;">"<span style="color: #800000;">charset<span style="color: #800000;">": <span style="color: #800000;">"<span style="color: #800000;">utf8<span style="color: #800000;">"<span style="color: #000000;">
}

<span style="color: #008000;">#<span style="color: #008000;"> 查询多条数据函数
<span style="color: #0000ff;">def get_list(sql,args=<span style="color: #000000;">None):
conn =<span style="color: #000000;"> pymysql.connect(
host=DB_CONFIG[<span style="color: #800000;">"<span style="color: #800000;">host<span style="color: #800000;">"<span style="color: #000000;">],port=DB_CONFIG[<span style="color: #800000;">"<span style="color: #800000;">port<span style="color: #800000;">"<span style="color: #000000;">],user=DB_CONFIG[<span style="color: #800000;">"<span style="color: #800000;">user<span style="color: #800000;">"<span style="color: #000000;">],passwd=DB_CONFIG[<span style="color: #800000;">"<span style="color: #800000;">passwd<span style="color: #800000;">"<span style="color: #000000;">],db=DB_CONFIG[<span style="color: #800000;">"<span style="color: #800000;">db<span style="color: #800000;">"<span style="color: #000000;">],charset=DB_CONFIG[<span style="color: #800000;">"<span style="color: #800000;">charset<span style="color: #800000;">"<span style="color: #000000;">]
)
cursor = conn.cursor(cursor=<span style="color: #000000;">pymysql.cursors.DictCursor)
cursor.execute(sql,args)
result =<span style="color: #000000;"> cursor.fetchall()
cursor.close()
conn.close()
<span style="color: #0000ff;">return<span style="color: #000000;"> result

<span style="color: #008000;">#<span style="color: #008000;"> 查询单挑数据函数
<span style="color: #0000ff;">def get_one(sql,args)
result =<span style="color: #000000;"> cursor.fetchone()
cursor.close()
conn.close()
<span style="color: #0000ff;">return<span style="color: #000000;"> result

<span style="color: #008000;">#<span style="color: #008000;"> 修改记录
<span style="color: #0000ff;">def modify(sql,args)
conn.commit()
cursor.close()
conn.close()

<span style="color: #008000;">#<span style="color: #008000;"> 创建记录
<span style="color: #0000ff;">def create(sql,args)
conn.commit()
<span style="color: #008000;">#<span style="color: #008000;"> 返回刚才创建的那条数据的ID
last_id =<span style="color: #000000;"> cursor.lastrowid
cursor.close()
conn.close()
<span style="color: #0000ff;">return last_id

这样只要在需要连接数据库做操作的时候,只需要调用我们上面定义好的函数就可以了。

但是这样还是有问题,当我要大批量创建数据的时候,就需要多次调用create方法了,相当于多次连接多次提交。

可以继续优化下,把数据库的连接重用,做到只需一次连接就可执行多次操作。

</span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt; 初始化实例方法</span> <span style="color: #0000ff;"&gt;def</span> <span style="color: #800080;"&gt;__init__</span><span style="color: #000000;"&gt;(self): self.conn </span>=<span style="color: #000000;"&gt; None self.cursor </span>=<span style="color: #000000;"&gt; None self.connect() </span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt; 连接数据库</span> <span style="color: #0000ff;"&gt;def</span><span style="color: #000000;"&gt; connect(self): self.conn </span>=<span style="color: #000000;"&gt; pymysql.connect( host</span>=DB_CONFIG[<span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;host</span><span style="color: #800000;"&gt;"</span><span style="color: #000000;"&gt;],charset</span>=DB_CONFIG[<span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;charset</span><span style="color: #800000;"&gt;"</span><span style="color: #000000;"&gt;] ) self.cursor </span>= self.conn.cursor(cursor=<span style="color: #000000;"&gt;pymysql.cursors.DictCursor) </span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt; 查询多条数据</span> <span style="color: #0000ff;"&gt;def</span> get_list(self,sql,args=<span style="color: #000000;"&gt;None): self.cursor.execute(sql,args) result </span>=<span style="color: #000000;"&gt; self.cursor.fetchall() </span><span style="color: #0000ff;"&gt;return</span><span style="color: #000000;"&gt; result </span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt; 查询单条数据</span> <span style="color: #0000ff;"&gt;def</span> get_one(self,args) result </span>=<span style="color: #000000;"&gt; self.cursor.fetchone() </span><span style="color: #0000ff;"&gt;return</span><span style="color: #000000;"&gt; result </span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt; 执行单条SQL语句</span> <span style="color: #0000ff;"&gt;def</span> moddify(self,args) self.conn.commit() </span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt; 创建单条记录的语句</span> <span style="color: #0000ff;"&gt;def</span> create(self,args) self.conn.commit() last_id </span>=<span style="color: #000000;"&gt; self.cursor.lastrowid </span><span style="color: #0000ff;"&gt;return</span><span style="color: #000000;"&gt; last_id </span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt; 关闭数据库cursor和连接</span> <span style="color: #0000ff;"&gt;def</span><span style="color: #000000;"&gt; close(self): self.cursor.close() self.conn.close()</span></pre>

我们把我们数据库的相关操作都封装成一个类,在用到的时候,只需要生成一个实例,并对实例调用相应的操作方法就可以了。

db == db.get_list(= db.get_list(

但是,我如果要批量执行多个创建操作,虽然只建立了一次数据库连接但是还是会多次提交,可不可以改成一次连接,一次提交呢?

可以,只需要用上pymysql的executemany()方法就可以了。

给我们的 SQLManager类添加一个批量执行的?multi_modify()方法就可以了。

multi_modify(self,args=
最后,我们每次操作完数据库之后都要手动关闭,可不可以写成自动关闭的呢?

联想到我们之前学过的文件操作,使用with语句可以实现缩进结束自动关闭文件句柄的例子。

我们来把我们的数据库连接类SQLManager类再优化下,使其支持with语句操作。

</span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt; 初始化实例方法</span> <span style="color: #0000ff;"&gt;def</span> <span style="color: #800080;"&gt;__init__</span><span style="color: #000000;"&gt;(self): self.conn </span>=<span style="color: #000000;"&gt; None self.cursor </span>=<span style="color: #000000;"&gt; None self.connect() </span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt; 连接数据库</span> <span style="color: #0000ff;"&gt;def</span><span style="color: #000000;"&gt; connect(self): self.conn </span>=<span style="color: #000000;"&gt; pymysql.connect( host</span>=DB_CONFIG[<span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;host</span><span style="color: #800000;"&gt;"</span><span style="color: #000000;"&gt;],args) self.conn.commit() </span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt; 执行多条SQL语句</span> <span style="color: #0000ff;"&gt;def</span> multi_modify(self,args=<span style="color: #000000;"&gt;None): self.cursor.executemany(sql,args) self.conn.commit() last_id </span>=<span style="color: #000000;"&gt; self.cursor.lastrowid </span><span style="color: #0000ff;"&gt;return</span><span style="color: #000000;"&gt; last_id </span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt; 关闭数据库cursor和连接</span> <span style="color: #0000ff;"&gt;def</span><span style="color: #000000;"&gt; close(self): self.cursor.close() self.conn.close() </span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt; 进入with语句自动执行</span> <span style="color: #0000ff;"&gt;def</span> <span style="color: #800080;"&gt;__enter__</span><span style="color: #000000;"&gt;(self): </span><span style="color: #0000ff;"&gt;return</span><span style="color: #000000;"&gt; self </span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt; 退出with语句块自动执行</span> <span style="color: #0000ff;"&gt;def</span> <span style="color: #800080;"&gt;__exit__</span><span style="color: #000000;"&gt;(self,exc_type,exc_val,exc_tb): self.close()</span></pre>

现阶段,我们只需要优化到这一步就可以,后面的项目实战中会继续优化。如使用数据库连接池等。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读