<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;">#</span><span style="color: #008000;"> 初始化实例方法</span>
<span style="color: #0000ff;">def</span> <span style="color: #800080;">__init__</span><span style="color: #000000;">(self):
self.conn </span>=<span style="color: #000000;"> None
self.cursor </span>=<span style="color: #000000;"> None
self.connect()
</span><span style="color: #008000;">#</span><span style="color: #008000;"> 连接数据库</span>
<span style="color: #0000ff;">def</span><span style="color: #000000;"> connect(self):
self.conn </span>=<span style="color: #000000;"> pymysql.connect(
host</span>=DB_CONFIG[<span style="color: #800000;">"</span><span style="color: #800000;">host</span><span style="color: #800000;">"</span><span style="color: #000000;">],charset</span>=DB_CONFIG[<span style="color: #800000;">"</span><span style="color: #800000;">charset</span><span style="color: #800000;">"</span><span style="color: #000000;">]
)
self.cursor </span>= self.conn.cursor(cursor=<span style="color: #000000;">pymysql.cursors.DictCursor)
</span><span style="color: #008000;">#</span><span style="color: #008000;"> 查询多条数据</span>
<span style="color: #0000ff;">def</span> get_list(self,sql,args=<span style="color: #000000;">None):
self.cursor.execute(sql,args)
result </span>=<span style="color: #000000;"> self.cursor.fetchall()
</span><span style="color: #0000ff;">return</span><span style="color: #000000;"> result
</span><span style="color: #008000;">#</span><span style="color: #008000;"> 查询单条数据</span>
<span style="color: #0000ff;">def</span> get_one(self,args)
result </span>=<span style="color: #000000;"> self.cursor.fetchone()
</span><span style="color: #0000ff;">return</span><span style="color: #000000;"> result
</span><span style="color: #008000;">#</span><span style="color: #008000;"> 执行单条SQL语句</span>
<span style="color: #0000ff;">def</span> moddify(self,args)
self.conn.commit()
</span><span style="color: #008000;">#</span><span style="color: #008000;"> 创建单条记录的语句</span>
<span style="color: #0000ff;">def</span> create(self,args)
self.conn.commit()
last_id </span>=<span style="color: #000000;"> self.cursor.lastrowid
</span><span style="color: #0000ff;">return</span><span style="color: #000000;"> last_id
</span><span style="color: #008000;">#</span><span style="color: #008000;"> 关闭数据库cursor和连接</span>
<span style="color: #0000ff;">def</span><span style="color: #000000;"> 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语句可以实现缩进结束自动关闭文件句柄的例子。