Python之pymysql库

本篇仅作为代码记录

#!/usr/bin/python
# -*- coding: UTF-8 -*-    
# Author: RuiMing Lin
# DateTime: 2021/01/25 14:51
# Description:

import pymysql

# 查看数据库版本
flag = False
# flag = True
if flag:
    db = pymysql.connect(host='localhost', user='root', password='', port=3306)
    cursor = db.cursor()  # 获得 MySQL 的操作游标
    cursor.execute('SELECT VERSION()')  # 获得第一条数据,即MySQL版本号
    data = cursor.fetchone()
    print('Database version:', data)
    cursor.execute("CREATE DATABASE spiders DEFAULT CHARACTER SET utf8")
    db.close()

# 创建数据库
flag = False
# flag = True
if flag:
    db = pymysql.connect(host='localhost', user='root', password='', port=3306)
    cursor = db.cursor()  # 获得 MySQL 的操作游标
    cursor.execute("CREATE DATABASE IF NOT EXISTS spiders DEFAULT CHARACTER SET utf8")
    db.close()

# 创建表
flag = False
# flag = True
if flag:
    db = pymysql.connect(host='localhost', user='root', password='', port=3306, db='spiders')
    cursor = db.cursor()
    sql = 'CREATE TABLE IF NOT EXISTS students (id VARCHAR(255) NOT NULL, name VARCHAR(255) NOT NULL, age INT NOT NULL, PRIMARY KEY (id))'
    cursor.execute(sql)
    db.close()

# 插入数据
flag = False
# flag = True
if flag:
    id = '20120001'
    user = 'Bob'
    age = 20
    db = pymysql.connect(host='localhost', user='root', password='', port=3306, db='spiders')
    cursor = db.cursor()
    sql = 'INSERT INTO students(id, name, age) values(%s, %s, %s)'
    # sql = 'INSERT INTO students(id, name, age) values(' + id + ', ' + name + ', ' + age + ')'
    try:
        cursor.execute(sql, (id, user, age))
        db.commit()
    except:
        db.rollback()  # 事务回滚
        print("插入数据失败")
    db.close()

# 更新数据
flag = False
flag = True
if flag:
    db = pymysql.connect(host='localhost', user='root', password='', port=3306, db='spiders')
    cursor = db.cursor()
    sql = 'UPDATE students SET age = %s WHERE name = %s'
    try:
        cursor.execute(sql, (25, 'Bob'))
        db.commit()
    except:
        db.rollback()
    db.close()

# 删除数据
flag = False
# flag = True
if flag:
    db = pymysql.connect(host='localhost', user='root', password='', port=3306, db='spiders')
    cursor = db.cursor()
    table = 'students'
    condition = 'age > 20'
    sql = 'DELETE FROM  {table} WHERE {condition}'.format(table=table, condition=condition)
    try:
        cursor.execute(sql)
        db.commit()
    except:
        db.rollback()
    db.close()

# 查询数据
flag = False
flag = True
if flag:
    db = pymysql.connect(host='localhost', user='root', password='', port=3306, db='spiders')
    cursor = db.cursor()
    sql = 'SELECT * FROM students WHERE age >= 20'
    try:
        cursor.execute(sql)
        print('Count:', cursor.rowcount)
        one = cursor.fetchone()
        print('One:', one)
        results = cursor.fetchall()
        print('Results:', results)
        print('Results Type:', type(results))
        for row in results:
            print(row)
    except:
        print('Error')

猜你喜欢

转载自blog.csdn.net/Orange_minger/article/details/113476025
今日推荐