Windows下 Python Mysql数据库安装及简单使用

原博文

一、软件安装

安装环境:Windows 10 

Python版本:anaconda5.1 自带Python 3.6.4版本 下载地址:https://www.anaconda.com/download/

Mysql 版本为 5.7.21

Mysql安装可参考:找的链接,可自行百度相关安装方法基本大同小异   https://www.cnblogs.com/chengxs/p/5986095.html


Mysql 装好后安装pymysql:可以用conda 命令安装 conda installl pymysql   

也可用pip 命令来装:pip installl pymysql 安装好后可用 pip show pymysql 来检测是否安装好




二、数据库简单操作:

Python和数据库安装好后即可进行简单数据库操作

2.1创建数据表:

import pymysql
#连接数据库
db = pymysql.connect(host='localhost',user='root',password='root',db='test',port=3306)
#获取cursor
cursor = db.cursor()
# 使用 execute() 方法执行 SQL,如果表存在则删除  
cursor.execute("DROP TABLE IF EXISTS user")  
# 使用预处理语句创建表  
sql = """CREATE TABLE user ( 
         id INT(11) auto_increment primary key NOT NULL,
         name  CHAR(20) NOT NULL,
         password INT(12),
         age INT(3) )"""  
cursor.execute(sql)  
print("CREATE TABLE OK")  
# 关闭数据库连接  

db.close()  

不管你使用的是什么工具或库,连接数据库这一步必不可少。host为数据库的主机IP地址,user为数据的用户名,password为数据库的登录密码,db为数据库的名称,portMySQL的默认端口号。

cursor()方法创建数据库游标。

execute()方法执行SQL语句。

commit()将数据库的操作真正的提交到数据。


2.2 插入数据:

import pymysql
#连接数据库
db = pymysql.connect(host='localhost',user='root',password='root',db='test',port=3306)
cursor = db.cursor()
#插入数据 表内相应数据
sql_insert = """insert into user (name,password,age) values('lisi',111111,19)"""
try:
    cursor.execute(sql_insert)
    #提交事务
    db.commit()
    print("insert ok")
except Exception as e:
    #如果异常则回滚事务
    db.rollback()
    raise e #可做自己想做的事
finally:
    db.close()

2.3 查询数据库:

import pymysql
#连接数据库
db = pymysql.connect(host='localhost',user='root',password='root',db='test',port=3306)
cursor = db.cursor()
#查询 query
sql_query = "select * from user"
try :
    cursor.execute(sql_query)
    results = cursor.fetchall()
    print(results)
    for row in results:
        id = row[0]
        name = row[1]
        password = row[2]
        age = row[3]
        print('id = %d ,name = %s, password = %d, age = %d'%(id,name,password,age))
except Exception as e:
   #可记录log
    raise e
finally:

    db.close()

2.4 修改更新数据:

import pymysql
#连接数据库
db = pymysql.connect(host='localhost',user='root',password='root',db='test',port=3306)
cursor = db.cursor()
#update 更新数据
sql_update = "update user set age = 30 where name = 'zhang'"
try:
    cursor.execute(sql_update)
    #提交事务
    db.commit()
except Exception as e:
    #异常则回滚
    db.rollback()
    raise e
finally:

    db.close()

2.5 删除表内一行数据:

import pymysql
#连接数据库
db = pymysql.connect(host='localhost',user='root',password='root',db='test',port=3306)
cursor = db.cursor()
#delete 删除一行数据
sql_delete = "delete from user where id = 1"
try:
    cursor.execute(sql_delete)
    db.commit()
    #再次查询
    cursor.execute("select * from user")
    results = cursor.fetchall()
    for user in results:
        id = user[0]
        name = user[1]
        password = user[2]
        age = user[3]
        print('id = %d ,name = %s, password = %d, age = %d'%(id,name,password,age)) 
except Exception as e:
    #y异常则回滚
    db.rollback()
    raise e
finally:
    db.close()

猜你喜欢

转载自blog.csdn.net/u011511601/article/details/79899111