当前位置:Gxlcms > 数据库问题 > Python连接MySQL数据库之pymysql模块使用

Python连接MySQL数据库之pymysql模块使用

时间:2021-07-01 10:21:17 帮助过:15人阅读

= pymysql.connect(host=“你的数据库地址”, user=“用户名”,password=“密码”,database=“数据库名”,charset=“utf8”) # 得到一个可以执行SQL语句的光标对象 cursor = conn.cursor() # 定义要执行的SQL语句 sql = """ CREATE TABLE USER1 ( id INT auto_increment PRIMARY KEY , name CHAR(10) NOT NULL UNIQUE, age TINYINT NOT NULL )ENGINE=innodb DEFAULT CHARSET=utf8; """ # 执行SQL语句 cursor.execute(sql) # 关闭光标对象 cursor.close() # 关闭数据库连接 conn.close()

返回字典格式数据:

  1. <span style="color: #000000"># 导入pymysql模块
  2. import pymysql
  3. # 连接database
  4. conn </span>= pymysql.connect(host=“你的数据库地址”, user=“用户名”,password=“密码”,database=“数据库名”,charset=<span style="color: #000000">“utf8”)
  5. # 得到一个可以执行SQL语句并且将结果作为字典返回的游标
  6. cursor </span>= conn.cursor(cursor=<span style="color: #000000">pymysql.cursors.DictCursor)
  7. # 定义要执行的SQL语句
  8. sql </span>= """<span style="color: #000000">
  9. CREATE TABLE USER1 (
  10. id INT auto_increment PRIMARY KEY ,
  11. name CHAR(10) NOT NULL UNIQUE,
  12. age TINYINT NOT NULL
  13. )ENGINE=innodb DEFAULT CHARSET=utf8;
  14. </span>"""<span style="color: #000000">
  15. # 执行SQL语句
  16. cursor.execute(sql)
  17. # 关闭光标对象
  18. cursor.close()
  19. # 关闭数据库连接
  20. conn.close()</span>

注意:

charset=“utf8”,编码不要写成"utf-8"

增删改查操作

  1. <span style="color: #000000"># 导入pymysql模块
  2. import pymysql
  3. # 连接database
  4. conn </span>= pymysql.connect(host=“你的数据库地址”, user=“用户名”,password=“密码”,database=“数据库名”,charset=<span style="color: #000000">“utf8”)
  5. # 得到一个可以执行SQL语句的光标对象
  6. cursor </span>=<span style="color: #000000"> conn.cursor()
  7. sql </span>= "INSERT INTO USER1(name, age) VALUES (%s, %s);"<span style="color: #000000">
  8. username </span>= "Alex"<span style="color: #000000">
  9. age </span>= 18<span style="color: #000000">
  10. # 执行SQL语句
  11. cursor.execute(sql, [username, age])
  12. # 提交事务
  13. conn.commit()
  14. cursor.close()
  15. conn.close()</span>

插入数据失败回滚:

  1. <span style="color: #008000">#</span><span style="color: #008000"> 导入pymysql模块</span>
  2. <span style="color: #0000ff">import</span><span style="color: #000000"> pymysql
  3. </span><span style="color: #008000">#</span><span style="color: #008000"> 连接database</span>
  4. conn = pymysql.connect(host=“你的数据库地址”, user=“用户名”,password=“密码”,database=“数据库名”,charset=<span style="color: #000000">“utf8”)
  5. </span><span style="color: #008000">#</span><span style="color: #008000"> 得到一个可以执行SQL语句的光标对象</span>
  6. cursor =<span style="color: #000000"> conn.cursor()
  7. 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">
  8. username </span>= <span style="color: #800000">"</span><span style="color: #800000">Alex</span><span style="color: #800000">"</span><span style="color: #000000">
  9. age </span>= 18
  10. <span style="color: #0000ff">try</span><span style="color: #000000">:
  11. </span><span style="color: #008000">#</span><span style="color: #008000"> 执行SQL语句</span>
  12. <span style="color: #000000"> cursor.execute(sql, [username, age])
  13. </span><span style="color: #008000">#</span><span style="color: #008000"> 提交事务</span>
  14. <span style="color: #000000"> conn.commit()
  15. </span><span style="color: #0000ff">except</span><span style="color: #000000"> Exception as e:
  16. </span><span style="color: #008000">#</span><span style="color: #008000"> 有异常,回滚事务</span>
  17. <span style="color: #000000"> conn.rollback()
  18. cursor.close()
  19. conn.close()</span>

获取插入数据的ID(关联操作时会用到)

  1. <span style="color: #008000">#</span><span style="color: #008000"> 导入pymysql模块</span>
  2. <span style="color: #0000ff">import</span><span style="color: #000000"> pymysql
  3. </span><span style="color: #008000">#</span><span style="color: #008000"> 连接database</span>
  4. conn = pymysql.connect(host=“你的数据库地址”, user=“用户名”,password=“密码”,database=“数据库名”,charset=<span style="color: #000000">“utf8”)
  5. </span><span style="color: #008000">#</span><span style="color: #008000"> 得到一个可以执行SQL语句的光标对象</span>
  6. cursor =<span style="color: #000000"> conn.cursor()
  7. 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">
  8. username </span>= <span style="color: #800000">"</span><span style="color: #800000">Alex</span><span style="color: #800000">"</span><span style="color: #000000">
  9. age </span>= 18
  10. <span style="color: #0000ff">try</span><span style="color: #000000">:
  11. </span><span style="color: #008000">#</span><span style="color: #008000"> 执行SQL语句</span>
  12. <span style="color: #000000"> cursor.execute(sql, [username, age])
  13. </span><span style="color: #008000">#</span><span style="color: #008000"> 提交事务</span>
  14. <span style="color: #000000"> conn.commit()
  15. </span><span style="color: #008000">#</span><span style="color: #008000"> 提交之后,获取刚插入的数据的ID</span>
  16. last_id =<span style="color: #000000"> cursor.lastrowid
  17. </span><span style="color: #0000ff">except</span><span style="color: #000000"> Exception as e:
  18. </span><span style="color: #008000">#</span><span style="color: #008000"> 有异常,回滚事务</span>
  19. <span style="color: #000000"> conn.rollback()
  20. cursor.close()
  21. conn.close()</span>

批量执行

  1. <span style="color: #008000">#</span><span style="color: #008000"> 导入pymysql模块</span>
  2. <span style="color: #0000ff">import</span><span style="color: #000000"> pymysql
  3. </span><span style="color: #008000">#</span><span style="color: #008000"> 连接database</span>
  4. conn = pymysql.connect(host=“你的数据库地址”, user=“用户名”,password=“密码”,database=“数据库名”,charset=<span style="color: #000000">“utf8”)
  5. </span><span style="color: #008000">#</span><span style="color: #008000"> 得到一个可以执行SQL语句的光标对象</span>
  6. cursor =<span style="color: #000000"> conn.cursor()
  7. 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">
  8. 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">)]
  9. </span><span style="color: #0000ff">try</span><span style="color: #000000">:
  10. </span><span style="color: #008000">#</span><span style="color: #008000"> 批量执行多条插入SQL语句</span>
  11. <span style="color: #000000"> cursor.executemany(sql, data)
  12. </span><span style="color: #008000">#</span><span style="color: #008000"> 提交事务</span>
  13. <span style="color: #000000"> conn.commit()
  14. </span><span style="color: #0000ff">except</span><span style="color: #000000"> Exception as e:
  15. </span><span style="color: #008000">#</span><span style="color: #008000"> 有异常,回滚事务</span>
  16. <span style="color: #000000"> conn.rollback()
  17. cursor.close()
  18. conn.close()</span>

  1. <span style="color: #008000">#</span><span style="color: #008000"> 导入pymysql模块</span>
  2. <span style="color: #0000ff">import</span><span style="color: #000000"> pymysql
  3. </span><span style="color: #008000">#</span><span style="color: #008000"> 连接database</span>
  4. conn = pymysql.connect(host=“你的数据库地址”, user=“用户名”,password=“密码”,database=“数据库名”,charset=<span style="color: #000000">“utf8”)
  5. </span><span style="color: #008000">#</span><span style="color: #008000"> 得到一个可以执行SQL语句的光标对象</span>
  6. cursor =<span style="color: #000000"> conn.cursor()
  7. sql </span>= <span style="color: #800000">"</span><span style="color: #800000">DELETE FROM USER1 WHERE id=%s;</span><span style="color: #800000">"</span>
  8. <span style="color: #0000ff">try</span><span style="color: #000000">:
  9. cursor.execute(sql, [</span>4<span style="color: #000000">])
  10. </span><span style="color: #008000">#</span><span style="color: #008000"> 提交事务</span>
  11. <span style="color: #000000"> conn.commit()
  12. </span><span style="color: #0000ff">except</span><span style="color: #000000"> Exception as e:
  13. </span><span style="color: #008000">#</span><span style="color: #008000"> 有异常,回滚事务</span>
  14. <span style="color: #000000"> conn.rollback()
  15. cursor.close()
  16. conn.close()</span>

  1. <span style="color: #008000">#</span><span style="color: #008000"> 导入pymysql模块</span>
  2. <span style="color: #0000ff">import</span><span style="color: #000000"> pymysql
  3. </span><span style="color: #008000">#</span><span style="color: #008000"> 连接database</span>
  4. conn = pymysql.connect(host=“你的数据库地址”, user=“用户名”,password=“密码”,database=“数据库名”,charset=<span style="color: #000000">“utf8”)
  5. </span><span style="color: #008000">#</span><span style="color: #008000"> 得到一个可以执行SQL语句的光标对象</span>
  6. cursor =<span style="color: #000000"> conn.cursor()
  7. </span><span style="color: #008000">#</span><span style="color: #008000"> 修改数据的SQL语句</span>
  8. 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">
  9. username </span>= <span style="color: #800000">"</span><span style="color: #800000">Alex</span><span style="color: #800000">"</span><span style="color: #000000">
  10. age </span>= 80
  11. <span style="color: #0000ff">try</span><span style="color: #000000">:
  12. </span><span style="color: #008000">#</span><span style="color: #008000"> 执行SQL语句</span>
  13. <span style="color: #000000"> cursor.execute(sql, [age, username])
  14. </span><span style="color: #008000">#</span><span style="color: #008000"> 提交事务</span>
  15. <span style="color: #000000"> conn.commit()
  16. </span><span style="color: #0000ff">except</span><span style="color: #000000"> Exception as e:
  17. </span><span style="color: #008000">#</span><span style="color: #008000"> 有异常,回滚事务</span>
  18. <span style="color: #000000"> conn.rollback()
  19. cursor.close()
  20. conn.close()</span>

查询单条数据

  1. <span style="color: #008000">#</span><span style="color: #008000"> 导入pymysql模块</span>
  2. <span style="color: #0000ff">import</span><span style="color: #000000"> pymysql
  3. </span><span style="color: #008000">#</span><span style="color: #008000"> 连接database</span>
  4. conn = pymysql.connect(host=“你的数据库地址”, user=“用户名”,password=“密码”,database=“数据库名”,charset=<span style="color: #000000">“utf8”)
  5. </span><span style="color: #008000">#</span><span style="color: #008000"> 得到一个可以执行SQL语句的光标对象</span>
  6. cursor =<span style="color: #000000"> conn.cursor()
  7. </span><span style="color: #008000">#</span><span style="color: #008000"> 查询数据的SQL语句</span>
  8. 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>
  9. <span style="color: #008000">#</span><span style="color: #008000"> 执行SQL语句</span>
  10. <span style="color: #000000">cursor.execute(sql)
  11. </span><span style="color: #008000">#</span><span style="color: #008000"> 获取单条查询数据</span>
  12. ret =<span style="color: #000000"> cursor.fetchone()
  13. cursor.close()
  14. conn.close()
  15. </span><span style="color: #008000">#</span><span style="color: #008000"> 打印下查询结果</span>
  16. <span style="color: #0000ff">print</span>(ret)

查询多条数据

  1. <span style="color: #008000">#</span><span style="color: #008000"> 导入pymysql模块</span>
  2. <span style="color: #0000ff">import</span><span style="color: #000000"> pymysql
  3. </span><span style="color: #008000">#</span><span style="color: #008000"> 连接database</span>
  4. conn = pymysql.connect(host=“你的数据库地址”, user=“用户名”,password=“密码”,database=“数据库名”,charset=<span style="color: #000000">“utf8”)
  5. </span><span style="color: #008000">#</span><span style="color: #008000"> 得到一个可以执行SQL语句的光标对象</span>
  6. cursor =<span style="color: #000000"> conn.cursor()
  7. </span><span style="color: #008000">#</span><span style="color: #008000"> 查询数据的SQL语句</span>
  8. sql = <span style="color: #800000">"</span><span style="color: #800000">SELECT id,name,age from USER1;</span><span style="color: #800000">"</span>
  9. <span style="color: #008000">#</span><span style="color: #008000"> 执行SQL语句</span>
  10. <span style="color: #000000">cursor.execute(sql)
  11. </span><span style="color: #008000">#</span><span style="color: #008000"> 获取多条查询数据</span>
  12. ret =<span style="color: #000000"> cursor.fetchall()
  13. cursor.close()
  14. conn.close()
  15. </span><span style="color: #008000">#</span><span style="color: #008000"> 打印下查询结果</span>
  16. <span style="color: #0000ff">print</span>(ret)

进阶用法

  1. <span style="color: #008000">#</span><span style="color: #008000"> 可以获取指定数量的数据</span>
  2. cursor.fetchmany(3<span style="color: #000000">)
  3. </span><span style="color: #008000">#</span><span style="color: #008000"> 光标按绝对位置移动1</span>
  4. cursor.scroll(1, mode=<span style="color: #800000">"</span><span style="color: #800000">absolute</span><span style="color: #800000">"</span><span style="color: #000000">)
  5. </span><span style="color: #008000">#</span><span style="color: #008000"> 光标按照相对位置(当前位置)移动1</span>
  6. 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   数据库名   

人气教程排行