Python MySQL数据库基本操作及项目示例详解

这篇文章主要介绍了Python连接MySQL数据库后的一些基本操作,并以银行管理系统项目为例,为大家具体介绍了一下部分功能的实现,文中的示例代码具有一定的学习价值,感兴趣的可以了解一下,编程资料白嫖点击

目录

一、数据库基础用法

二、项目:银行管理系统

1、进行初始化操作

2、登录检查,并选择操作

3、加入查询功能

4、加入取钱功能

5、加入存钱功能


一、数据库基础用法

要先配置环境变量,然后cmd安装:pip install pymysql

1、连接MySQL,并创建wzg库

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

#引入decimal模块

import pymysql

#连接数据库

db=pymysql.connect(host='localhost',user='root',password='1234',charset='utf8')

#创建一个游标对象(相当于指针)

cursor=db.cursor()

#执行创建数据库语句

cursor.execute('create schema wzg default charset=utf8;')

cursor.execute('show databases;')

#fetchone获取一条数据(元组类型)

print(cursor.fetchone())

#现在指针到了[1]的位置

#fetchall获取全部数据(字符串类型)

all=cursor.fetchall()

for i in all:

    print(i[0])

#关闭游标和数据库连接

cursor.close()

db.close()

2、创建student表,并插入数据

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

import pymysql

#连接数据库,并打开wzg数据库(数据库已创建)

db=pymysql.connect(host='localhost',user='root',password='1234',charset='utf8',db='wzg')

#创建游标对象

cursor=db.cursor()

try:

    #创建student表,并执行

    sql='''create table student(

       SNO char(10),

       SNAME varchar(20) NOT NULL,

       SSEX varchar(1),

       primary key(SNO)

       )default charset=utf8;'''

    cursor.execute(sql)

     

    #插入一条数据,并执行

    insert_sql='''

    insert into student values('200303016','王智刚','男'),('20030001','小明','男')

    '''

    cursor.execute(insert_sql)

     

    #将数据提交给数据库(加入数据,修改数据要先提交)

    db.commit()

     

    #执行查询语句

    cursor.execute('select * from student')

     

    #打印全部数据

    all=cursor.fetchall()

    for i in all:

        print(i)

#发生错误时,打印报错原因

except Exception as e:

    print(e)

#无论是否报错都执行

finally:

    cursor.close()

    db.close()

数据库中char和varchar的区别:

char类型的长度是固定的,varchar的长度是可变的。

例如:存储字符串'abc',使用char(10),表示存储的字符将占10个字节(包括7个空字符),

使用varchar(10),表示只占3个字节,10是最大值,当存储的字符小于10时,按照实际的长度存储。

二、项目:银行管理系统

完成功能:1.查询 2.取钱 3.存钱 4.退出

练习:创建信息表,并进行匹配

1、创建数据库为(bank),账户信息表为(account)

account_id(varchar(20)) Account_passwd(char(6)) Money(decimal(10,2))
001 123456 1000.00
002 456789 5000.00

2、拓展:进行账号和密码的匹配

请输入账号:001

请输入密码:123456

1

2

3

4

5

select * from account where account_id=001 and Account_passwd=123456

if cursor.fetchall():

登录成功

else:

登录失败

</

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

import pymysql

# 连接数据库

db = pymysql.connect(host='localhost', user='root', password='1234', charset='utf8')

cursor = db.cursor()

# 创建bank库

cursor.execute('create database bank charset utf8;')

cursor.execute('use bank;')

try:

    # # 创建表

    # sql = '''create table account(

    #    account_id varchar(20) NOT NULL,

    #    account_passwd char(6) NOT NULL,

    #    money decimal(10,2),

    #    primary key(account_id)

    #    );'''

    # cursor.execute(sql)

    # # 插入数据

    # insert_sql = '''

    # insert into account values('001','123456',1000.00),('002','456789',5000.00)

    # '''

    # cursor.execute(insert_sql)

    # db.commit()

    # # 查询所有数据

    # cursor.execute('select * from account')

    # all = cursor.fetchall()

    # for i in all:

    #     print(i)

    # 输入账号和密码

    z=input("请输入账号:")

    m=input("请输入密码:")

     

    # 从account表中进行账号和密码的匹配

    cursor.execute('select * from account where account_id=%s and account_passwd=%s',(z,m))

     

    # 如果找到,则登录成功

    if cursor.fetchall():

        print('登录成功')

    else:

        print('登录失败')

except Exception as e:

    print(e)

finally:

    cursor.close()

    db.close()

  • 6
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值