创建数据库实例

import pymysql
db= pymysql.connect(host="localhost",user="root",password="1234",charset="utf8")
cursor=db.cursor()
cursor.execute("create database 数据库名 default charset=utf8;")
db= pymysql.connect(host="localhost",user="root",password="000000",charset="utf8",db="数据库名")
cursor=db.cursor()
cursor.execute('''
create table 表名(
SNO varchar(20),
SNAME char(5) ,
SAGE int(5),
SSEX char(5),
SACADEMY char(10),
SGRADE char(10),
SCLASS char(5),
primary key(SNO)
)default charset=utf8;
''')
cursor.execute('''
insert into 表名 values('2016081111','张三','20','男','软件工程学院','2016','3'),('2016061111','王杰','21','男','网络工程学院','2016','3'),
('2016071113','周顺','19','男','大气科学学院','2016','3'),('2017081180','李伟','20','男','软件工程学院','2017','2'),('2016081201','王丽','20','女','软件工程学院','2016','5');
db.commit()
cursor.execute('select * from 表名')
for i in cursor.fetchall():
print(i)
cursor.close()
db.close()

功能搭建

import pymysql
DB=None
class Student():
def select(self):
cursor=DB.cursor()
try:
SQL="select * from student"
cursor.execute(SQL)
for i in cursor.fetchall():
print(i)
except Exception as e:
print("报错",e)
finally:
cursor.close()
#查询
def Cha(self):
try:
cursor = DB.cursor()
a=input("请输入学号:")
cursor.execute("select * from student where SNO=%s"%a)
if cursor.fetchall():
SQL = "select * from student where SNO=%s"%a
cursor.execute(SQL)
for i in cursor.fetchall():
print(i)
else:
print("\n查询失败\n")
except Exception as e:
print("报错",e)
finally:
cursor.close()
#添加
def add(self):
cursor = DB.cursor()
a = input("输入添加的学号:")
b = input("输入添加的姓名:")
c = input("输入添加的年龄:")
d = input("输入添加的性别:")
e = input("输入添加的院系:")
f = input("输入添加的年级:")
g = input("输入添加的班级:")
try:
SQL = "insert into student values ('%s','%s','%s','%s','%s','%s','%s');" % (a, b, c, d, e, f, g)
cursor.execute(SQL)
DB.commit()
print("\n添加成功\n")
except Exception as e:
print("报错",e)
finally:
cursor.close()
#修改
def modify(self):
cursor = DB.cursor()
a = input("输入您要修改的学号:")
b = input("姓名修改为:")
c = input("年龄修改为:")
d = input("性别修改为:")
e = input("学院修改为:")
f = input("年级修改为:")
g = input("班级修改为:")
try:
SQL = "update student set SNAME='%s',SAGE='%s',SSEX='%s',SACADEMY='%s',SGRADE='%s',SCLASS='%s' where SNO='%s';" % (b,c,d,e,f,g,a)
cursor.execute(SQL)
DB.commit()
print("\n修改成功\n")
for i in cursor.fetchall():
print(i)
except Exception as e:
print("报错",e)
finally:
cursor.close()
#删除
def drop(self):
try:
cursor = DB.cursor()
a=input("输入您要删除的学生学号:")
cursor.execute("select * from student where SNO=%s"%a)
if cursor.fetchall():
SQL="delete from student where SNO=%s" % a
cursor.execute(SQL)
DB.commit()
print("\n删除成功\n")
for i in cursor.fetchall():
print(i)
else:
print("\n删除失败,可能没有该学号\n")
except Exception as e:
print("报错",e)
finally:
cursor.close() def main():
global DB
student=Student()
DB=pymysql.connect(host="localhost",user="root",password="1234",charset="utf8",db="zzz")
choose=input("请选择您的操作\n"
"1.查询所有学生的信息\n"
"2.按学号查询学生的信息\n"
"3.添加学生信息\n"
"4.修改学生信息\n"
"5.删除学生信息\n"
"6.退出\n")
while choose != "6":
if choose == "1":
student.select() elif choose == "2":
student.Cha() if choose == "3":
student.add() if choose == "4":
student.modify() if choose == "5":
student.drop() choose = input("请选择您的操作\n"
"1.查询所有学生的信息\n"
"2.按学号查询学生的信息\n"
"3.添加学生信息\n"
"4.修改学生信息\n"
"5.删除学生信息\n"
"6.退出\n")
else:
print("\n您已退出学生信息表\n") main()

Python之创建数据库及功能示例样本的更多相关文章

  1. Python MongoDB 创建数据库

    章节 Python MySQL 入门 Python MySQL 创建数据库 Python MySQL 创建表 Python MySQL 插入表 Python MySQL Select Python M ...

  2. Python MySQL 创建数据库

    章节 Python MySQL 入门 Python MySQL 创建数据库 Python MySQL 创建表 Python MySQL 插入表 Python MySQL Select Python M ...

  3. Python MongoDB 创建集合

    章节 Python MySQL 入门 Python MySQL 创建数据库 Python MySQL 创建表 Python MySQL 插入表 Python MySQL Select Python M ...

  4. Python MySQL 创建表

    章节 Python MySQL 入门 Python MySQL 创建数据库 Python MySQL 创建表 Python MySQL 插入表 Python MySQL Select Python M ...

  5. EntityFramework SQLiteCodeFirst 自动创建数据库 关闭级联删除

    外键的级联删除: 如A表中有主键idA, B表中设置外键(ForeignKey)为A表中的主键idA, 当A表中的记录被删除时, B表中所有引用此条记录的记录(即所有外键为idA的记录)将自动被删除 ...

  6. Python操作Access数据库

    我们在这篇文章中公分了五个步骤详细分析了Python操作Access数据库的相关方法,希望可以给又需要的朋友们带来一些帮助. AD: Python编 程语言的出现,带给开发人员非常大的好处.我们可以利 ...

  7. python操作Mysql数据库示例

    python库:pymysql 安装:install pymysql.mysql数据库 一.连接数据库.创建speder库.查询版本. import pymysql ##链接数据库 db = pymy ...

  8. Python访问MySQL数据库并实现其增删改查功能

    概述:对于访问MySQL数据库的操作,我想大家也都有一些了解.不过,因为最近在学习Python,以下就用Python来实现它.其中包括创建数据库和数据表.插入记录.删除记录.修改记录数据.查询数据.删 ...

  9. Python操作MySQL数据库完成简易的增删改查功能

    说明:该篇博客是博主一字一码编写的,实属不易,请尊重原创,谢谢大家! 目录 一丶项目介绍 二丶效果展示 三丶数据准备 四丶代码实现 五丶完整代码 一丶项目介绍 1.叙述 博主闲暇之余花了10个小时写的 ...

随机推荐

  1. python基础知识-day9(数据驱动)

    1.数据驱动的概念 在自动化测试中,需要把测试的数据分离到JSON,YAML等文件中. 2.YAML 的相关知识 YAML 入门教程 分类 编程技术 YAML 是 "YAML Ain't a ...

  2. Moriis神级遍历!

    Moriis 遍历 Morris 遍历是二叉树遍历的一种方式,传统的递归和非递归遍历的时间复杂的都是O(N),空间复杂度都是O(h)(h为树的高度),而 Morris 遍历可以做到时间复杂的依然为 O ...

  3. 搭建zabbix及报错处理

    搭建ZABBIX服务器准备工作 1.需要服务器是LAMP 或 LNMP 环境 2.主机名和IP要写在HOST文件里 3.iptables 和 selinux 必须关闭 一.先用最简单的方式搭建lamp ...

  4. Linux yum搭建私有仓库

    搭建yum仓库需要两种资源: rpm包 rpm包的元数据(repodata) 搭建好仓库后需要使用三种网络协议共享出来 http或https ftp 范例: 使用http协议搭建私有仓库 (本示例使用 ...

  5. 毕设之Python爬取天气数据及可视化分析

    写在前面的一些P话:(https://jq.qq.com/?_wv=1027&k=RFkfeU8j) 天气预报我们每天都会关注,我们可以根据未来的天气增减衣物.安排出行,每天的气温.风速风向. ...

  6. mariadb安装配置(主从配置)

    主服务器192.168.206.183 从服务器192.168.206.193 1.创建并编辑 /etc/yum.repos.d/MariaDB.repo文件(主从都要做) [mariadb] nam ...

  7. 常见的git命令和git->github错误

    相关命令 git remote git remote add origin xxx (xxx为仓库链接) 给这个链接取一个名字,为origin git pull git pull <远程主机名& ...

  8. Drone-比Jenkins更轻量化的持续集成部署工具

    Drone 简介 Drone 是一个基于Docker容器技术的可扩展的持续集成引擎,由GO语言编写,可用于自动化测试与构建,甚至发布.每个构建都在一个临时的Docker容器中执行,使开发人员能够完全控 ...

  9. JetBrains系列IDE创建文件模板

    #coding:utf-8 ''' @version: python3.6 @author: '$USER' @license: Apache Licence @contact: steinven@q ...

  10. 千万小心,99%的Java程序员会踩这些坑

    前言 作为Java程序员的你,不知道有没有踩过一些基础知识的坑. 有时候,某个bug查了半天,最后发现竟然是一个低级错误. 有时候,某些代码,这一批数据功能正常,但换了一批数据就出现异常了. 有时候, ...