python管理数据库mysql

之前已经学习了mariadb的命令管理,现在做到用python管理数据库相关数据。

一、安装数据库

1、利用yum安装mariadb:
在这里插入图片描述
在这里插入图片描述
2.开启数据库服务:
服务未开启前利用mysql无法登陆:
在这里插入图片描述
开启服务后可以登陆:
在这里插入图片描述
3.数据库的安全初始化:
设置root密码:
在这里插入图片描述
移除匿名用户、移除test库:
在这里插入图片描述
在这里插入图片描述
4.以root身份登陆并创建数据库:
在这里插入图片描述
创建数据库westos:
在这里插入图片描述

二、python脚本管理数据库

1.创建数据表
在westos库中创建hello数据表:

import pymysql

# 1.连接数据库 host user passwd charset
conn = pymysql.connect(host='localhost',
                       user='root',
                       password='redhat',
                       db='westos',
                       charset='utf8'
)

# 2.创建游标对象
cur = conn.cursor()

# 3.对数据库进行操作
#1.)##################创建数据表########################
try:
    create_sqli = "create table hello (id int,name varchar(30));"
    print(cur.execute(create_sqli))
except Exception as e:
    print('创建数据表失败:',e)
else:
    print('创建数据表成功')

在这里插入图片描述
在这里插入图片描述
登陆数据库发现表格hello同步更新:
在这里插入图片描述
2.对数据库进行单条插入

import pymysql

# 1.连接数据库 host user passwd charset
conn = pymysql.connect(host='localhost',
                       user='root',
                       password='redhat',
                       db='westos',
                       charset='utf8',
                       autocommit='True'
)

# 2.创建游标对象
cur = conn.cursor()

# 3.对数据库进行操作
##########插入数据
# try:
#     insert_sqli = "insert into hello values(3,'apple');"
#     cur.execute(insert_sqli)
# except Exception as e:
#     print('插入数据失败:',e)
# else:
#     # 如果是插入数据,一定要提交数据 不然数据库中的数据表中找不到腰插入的数据
#     #conn.commit()
#     print('插入数据成功')

首先,创建新数据库redhat:
在这里插入图片描述
在这里插入图片描述
2.在redhat库中创建hello数据表:

import pymysql

# 1.连接数据库 host user passwd charset
conn = pymysql.connect(host='localhost',
                       user='root',
                       password='redhat',
                       db='redhat',
                       charset='utf8'
)

# 2.创建游标对象
cur = conn.cursor()

# 3.对数据库进行操作
#1.)##################创建数据表########################
try:
    create_sqli = "create table hello (id int,name varchar(30));"
    print(cur.execute(create_sqli))
except Exception as e:
    print('创建数据表失败:',e)
else:
    print('创建数据表成功')

在这里插入图片描述
在这里插入图片描述
可以看到数据表创建成功:
在这里插入图片描述

插入数据必须保存,否则查看不到数据
保存方法一:自动保存

import pymysql

# 1.连接数据库 host user passwd charset
conn = pymysql.connect(host='localhost',
                       user='root',
                       password='redhat',
                       db='redhat',
                       charset='utf8',
                       # autocommit='True'
)

# 2.创建游标对象
cur = conn.cursor()

# 3.对数据库进行操作
##########插入数据
try:
    insert_sqli = "insert into hello values(4,'haha');"
    cur.execute(insert_sqli)
except Exception as e:
    print('插入数据失败:',e)
else:
    # 如果是插入数据,一定要提交数据 不然数据库中的数据表中找不到腰插入的数据
    conn.commit()
    print('插入数据成功')

在这里插入图片描述

在这里插入图片描述
保存方法二:暂时保存
在这里插入图片描述

在这里插入图片描述
可以看到插入的两条数据同步成功:
在这里插入图片描述
3.对数据库进行多条插入

import pymysql

# 1.连接数据库 host user passwd charset
conn = pymysql.connect(host='localhost',
                       user='root',
                       password='redhat',
                       db='redhat',
                       charset='utf8',
                       # autocommit='True'
)

# 2.创建游标对象
cur = conn.cursor()
#####插入多条数据
# try:
#     info = [(i,i) for i in range(100,1000)]
#     ##第一种方式
#     # insert_sqli = "insert into hello values(%d,'%s');"
#     # for item in info:
#     #     print('insert语句:',insert_sqli %item)
#     #     cur.execute(insert_sqli %item)
#     insert_sqli = "insert into hello values('%s','%s');"
#     cur.executemany(insert_sqli,info)
# except Exception as e:
#     print('插入多条数据失败:',e)
# else:
#     print('插入多条数据成功')

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

可以看到数据同步:
在这里插入图片描述
在这里插入图片描述

三、数据库的查询
import pymysql

# 1.连接数据库 host user passwd charset
conn = pymysql.connect(host='localhost',
                       user='root',
                       password='redhat',
                       db='westos',
                       charset='utf8',
                       autocommit='True'
)

# 2.创建游标对象
cur = conn.cursor()

# 3.对数据库进行操作
######查询数据库
# sqli = 'select name from hello where id =100'
# 默认不返回查询结果集 返回数据记录数
# result = cur.execute(sqli)
# print(result)
# cur.execute(sqli)
# a = cur.fetchone()
# print(a)
# print(cur.fetchone()) #获取下一个查询结果集
# print(cur.fetchone())

# print(cur.fetchmany(4)) #获取指定个数查询结果集

# info = cur.fetchall()
# print(info)

效果如图:
在这里插入图片描述

在这里插入图片描述

四、移动游标指针
import pymysql

# 1.连接数据库 host user passwd charset
conn = pymysql.connect(host='localhost',
                       user='root',
                       password='redhat',
                       db='westos',
                       charset='utf8',
                       autocommit='True'
)

# 2.创建游标对象
cur = conn.cursor()
# 移动游标指针
sqli = 'select * from hello;'
cur.execute(sqli)
# print(cur.fetchmany(3))
# print('正在移动指针到最开始的地方....')
# cur.scroll(0,'absolute')
# print(cur.fetchmany(3))
# cur.scroll(0,'absolute')
# print(cur.fetchmany(2))
# print(cur.fetchall()) #返
"""
# 可以通过cursor.scroll(position, mode="relative | absolute")方法,
# 来设置相对位置游标和绝对位置游标
# # 当mode='absolute'时,代表绝对移动,
# # value就代表移动的绝对位置,value=0就代表移动到位置0处,
# # 就是结果集开头,
# # value=3就是移动到位置3处,也就是第4条记录处

mode缺省值为'relative',代表相对移
# 当mode='relative'时,value就是移动的长度,
# value>0向后移动(从位置0移动到位置2),
# value<0向前移动(比如从位置2移动到位置0)
"""

# 回游标之后的所有结果
print(cur.fetchmany(3))
cur.scroll(-2,mode='relative')
print(cur.fetchmany(2))

在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值