准备工作:

1.在本地虚拟机172.16.0.115上安装mysql,并设置权限如下

mysql>  grant all privileges on *.* to root@"%" Identified by "Mypassword4!";
Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)

2.在pycharm所在ubuntu虚拟机上验证是否可以访问172.16.0.115上的mysql

hanli@ubuntu:~$ mysql -h 172.16.0.115 -u root -pMypassword4!`

3.代码如下:

import mysql.connector

# 连接到数据库:
conn = mysql.connector.connect(host='172.16.0.115',
user='root', password='Mypassword4!', database='test')
# 连接到数据库后,需要打开游标,称之为Cursor,通过Cursor执行SQL语句,然后,获得执行结果
cursor = conn.cursor()
# 创建user表:
cursor.execute('create table user (id varchar(20) primary key, name varchar(20))')
# 插入一行记录,注意MySQL的占位符是%s:
cursor.execute('insert into user (id, name) values (%s, %s)', ('1', 'Michael'))
print('rowcount =', cursor.rowcount)
# 提交事务:
conn.commit()
cursor.close() # 运行查询:
cursor = conn.cursor()
cursor.execute('select * from user where id = %s', ('1',))
values = cursor.fetchall()
print(values)
# 关闭Cursor和Connection:
cursor.close()
conn.close()

执行结果:

rowcount = 1
[('1', 'Michael')]

python3 连接mysql数据库的更多相关文章

  1. Python3连接MySQL数据库实战

    Python3连接MySQL数据库实战 第三方库 :pymysql 数据库连接 def connect(): try: #建立数据库连接,从左至右参数依次为 # ip地址 我用的是云端数据库 如果为本 ...

  2. python3连接MySQL数据库实例

    #python3连接MySQL实例 import pymysql """导入连接MySQL需要的包,没有安装pymysql需要先安装 使用命令行切换到python的安装路 ...

  3. 【python】python3连接mysql数据库

    一.安装pymysql 详见http://www.runoob.com/python3/python3-mysql.html 二.连接mysql数据库 db = pymysql.connect( #连 ...

  4. PYTHON3连接MYSQL数据库

    http://smilejay.com/2013/03/python3-mysql-connector/ Python 2.x 上连接MySQL的库倒是不少的,其中比较著名就是MySQLdb(Djan ...

  5. linux下python3连接mysql数据库

    python语言的3.x完全不向前兼容,导致我们在python2.x中可以正常使用的库,到了python3就用不了了.比如说mysqldb 1.安装pymysql pymysql就是作为python3 ...

  6. python3.4怎么连接mysql pymysql连接mysql数据库

    本文介绍了python3 4连接mysql数据库的方法,在python3 4中使用原来python2 7的mysqldb已不能连接mysql数据库了,可以使用pymysql.   在python3.4 ...

  7. Python3.x使用PyMysql连接MySQL数据库

    Python3.x使用PyMysql连接MySQL数据库 由于Python3.x不向前兼容,导致Python2.x中的很多库在Python3.x中无法使用,例如Mysqldb,我前几天写了一篇博客Py ...

  8. Python3.x:使用PyMysql连接Mysql数据库

    Python3.x:使用PyMysql连接Mysql数据库 Python3.x完全不向前兼容,导致Python2.x中可以正常使用的库,到了Python3就用不了: 比如说mysqldb,目前MySQ ...

  9. Python3:Django连接Mysql数据库时出错,'Did you install mysqlclient or MySQL-python?'

    Python3:Django连接Mysql数据库时出错,'Did you install mysqlclient or MySQL-python?' 一.原因 因为Python版本问题,MySQLdb ...

随机推荐

  1. 【九度OJ】题目1435:迷瘴 解题报告

    [九度OJ]题目1435:迷瘴 解题报告 标签(空格分隔): 九度OJ http://ac.jobdu.com/problem.php?pid=1435 题目描述: 通过悬崖的yifenfei,又面临 ...

  2. 【LeetCode】56. Merge Intervals 解题报告(Python & C++ & Java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  3. 【LeetCode】825. Friends Of Appropriate Ages 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/friends-o ...

  4. 【LeetCode】63. Unique Paths II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/unique-pa ...

  5. 【LeetCode】880. Decoded String at Index 解题报告(Python)

    [LeetCode]880. Decoded String at Index 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...

  6. C. Andryusha and Colored Balloons

    C. Andryusha and Colored Balloons time limit per test 2 seconds memory limit per test 256 megabytes ...

  7. 1131 - Just Two Functions

    1131 - Just Two Functions    PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 ...

  8. C语言string操作

    创建方式 字符数组:空间已定 字符指针:未分配空间 初始化 字符数组: 创建与赋值必须在同一行 指定大小:未填满部分用'\0'填充 用字符串初始化:末尾自动添加'\0' 不初始化赋值则乱值 字符指针: ...

  9. ComfortTypingPro快速录入工具

    1.简介 Comfort Typing Pro 官方中文注册版是一款非常方便的键盘快速录入辅助工具, 也可以叫做键盘宏工具吧. 程序的主要功能是可以帮助你快速的输入大量需要重复输入的内容, 支持文本和 ...

  10. BUG—Nuget包版本不一致导致程序行为与预期不符

    注:本文收录于<Bug集锦>,请点击此处查看全文目录 BUG起因 先介绍一下背景: 数周前的一个极其平常的下午,完成了本次迭代的开发工作,发布到QA提测,然后开始摸鱼.没几分钟,测试就来找 ...