博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python3.7和数据库MySQL 8.0.12 数据库数据驱动mysql-connector安装(四)
阅读量:5291 次
发布时间:2019-06-14

本文共 2405 字,大约阅读时间需要 8 分钟。

安装mysql-connector驱动 在系统CMD输入命令:

pip install mysql-connector

 示例:

 

创建数据库

# 导入驱动import mysql.connector# 创建连接数据库conn = mysql.connector.connect(host="localhost", user="root", passwd="root")mcursor = conn.cursor()mcreate_database_sql = "create database python_dbs"mcursor.execute(mcreate_database_sql)

创建表

 
# 导入驱动 import mysql.connector conn = mysql.connector.connect(host="localhost", user="root", passwd="root",database="python_dbs") mcursor = conn.cursor() mcreate_table_sql = "create table user (id int auto_increment primary key ,name varchar (20),password varchar (20))" mcursor.execute(mcreate_table_sql) conn.commit() mcursor.close() conn.close

插入一条数据

import mysql.connectorconn = mysql.connector.connect(host="localhost", user="root", passwd="root",database="python_dbs")mcursor = conn.cursor()#mcreate_table_sql = "create table user (id int auto_increment primary key ,name varchar (20),password varchar (20))"minstert_sql = "insert into user (name,password) values ('TOM','12345')"mcursor.execute(minstert_sql)conn.commit()mcursor.close()conn.close

插入多条数据

# 导入驱动import mysql.connectorconn = mysql.connector.connect(host="localhost", user="root", passwd="root",database="python_dbs")mcursor = conn.cursor()#mcreate_table_sql = "create table user (id int auto_increment primary key ,name varchar (20),password varchar (20))"#minstert_sql = "insert into user (name,password) values ('TOM','12345')"#插入多条数据 使用元组列表minstert_sqls = "insert into user (name,password) values (%s,%s)"var = [("LiLei","1111"),("HanMeiMei","2222"),("WangXxinXixn","3333")]mcursor.executemany(minstert_sqls,var)conn.commit()mcursor.close()conn.close

查询记录  模糊匹配

# 导入驱动 import mysql.connector conn = mysql.connector.connect(host="localhost", user="root", passwd="root",database="python_dbs") mcursor = conn.cursor() #查询所有的name msql = "select name from user" #查询name=TOM msql1 = "select * from user where name = 'TOM'" #查询name含有n的 msql2 = "select * from user where name like  '%n%'" #排序 默认升序 msql3 = "select * from user  order by name asc " #排序 降序 msql4 = "select * from user  order by name desc " #返回数量 msql5 = "select * from user  order by name desc  Limit 3" #删除 msql6 = "delete * from user where name = 'TOM'" mcursor.execute(msql5) #获取所有查询的记录 myresulet = mcursor.fetchall() conn.commit() mcursor.close() conn.close for x in myresulet:     print(x)

 

 只查询一条

mycursor.fetchone()

 

 

 

 

转载于:https://www.cnblogs.com/dangzhengtao/p/9579904.html

你可能感兴趣的文章
C# DataTable 详解
查看>>
2018icpc徐州OnlineA Hard to prepare
查看>>
扫描线矩形周长的并 POJ1177
查看>>
javascript数组
查看>>
spark_load csv to hive via hivecontext
查看>>
R语言-rnorm函数
查看>>
Spark的启动进程详解
查看>>
Java 字符终端上获取输入三种方式
查看>>
javascript 简单工厂
查看>>
oracle求时间差的常用函数
查看>>
java调用oracle存储过程,返回结果集
查看>>
使用命令创建数据库和表
查看>>
数据库的高级查询
查看>>
HttpClient(一)-- HelloWorld
查看>>
dump调试函数
查看>>
Android 利用Sharp样式设置文本框EditText圆角形状
查看>>
[YTU]_2443 ( C++习题 复数类--重载运算符3+)
查看>>
sdut_1189
查看>>
归并排序
查看>>
机器视觉:SSD Single Shot MultiBox Detector
查看>>