pymysql模块
pymysql是一个连接mysql数据库的第三方模块,可作为连接mysql数据库的客户端,对数据库进行增删改查操作
<span>#</span><span> 下载第三方模块:pip3 install pymysql</span>
www#gaodaima.com来源gao@daima#com搞(%代@#码@网搞代码
如何连接数据库并对数据库进行操作:
<span>#</span><span> 1.连接数据库</span> <span>"""</span><span> 需要传入的参数: host=None, user=None, password="", database=None, port=0, charset="", autocommit=True # 默认提交 </span><span>"""</span><span> client </span>=<span> pymysql.connect( host</span>=<span>"</span><span>127.0.0.1</span><span>"</span><span>, port</span>=3306<span>, user</span>=<span>"</span><span>root</span><span>"</span><span>, password</span>=<span>"</span><span>123</span><span>"</span><span>, database</span>=<span>"test</span><span>"</span><span>, charset</span>=<span>"</span><span>utf8</span><span>"</span>, <span>#</span><span> 一定不要写utf-8</span> autocommit=<span>True ) </span><span>#</span><span> print(client) # 打印结果:<pymysql.connections.Connection object at 0x00000000028A8B88></span> <span>#</span><span> 2.获取游标对象。游标:可以用来提交sql命令</span> cursor_obj =<span> client.cursor(pymysql.cursors.DictCursor) </span><span>#</span><span> 3.通过execute可以提交sql语句(增删改查都可以,可以使用try来捕获异常)</span> <span>try</span><span>: </span><span>#</span><span> 1)查数据</span> sql = <span>"</span><span>select * from books</span><span>"</span> <span>#</span><span> 输入sql语句,不用写分号</span> <span>#</span><span> 2)插入数据</span> <span>#</span><span> sql = "insert into student(id, name, sex, birthday, class) values("110", "张芳", "男", "1946", "95030")" # 不用写分号</span> <span>#</span><span> 提交sql语句</span> <span> cursor_obj.execute(sql) </span><span>#</span><span> 提交后,通过cursor_obj对象.fetchall()获取所查询到的结果</span> data =<span> cursor_obj.fetchall() </span><span>for</span> i <span>in</span><span> data: </span><span>print</span><span>(i) </span><span>except</span><span> Exception as e: </span><span>print</span><span>(e) <br></span><span>#</span><span> 关闭游标</span> <span>cursor_obj.close() </span><span>#</span><span> 关闭客户端连接</span> client.close()
执行结果(以下为我本地数据库的数据):
{<span>"</span><span>BookID</span><span>"</span>: 11425, <span>"</span><span>BookTitle</span><span>"</span>: <span>"</span><span>Python</span><span>"</span>, <span>"</span><span>Copyright</span><span>"</span>: 1984<span>} {</span><span>"</span><span>BookID</span><span>"</span>: 12786, <span>"</span><span>BookTitle</span><span>"</span>: <span>"</span><span>Java</span><span>"</span>, <span>"</span><span>Copyright</span><span>"</span>: 1934<span>} {</span><span>"</span><span>BookID</span><span>"</span>: 13331, <span>"</span><span>BookTitle</span><span>"</span>: <span>"</span><span>MySQL</span><span>"</span>, <span>"</span><span>Copyright</span><span>"</span>: 1919<span>} {</span><span>"</span><span>BookID</span><span>"</span>: 14356, <span>"</span><span>BookTitle</span><span>"</span>: <span>"</span><span>PHP</span><span>"</span>, <span>"</span><span>Copyright</span><span>"</span>: 1966<span>} {</span><span>"</span><span>BookID</span><span>"</span>: 15729, <span>"</span><span>BookTitle</span><span>"</span>: <span>"</span><span>PERL</span><span>"</span>, <span>"</span><span>Copyright</span><span>"</span>: 1932<span>} {</span><span>"</span><span>BookID</span><span>"</span>: 16284, <span>"</span><span>BookTitle</span><span>"</span>: <span>"</span><span>Oracle</span><span>"</span>, <span>"</span><span>Copyright</span><span>"</span>: 1996}