首页 Python python学习笔记十一:操作mysql

python学习笔记十一:操作mysql

一、安装MySQL-python# yum install -y MySQL-python二、打开数据库连接#!/usr/bin/pythonimport MySQLdbconn = MySQLdb.connect(user=root,passwd=admin,host=127.0.0.1)conn.select_db(test)cur = conn.cursor()三、操作数据库def insertdb():sql = insert

一、安装MySQL-python

# -y MySQL-python

conn = MySQLdb.connect(user=<span style=”color: #800000;”>'<span style=”color: #800000;”>root<span style=”color: #800000;”>’,passwd=<span style=”color: #800000;”>'<span style=”color: #800000;”>admin<span style=”color: #800000;”>’,host=<span style=”color: #800000;”>'<span style=”color: #800000;”>127.0.0.1<span style=”color: #800000;”>'<span style=”color: #000000;”>)
conn.select_db(<span style=”color: #800000;”>'<span style=”color: #800000;”>test<span style=”color: #800000;”>'<span style=”color: #000000;”>)
cur = conn.cursor()

<div class=”cnblogs_code”>

= = sql % (, 

<span style="color: #0000ff;">def<span style="color: #000000;"> selectdb():
sql = <span style="color: #800000;">'<span style="color: #800000;">select name from test where sort = "%s"<span style="color: #800000;">'<span style="color: #000000;">
exsql = sql % (<span style="color: #800000;">'<span style="color: #800000;">python<span style="color: #800000;">'<span style="color: #000000;">)
count =<span style="color: #000000;"> cur.execute(exsql)
<span style="color: #0000ff;">for row <span style="color: #0000ff;">in<span style="color: #000000;"> cur:
<span style="color: #0000ff;">print<span style="color: #000000;"> row

</span><span style="color: #0000ff;"&gt;print</span> <span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;cursor move to top:</span><span style="color: #800000;"&gt;'</span><span style="color: #000000;"&gt;
cur.scroll(0,</span><span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;absolute</span><span style="color: #800000;"&gt;'</span><span style="color: #000000;"&gt;)

row </span>=<span style="color: #000000;"&gt; cur.fetchone()
</span><span style="color: #0000ff;"&gt;while</span> row <span style="color: #0000ff;"&gt;is</span> <span style="color: #0000ff;"&gt;not</span><span style="color: #000000;"&gt; None:
    </span><span style="color: #0000ff;"&gt;print</span><span style="color: #000000;"&gt; row
    row </span>=<span style="color: #000000;"&gt; cur.fetchone()

</span><span style="color: #0000ff;"&gt;print</span> <span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;cursor move to top:</span><span style="color: #800000;"&gt;'</span><span style="color: #000000;"&gt;
cur.scroll(0,</span><span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;absolute</span><span style="color: #800000;"&gt;'</span><span style="color: #000000;"&gt;)

many </span>=<span style="color: #000000;"&gt; cur.fetchmany(count)
</span><span style="color: #0000ff;"&gt;print</span><span style="color: #000000;"&gt; many

<span style="color: #0000ff;">def<span style="color: #000000;"> deletedb():
sql = <span style="color: #800000;">'<span style="color: #800000;">delete from test where sort = "%s"<span style="color: #800000;">'<span style="color: #000000;">
exsql = sql % (<span style="color: #800000;">'<span style="color: #800000;">python<span style="color: #800000;">'<span style="color: #000000;">)
cur.execute(exsql)
conn.commit()
<span style="color: #0000ff;">return <span style="color: #800000;">'<span style="color: #800000;">delete success<span style="color: #800000;">'

<span style="color: #0000ff;">print<span style="color: #000000;"> insertdb()
<span style="color: #0000ff;">print<span style="color: #000000;"> insertdb()
selectdb()
<span style="color: #0000ff;">print deletedb()

注意顺序。

本文来自网络,不代表青岛站长网立场。转载请注明出处: https://www.0532zz.com/html/kaifa/python/20200723/7124.html
上一篇
下一篇

作者: dawei

【声明】:青岛站长网内容转载自互联网,其相关言论仅代表作者个人观点绝非权威,不代表本站立场。如您发现内容存在版权问题,请提交相关链接至邮箱:bqsm@foxmail.com,我们将及时予以处理。

为您推荐

返回顶部