PyMySQL连接MySQL数据库
首先,
添加PyMySQL模块:


代码:
import pymysql db = pymysql.connect(host="localhost",
user="root",
password="123456",
db="world",
port=3306,
charset="utf8")
# 打开数据库连接 cur = db.cursor()
# 使用cursor()方法获取操作游标 sql = "SELECT `ID`, `Name`, `CountryCode`, `District`, `Population` FROM city LIMIT 1;"
# sql语句 try:
cur.execute(sql)
# 执行sql语句
results = cur.fetchall()
# 获取查询的结果 for row in results:
# 遍历结果
ID = row[0]
Name = row[1]
CountryCode = row[2]
District = row[3]
Population = row[4]
print(ID, Name, CountryCode, District, Population)
except Exception as e:
raise e
finally:
db.commit()
# 提交
cur.close()
# 关闭游标
db.close()
# 断开数据库连接
PyMySQL连接MySQL数据库的更多相关文章
- Python3.x使用PyMysql连接MySQL数据库
Python3.x使用PyMysql连接MySQL数据库 由于Python3.x不向前兼容,导致Python2.x中的很多库在Python3.x中无法使用,例如Mysqldb,我前几天写了一篇博客Py ...
- Python3.x:使用PyMysql连接Mysql数据库
Python3.x:使用PyMysql连接Mysql数据库 Python3.x完全不向前兼容,导致Python2.x中可以正常使用的库,到了Python3就用不了: 比如说mysqldb,目前MySQ ...
- python3.4怎么连接mysql pymysql连接mysql数据库
本文介绍了python3 4连接mysql数据库的方法,在python3 4中使用原来python2 7的mysqldb已不能连接mysql数据库了,可以使用pymysql. 在python3.4 ...
- python3使用PyMysql连接mysql数据库
python语言的3 x完全不向前兼容,导致我们在python2 x中可以正常使用的库,到了python3就用不了了 比如说mysqldb目前MySQLdb并不支持python3 python语言的3 ...
- 使用pymysql连接MySql数据库
MySQLdb安装失败了,直接使用pymysql,安装了pymysql. 并学习了使用使用pymysql创建数据库和表,并插入数据. __author__ = 'Administrator' impo ...
- python pymysql 连接 mysql数据库进行操作
1.数据库的连接操作 import pymysql conn = pymysql.connect(host=', db='oldboydb') # host表示ip地址,user表示用户名,passw ...
- (转)pymysql 连接mysql数据库---不支持中文解决
往数据库里插入中文时出现异常:UnicodeEncodeError: 'latin-1' codec can't encode characters 就是编码的问题,pymysql默认的编码是lati ...
- python3.6 使用 pymysql 连接 Mysql 数据库及 简单的增删改查操作
1.通过 pip 安装 pymysql 进入 cmd 输入 pip install pymysql 回车等待安装完成: 安装完成后出现如图相关信息,表示安装成功. 2.测试连接 import ...
- Django2.2 pymysql 连接mysql数据库的坑
参考链接 https://www.52pojie.cn/thread-921141-1-1.html 1. mysqlclient 1.3版本不对 解决办法:注释掉D:\Users\xxx\AppDa ...
随机推荐
- linux-资料汇集
1.http://www.debian.org/doc/ 2.鸟哥的私房菜 3.The Linux Command Line by William E. Shotts, Jr. 4.https://d ...
- woff字体找不到导致的404错误
在iis中添加mime类型: .woff application/x-font-woff .woff2 application/x-font-woff
- Many-to-many relationships in EF Core 2.0 – Part 4: A more general abstraction
In the last few posts we saw how to hide use of the join entity from two entities with a many-to-man ...
- sharePoint中简单的父页面跳转子页面代码!
1,SharePoint中挺简单的一个父页面跳转到子页面的Js代码!常常用到,每次都到以前的项目中去找代码,挺麻烦! (1)父页面代码. function imgAddParentclick() { ...
- Knowledge Point 20180309 字符串常量池与String,intern()
引言 什么都先不说,先看下面这个引入的例子: public static void test4(){ String str1 = new String("SEU") + new S ...
- C#中小写人民币转大写
/// <summary> /// 转换成大写人民币 /// </summary> /// <param name="myMoney">< ...
- ArrayList详解
一.ArrayList类介绍:(这里给出jdk1.8源码上中文翻译) ArrayList是List接口以可变数组方式实现的,实现了所有的lis接口中的操作,并容许有null等所有元素.除了实现了Lis ...
- python初学者日记01(字符串操作方法)
时间:2018/12/16 作者:永远的码农(博客园) 环境: win10,pycharm2018,python3.7.1 1.1 基础操作(交互输入输出) input = input(" ...
- 前端模块化小总结—commonJs,AMD,CMD, ES6 的Module
随着前端快速发展,需要使用javascript处理越来越多的事情,不在局限页面的交互,项目的需求越来越多,更多的逻辑需要在前端完成,这时需要一种新的模式 --模块化编程 模块化的理解:模块化是一种处理 ...
- Change runlevel on CentOS 6.9/CentOS 7.5
1:CentOS 6.9 # 0 - halt (Do NOT set initdefault to this) # 1 - Single user mode # 2 - Multiuser, wit ...