温欣爸比

  • 主页
  • Alfred Workflow
  • 《Vim 练级手册》
  • 常用命令
  • 代码笔记
  • 合辑
  • 在线工具
所有文章 友链 关于我

温欣爸比

  • 主页
  • Alfred Workflow
  • 《Vim 练级手册》
  • 常用命令
  • 代码笔记
  • 合辑
  • 在线工具

Python 操作 Mysql

2017-12-13

在 Python 中有很多可以操作 Mysql 的模块,在这里简单介绍下 pymysql 模块

pymysql

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: wxnacy(wxnacy@gmail.com)

import pymysql
from urllib.parse import urlparse

url = 'mysql+pymysql://root:password@localhost:3306/db?charset=utf8mb4'
URL_CONFIG = urlparse(url)

class BaseDB(object):
@classmethod
def create_conn(cls):
'''创建mysql链接'''
return pymysql.connect(
host=URL_CONFIG.hostname,
port=URL_CONFIG.port,
user=URL_CONFIG.username,
password=URL_CONFIG.password,
db=URL_CONFIG.path[1:],
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor
)

@classmethod
def query(cls, sql, params):
"""
查询操作
:param sql:
:param params:
:return:
"""
conn = cls.create_conn()
try:
cursor = conn.cursor() # 获取操作游标

cursor.execute(sql, params) # 执行 sql 语句
conn.commit() # 提交
result = cursor.fetchall() # 查询语句获取所以数据
cursor.close() # 关闭游标
return result
except BaseException as e:
return []
finally:
conn.close() # 关闭连接

@classmethod
def execute(cls, sql, params):
"""
更新操作
:param sql:
:param params:
:return:
"""
conn = cls.create_conn()
try:
cursor = conn.cursor()

result = cursor.execute(sql, params)
conn.commit()
cursor.close()
return result
except BaseException as e:
return False
finally:
conn.close()


if __name__ == "__main__":
db = BaseDB()
# 需要传递的参数使用 %s 替换
sql = "insert into test_user(id, name) values (%s, %s);"
# 参数放在数组中
db.execute(sql, [1, 'wxnacy'])
sql = "select * from test_user;"
res = db.query(sql, [])
print(res) # ==> [{"id":1, "name": "wxnacy"}]
最近更新
Alfred Workflow 命令行帮助工具
最近热读
Go 判断数组中是否包含某个 item
Vim 高级功能 vimgrep 全局搜索文件
办理北京工作居住证的一些细节
Go 语法错误:Non-declaration statement outside function body
Mac 电脑查看字体文件位置
扫码关注公众号,或搜索公众号“温欣爸比” 及时获取我的最新文章
赏

谢谢你请我喝咖啡

支付宝
微信
  • python
  • mysql
UglifyJS2 压缩 Javascript 代码
Hexo 如何引入自定义 js 文件
  1. 1. pymysql
© 2017 - 2022 温欣爸比 京ICP备15062634号 总访问量1361498次 访客数1119283人次 本文总阅读量398次
Hexo Theme Yilia by Litten
  • 所有文章
  • 友链
  • 关于我

tag:

  • python
  • flask
  • javascript
  • docker
  • 工具
  • openresty
  • 微信
  • java
  • hexo
  • 杂谈
  • vim
  • git
  • mysql
  • http
  • linux
  • mac
  • tmux
  • ssh
  • 算法
  • 开发
  • node
  • 杂文
  • jinja2
  • maven
  • spring
  • 北京
  • 生活
  • springboot
  • react
  • shell
  • graphql
  • iterm
  • expect
  • nginx
  • sqlalchemy
  • html
  • electron
  • vagrant
  • elastic
  • 宝贝
  • ansible
  • css
  • jquery
  • go
  • markdown
  • awk
  • redis
  • leetcode
  • zsh
  • 漫威
  • ssr
  • android
  • ffmpeg
  • chrome
  • vmware
  • youtube
  • windows
  • jupyter
  • excel
  • jq
  • Mac
  • Homebrew
  • mongo
  • py2
  • HomeBrew
  • movie
  • nodejs

    缺失模块。
    1、请确保node版本大于6.2
    2、在博客根目录(注意不是yilia根目录)执行以下命令:
    npm i hexo-generator-json-content --save

    3、在根目录_config.yml里添加配置:

      jsonContent:
        meta: false
        pages: false
        posts:
          title: true
          date: true
          path: true
          text: false
          raw: false
          content: false
          slug: false
          updated: false
          comments: false
          link: false
          permalink: false
          excerpt: false
          categories: false
          tags: true
    

  • Guru99
每天看书
每天背单词
每天一篇
写写代码
听听周杰伦
爱爱老婆