时间:2021-07-01 10:21:17 帮助过:15人阅读
返回字典格式数据:
- <span style="color: #000000"># 导入pymysql模块
- import pymysql
- # 连接database
- conn </span>= pymysql.connect(host=“你的数据库地址”, user=“用户名”,password=“密码”,database=“数据库名”,charset=<span style="color: #000000">“utf8”)
- # 得到一个可以执行SQL语句并且将结果作为字典返回的游标
- cursor </span>= conn.cursor(cursor=<span style="color: #000000">pymysql.cursors.DictCursor)
- # 定义要执行的SQL语句
- sql </span>= """<span style="color: #000000">
- CREATE TABLE USER1 (
- id INT auto_increment PRIMARY KEY ,
- name CHAR(10) NOT NULL UNIQUE,
- age TINYINT NOT NULL
- )ENGINE=innodb DEFAULT CHARSET=utf8;
- </span>"""<span style="color: #000000">
- # 执行SQL语句
- cursor.execute(sql)
- # 关闭光标对象
- cursor.close()
- # 关闭数据库连接
- conn.close()</span>
注意:
charset=“utf8”,编码不要写成"utf-8"
- <span style="color: #000000"># 导入pymysql模块
- import pymysql
- # 连接database
- conn </span>= pymysql.connect(host=“你的数据库地址”, user=“用户名”,password=“密码”,database=“数据库名”,charset=<span style="color: #000000">“utf8”)
- # 得到一个可以执行SQL语句的光标对象
- cursor </span>=<span style="color: #000000"> conn.cursor()
- sql </span>= "INSERT INTO USER1(name, age) VALUES (%s, %s);"<span style="color: #000000">
- username </span>= "Alex"<span style="color: #000000">
- age </span>= 18<span style="color: #000000">
- # 执行SQL语句
- cursor.execute(sql, [username, age])
- # 提交事务
- conn.commit()
- cursor.close()
- conn.close()</span>
插入数据失败回滚:
- <span style="color: #008000">#</span><span style="color: #008000"> 导入pymysql模块</span>
- <span style="color: #0000ff">import</span><span style="color: #000000"> pymysql
- </span><span style="color: #008000">#</span><span style="color: #008000"> 连接database</span>
- conn = pymysql.connect(host=“你的数据库地址”, user=“用户名”,password=“密码”,database=“数据库名”,charset=<span style="color: #000000">“utf8”)
- </span><span style="color: #008000">#</span><span style="color: #008000"> 得到一个可以执行SQL语句的光标对象</span>
- cursor =<span style="color: #000000"> conn.cursor()
- sql </span>= <span style="color: #800000">"</span><span style="color: #800000">INSERT INTO USER1(name, age) VALUES (%s, %s);</span><span style="color: #800000">"</span><span style="color: #000000">
- username </span>= <span style="color: #800000">"</span><span style="color: #800000">Alex</span><span style="color: #800000">"</span><span style="color: #000000">
- age </span>= 18
- <span style="color: #0000ff">try</span><span style="color: #000000">:
- </span><span style="color: #008000">#</span><span style="color: #008000"> 执行SQL语句</span>
- <span style="color: #000000"> cursor.execute(sql, [username, age])
- </span><span style="color: #008000">#</span><span style="color: #008000"> 提交事务</span>
- <span style="color: #000000"> conn.commit()
- </span><span style="color: #0000ff">except</span><span style="color: #000000"> Exception as e:
- </span><span style="color: #008000">#</span><span style="color: #008000"> 有异常,回滚事务</span>
- <span style="color: #000000"> conn.rollback()
- cursor.close()
- conn.close()</span>
获取插入数据的ID(关联操作时会用到)
- <span style="color: #008000">#</span><span style="color: #008000"> 导入pymysql模块</span>
- <span style="color: #0000ff">import</span><span style="color: #000000"> pymysql
- </span><span style="color: #008000">#</span><span style="color: #008000"> 连接database</span>
- conn = pymysql.connect(host=“你的数据库地址”, user=“用户名”,password=“密码”,database=“数据库名”,charset=<span style="color: #000000">“utf8”)
- </span><span style="color: #008000">#</span><span style="color: #008000"> 得到一个可以执行SQL语句的光标对象</span>
- cursor =<span style="color: #000000"> conn.cursor()
- sql </span>= <span style="color: #800000">"</span><span style="color: #800000">INSERT INTO USER1(name, age) VALUES (%s, %s);</span><span style="color: #800000">"</span><span style="color: #000000">
- username </span>= <span style="color: #800000">"</span><span style="color: #800000">Alex</span><span style="color: #800000">"</span><span style="color: #000000">
- age </span>= 18
- <span style="color: #0000ff">try</span><span style="color: #000000">:
- </span><span style="color: #008000">#</span><span style="color: #008000"> 执行SQL语句</span>
- <span style="color: #000000"> cursor.execute(sql, [username, age])
- </span><span style="color: #008000">#</span><span style="color: #008000"> 提交事务</span>
- <span style="color: #000000"> conn.commit()
- </span><span style="color: #008000">#</span><span style="color: #008000"> 提交之后,获取刚插入的数据的ID</span>
- last_id =<span style="color: #000000"> cursor.lastrowid
- </span><span style="color: #0000ff">except</span><span style="color: #000000"> Exception as e:
- </span><span style="color: #008000">#</span><span style="color: #008000"> 有异常,回滚事务</span>
- <span style="color: #000000"> conn.rollback()
- cursor.close()
- conn.close()</span>
批量执行
- <span style="color: #008000">#</span><span style="color: #008000"> 导入pymysql模块</span>
- <span style="color: #0000ff">import</span><span style="color: #000000"> pymysql
- </span><span style="color: #008000">#</span><span style="color: #008000"> 连接database</span>
- conn = pymysql.connect(host=“你的数据库地址”, user=“用户名”,password=“密码”,database=“数据库名”,charset=<span style="color: #000000">“utf8”)
- </span><span style="color: #008000">#</span><span style="color: #008000"> 得到一个可以执行SQL语句的光标对象</span>
- cursor =<span style="color: #000000"> conn.cursor()
- sql </span>= <span style="color: #800000">"</span><span style="color: #800000">INSERT INTO USER1(name, age) VALUES (%s, %s);</span><span style="color: #800000">"</span><span style="color: #000000">
- data </span>= [(<span style="color: #800000">"</span><span style="color: #800000">Alex</span><span style="color: #800000">"</span>, 18), (<span style="color: #800000">"</span><span style="color: #800000">Egon</span><span style="color: #800000">"</span>, 20), (<span style="color: #800000">"</span><span style="color: #800000">Yuan</span><span style="color: #800000">"</span>, 21<span style="color: #000000">)]
- </span><span style="color: #0000ff">try</span><span style="color: #000000">:
- </span><span style="color: #008000">#</span><span style="color: #008000"> 批量执行多条插入SQL语句</span>
- <span style="color: #000000"> cursor.executemany(sql, data)
- </span><span style="color: #008000">#</span><span style="color: #008000"> 提交事务</span>
- <span style="color: #000000"> conn.commit()
- </span><span style="color: #0000ff">except</span><span style="color: #000000"> Exception as e:
- </span><span style="color: #008000">#</span><span style="color: #008000"> 有异常,回滚事务</span>
- <span style="color: #000000"> conn.rollback()
- cursor.close()
- conn.close()</span>
- <span style="color: #008000">#</span><span style="color: #008000"> 导入pymysql模块</span>
- <span style="color: #0000ff">import</span><span style="color: #000000"> pymysql
- </span><span style="color: #008000">#</span><span style="color: #008000"> 连接database</span>
- conn = pymysql.connect(host=“你的数据库地址”, user=“用户名”,password=“密码”,database=“数据库名”,charset=<span style="color: #000000">“utf8”)
- </span><span style="color: #008000">#</span><span style="color: #008000"> 得到一个可以执行SQL语句的光标对象</span>
- cursor =<span style="color: #000000"> conn.cursor()
- sql </span>= <span style="color: #800000">"</span><span style="color: #800000">DELETE FROM USER1 WHERE id=%s;</span><span style="color: #800000">"</span>
- <span style="color: #0000ff">try</span><span style="color: #000000">:
- cursor.execute(sql, [</span>4<span style="color: #000000">])
- </span><span style="color: #008000">#</span><span style="color: #008000"> 提交事务</span>
- <span style="color: #000000"> conn.commit()
- </span><span style="color: #0000ff">except</span><span style="color: #000000"> Exception as e:
- </span><span style="color: #008000">#</span><span style="color: #008000"> 有异常,回滚事务</span>
- <span style="color: #000000"> conn.rollback()
- cursor.close()
- conn.close()</span>
- <span style="color: #008000">#</span><span style="color: #008000"> 导入pymysql模块</span>
- <span style="color: #0000ff">import</span><span style="color: #000000"> pymysql
- </span><span style="color: #008000">#</span><span style="color: #008000"> 连接database</span>
- conn = pymysql.connect(host=“你的数据库地址”, user=“用户名”,password=“密码”,database=“数据库名”,charset=<span style="color: #000000">“utf8”)
- </span><span style="color: #008000">#</span><span style="color: #008000"> 得到一个可以执行SQL语句的光标对象</span>
- cursor =<span style="color: #000000"> conn.cursor()
- </span><span style="color: #008000">#</span><span style="color: #008000"> 修改数据的SQL语句</span>
- sql = <span style="color: #800000">"</span><span style="color: #800000">UPDATE USER1 SET age=%s WHERE name=%s;</span><span style="color: #800000">"</span><span style="color: #000000">
- username </span>= <span style="color: #800000">"</span><span style="color: #800000">Alex</span><span style="color: #800000">"</span><span style="color: #000000">
- age </span>= 80
- <span style="color: #0000ff">try</span><span style="color: #000000">:
- </span><span style="color: #008000">#</span><span style="color: #008000"> 执行SQL语句</span>
- <span style="color: #000000"> cursor.execute(sql, [age, username])
- </span><span style="color: #008000">#</span><span style="color: #008000"> 提交事务</span>
- <span style="color: #000000"> conn.commit()
- </span><span style="color: #0000ff">except</span><span style="color: #000000"> Exception as e:
- </span><span style="color: #008000">#</span><span style="color: #008000"> 有异常,回滚事务</span>
- <span style="color: #000000"> conn.rollback()
- cursor.close()
- conn.close()</span>
查询单条数据
- <span style="color: #008000">#</span><span style="color: #008000"> 导入pymysql模块</span>
- <span style="color: #0000ff">import</span><span style="color: #000000"> pymysql
- </span><span style="color: #008000">#</span><span style="color: #008000"> 连接database</span>
- conn = pymysql.connect(host=“你的数据库地址”, user=“用户名”,password=“密码”,database=“数据库名”,charset=<span style="color: #000000">“utf8”)
- </span><span style="color: #008000">#</span><span style="color: #008000"> 得到一个可以执行SQL语句的光标对象</span>
- cursor =<span style="color: #000000"> conn.cursor()
- </span><span style="color: #008000">#</span><span style="color: #008000"> 查询数据的SQL语句</span>
- sql = <span style="color: #800000">"</span><span style="color: #800000">SELECT id,name,age from USER1 WHERE id=1;</span><span style="color: #800000">"</span>
- <span style="color: #008000">#</span><span style="color: #008000"> 执行SQL语句</span>
- <span style="color: #000000">cursor.execute(sql)
- </span><span style="color: #008000">#</span><span style="color: #008000"> 获取单条查询数据</span>
- ret =<span style="color: #000000"> cursor.fetchone()
- cursor.close()
- conn.close()
- </span><span style="color: #008000">#</span><span style="color: #008000"> 打印下查询结果</span>
- <span style="color: #0000ff">print</span>(ret)
查询多条数据
- <span style="color: #008000">#</span><span style="color: #008000"> 导入pymysql模块</span>
- <span style="color: #0000ff">import</span><span style="color: #000000"> pymysql
- </span><span style="color: #008000">#</span><span style="color: #008000"> 连接database</span>
- conn = pymysql.connect(host=“你的数据库地址”, user=“用户名”,password=“密码”,database=“数据库名”,charset=<span style="color: #000000">“utf8”)
- </span><span style="color: #008000">#</span><span style="color: #008000"> 得到一个可以执行SQL语句的光标对象</span>
- cursor =<span style="color: #000000"> conn.cursor()
- </span><span style="color: #008000">#</span><span style="color: #008000"> 查询数据的SQL语句</span>
- sql = <span style="color: #800000">"</span><span style="color: #800000">SELECT id,name,age from USER1;</span><span style="color: #800000">"</span>
- <span style="color: #008000">#</span><span style="color: #008000"> 执行SQL语句</span>
- <span style="color: #000000">cursor.execute(sql)
- </span><span style="color: #008000">#</span><span style="color: #008000"> 获取多条查询数据</span>
- ret =<span style="color: #000000"> cursor.fetchall()
- cursor.close()
- conn.close()
- </span><span style="color: #008000">#</span><span style="color: #008000"> 打印下查询结果</span>
- <span style="color: #0000ff">print</span>(ret)
- <span style="color: #008000">#</span><span style="color: #008000"> 可以获取指定数量的数据</span>
- cursor.fetchmany(3<span style="color: #000000">)
- </span><span style="color: #008000">#</span><span style="color: #008000"> 光标按绝对位置移动1</span>
- cursor.scroll(1, mode=<span style="color: #800000">"</span><span style="color: #800000">absolute</span><span style="color: #800000">"</span><span style="color: #000000">)
- </span><span style="color: #008000">#</span><span style="color: #008000"> 光标按照相对位置(当前位置)移动1</span>
- cursor.scroll(1, mode=<span style="color: #800000">"</span><span style="color: #800000">relative</span><span style="color: #800000">"</span>)
Python连接MySQL数据库之pymysql模块使用
标签:sql数据库 values 数据 inno rowid pymysql post ict 数据库名