pymysql

Python学习 Day16 Python3 MySQL 数据库

▼魔方 西西 提交于 2020-03-16 18:36:15
Python3 MySQL 数据库 Python3 使用 PyMySQL 连接数据库,并实现简单的增删改查。 什么是 PyMySQL? PyMySQL 是在 Python3.x 版本中用于连接 MySQL 服务器的一个库,Python2中则使用mysqldb。 PyMySQL 遵循 Python 数据库 API v2.0 规范,并包含了 pure-Python MySQL 客户端库。 PyMySQL 安装 在使用 PyMySQL 之前,我们需要确保 PyMySQL 已安装。 PyMySQL 下载地址:https://github.com/PyMySQL/PyMySQL。 如果还未安装,我们可以使用以下命令安装最新版的 PyMySQL: $ pip install PyMySQL 如果你的系统不支持 pip 命令,可以使用以下方式安装: 1、使用 git 命令下载安装包安装(你也可以手动下载): $ git clone https://github.com/PyMySQL/PyMySQL $ cd PyMySQL/ $ python3 setup.py install 2、如果需要制定版本号,可以使用 curl 命令来安装: $ # X.X 为 PyMySQL 的版本号 $ curl -L https://github.com/PyMySQL/PyMySQL/tarball

Python3 MySQL 数据库连接

江枫思渺然 提交于 2020-03-16 18:35:51
PyMySQL 安装 在使用 PyMySQL 之前,我们需要确保 PyMySQL 已安装。 PyMySQL 下载地址:https://github.com/PyMySQL/PyMySQL。 如果还未安装,我们可以使用以下命令安装最新版的 PyMySQL: pip install PyMySQ 数据库连接 连接数据库前,请先确认以下事项: 您已经创建了数据库 TESTDB. 在TESTDB数据库中您已经创建了表 EMPLOYEE EMPLOYEE表字段为 FIRST_NAME, LAST_NAME, AGE, SEX 和 INCOME。 连接数据库TESTDB使用的用户名为 "testuser" ,密码为 "test123",你可以可以自己设定或者直接使用root用户名及其密码,Mysql数据库用户授权请使用Grant命令。 在你的机子上已经安装了 Python MySQLdb 模块。 实例: 以下实例链接Mysql的TESTDB数据库: #!/usr/bin/python3 import pymysql # 打开数据库连接 db = pymysql.connect("localhost","testuser","test123","TESTDB" ) # 使用 cursor() 方法创建一个游标对象 cursor cursor = db.cursor() # 使用 execute(

Python中pymysql基本使用

北战南征 提交于 2020-03-14 03:51:00
Python中pymysql模块通过获取mysql数据库命令行游标执行数据库命令来进行数据库操作   优点:操作数据库语句所见即所得,执行了什么数据库语句都很清楚   缺点:操作繁琐,代码量多 1. pymysql的基本使用 # -*- coding:utf-8 -*- # Author:Wong Du import pymysql # 创建链接,相当于建立一个socket conn = pymysql.Connection(host='10.0.0.100', port=3306, user='root', passwd='123456', db='testdb') # 建立游标,相当于进入 mysql> 命令操作界面 cursor = conn.cursor() # 建表,和mysql命令行操作一样 try: create_table = cursor.execute('''create table student( id int not null primary key auto_increment, name char(32) not null, register_date date not null DEFAULT "2018-05-09" ); ''') except pymysql.err.InternalError as e: # print(type(e))

python连接mysql之pymysql模块

孤者浪人 提交于 2020-03-13 19:25:32
以下demo均以python2中的mysqldb模块 一、插入数据 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 import MySQLdb conn = MySQLdb.connect(host = '127.0.0.1' ,user = 'root' ,passwd = '1234' ,db = 'mydb' ) cur = conn.cursor() reCount = cur.execute( 'insert into UserInfo(Name,Address) values(%s,%s)' ,( 'alex' , 'usa' )) # reCount = cur.execute('insert into UserInfo(Name,Address) values(%(id)s, %(name)s)',{'id':12345,'name':'wupeiqi'}) conn.commit() cur.close() conn.close() print reCount import MySQLdb conn = MySQLdb.connect(host='127.0.0.1',user='root',passwd='1234',db='mydb') cur = conn.cursor() li =[ ('alex','usa'), ('sb'

Python mariadb

孤街浪徒 提交于 2020-03-12 05:02:26
文章目录 Python mariadb 一、python mariadb使用实例 1.连接数据库如下 2.插入数据 3.查询数据 二、实现增删改查 1.输入数据一组并插入查询 2.修改数据 3.删除数据 Python mariadb 一、python mariadb使用实例 先安装PyMySQL库 pip install PyMySQL 1.连接数据库如下 import pymysql # 连接配置信息 conn = pymysql . connect ( host = '127.0.0.1' , port = 3306 , user = 'root' , password = '123' , db = 'hghtest' , charset = 'utf8mb4' , cursorclass = pymysql . cursors . DictCursor ) # 创建连接 cur = conn . cursor ( ) cur . execute ( "SELECT id1,amount1 FROM t1" ) for r in cur : print ( r ) cur . close ( ) conn . close ( ) 数据库中的数据 2.插入数据 import pymysql # 连接配置信息 config = { 'host' : '127.0.0.1' ,

python连接数据库SQL的基本方法

ぃ、小莉子 提交于 2020-03-11 11:51:25
#2020年3月11日 #Elizabeth import pymysql # 创建数据库链接 conn = pymysql.connect( host='localhost', port=3306, user='root', passwd='******', db='resumes', charset='utf8', ) # 创建游标 cursor = conn.cursor() # 关闭游标 cursor.close() # 关闭连接 conn.close() 来源: 51CTO 作者: wx5d72071a58c07 链接: https://blog.51cto.com/14534896/2477220

Python 获取MySql某个表所有字段名

无人久伴 提交于 2020-03-10 20:56:00
在使用python导出数据库中数据的时候,往往除了插入的数据以外,还有表字段等信息需要导出,查阅了资料后发现了2种方法 第一种:在mysql自带的表里查询,这个表保存了每张表的字段信息,可以用pymysql执行下面的sql语句 import pymysql conn = pymysql.connect(host="127.0.0.1",user="root",password="123456",db="study",autocommit=True) cur = conn.cursor() sql = "select COLUMN_NAME from information_schema.COLUMNS where table_name = 'userinfo'" cur.execute(sql) for field in cur.fetchall(): print(field[0]) cur.close() conn.close() 第二种:使用pymysql自带的方法获取 import pymysql conn = pymysql.connect(host="127.0.0.1",user="root",password="123456",db="study",autocommit=True) cur = conn.cursor() sql = "select * from

Python-pymysql

半世苍凉 提交于 2020-03-07 10:28:35
一 安装及导入   1. pip3 install pymysql   2. 安装完需要把包的路径加到Pycharm的路径中 二 执行SQL语句 import pymysql # create connection connection = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='', db='test', charset="utf8") # create cursor cursor = connection.cursor() # execute statement cursor.execute('insert into tb1(nid,name) values(3, "Howard")') #cursor.execute('insert into tb1(nid,name) values(4, %s), input') 可以进行字符串拼接传参数,不能通过字符串拼接后传递变量的形式,不安全#execute many statementsvalue = ( (5,'Paul'), (6, 'Wade') )cursor.executemany('insert into tb1(nid,name) values(%s,%s), value) # commit statemnet

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

依然范特西╮ 提交于 2020-03-07 08:23:59
Python连接MySQL数据库之pymysql模块使用 Python3连接MySQL 本文介绍Python3连接MySQL的第三方库--PyMySQL的基本使用。 PyMySQL介绍 PyMySQL 是在 Python3.x 版本中用于连接 MySQL 服务器的一个库,Python2中则使用mysqldb。 Django中也可以使用PyMySQL连接MySQL数据库。 PyMySQL安装 pip install pymysql 连接数据库 注意事项 在进行本文以下内容之前需要注意: 你有一个MySQL数据库,并且已经启动。 你有可以连接该数据库的用户名和密码 你有一个有权限操作的database 基本使用 # 导入pymysql模块 import pymysql # 连接database conn = 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