python3:连接mysql并操作 with PyMySql

使用的工具包:pymysql

安装:

  pip3 install PyMySQL

github 地址:https://github.com/PyMySQL/PyMySQL

Requirements

  • Python -- one of the following:
  • MySQL Server -- one of the following:

基础使用方法:

 1 # 与数据库建立连接
 2 db = pymysql.connect("localhost", "root", "edward1314", "wordcut")
 3 
 4 #建立执行游标,理解成console那个 '>'
 5 cursor = db.cursor()
 6 
 7 #利用cursor执行sql语句
 8 sql = """insert into word_cut(word,clean_word,cut_list) 
 9             VALUES (%s,%s,%s) 
10             ON DUPLICATE KEY UPDATE
11              cut_list = cut_list"""
12 value = ['a','b','c']
13 cursor.execute(sql, value)

待所有的操作执行完之后,记得要

#关闭链接
db.close
简单介绍到这。

猜你喜欢

转载自www.cnblogs.com/LukeJR/p/10813452.html