• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

pymysql模块:连接mysql数据库的模块

python 搞java代码 3年前 (2022-05-21) 16次浏览 已收录 0个评论

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}

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:pymysql模块:连接mysql数据库的模块
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址