1、python连接数据库:
- 1、不管是那一门编程语言都是不可以直接连接数据库的,需要有连接介质【连接池】
- 2、python的连接池有 pymysql 或者 mysqlclient
- 3、在cmd中导入库:pip install pymysql 或者 pip install mysqlclient (一般常用pymysql)
- 4、有时候需要通过管理员身份停之或者运行mysql库,如果停止了mysql库,那么用python代码就无法连接
- 5、通过python连接mysql的代码如下:
import pymysql
connect = None
try:
# 连接数据库(这里就是在模拟workbench中新建连接的过程)
connect = pymysql.Connection(
host = 'localhost', #数据库服务所在的ip地址
port = 3306, #数据库服务对应的端口号
user = 'root', #登录数据库服务所用的用户名
password = '123456789', #该用户对应的密码
database = 'db_exercise', #要连接的数据库名
charset = 'utf8mb4' #数据库对应的编码
)
print(connect)
except Exception as e:
print(e)
finally:
if connect != None:
connect.close()
连接成功打印的就是一个地址,否则就会返回错误!
2、通过python操作mysql代码如下【以检索数据为例】:
- 代码演示
import pymysql
from pymysql.cursors import DictCursor
connect = None
try:
# 连接数据库(这里就是在模拟workbench中新建连接的过程)
connect = pymysql.Connection(
host = '127.0.0.1', #数据库服务所在的ip地址
port = 3306, #数据库服务对应的端口号
user = 'root', #登录数据库服务所用的用户名
password = '123456', #该用户对应的密码
database = 'db_exercise', #要连接的数据库名
charset = 'utf8mb4' #数据库对应的编码
)
if connect:
# 获取操作数据库的游标【游标是用来执行sql语句的】
'''
1、可以设置游标的类型,不设置游标的话,默认获取的数据是元组格式
2、游标设置为DictCursor,返回数据为字典类型(如果不识别,alt+回车导入名称即可)
'''
cursor = connect.cursor(cursor=DictCursor)
# 这里用查询数据为例演示操作数据库
sql = "select * from tb_cc_log;"
try:
# 执行sql语句
cursor.execute(sql)
# 获取所有查询结果
all_data = cursor.fetchall()
print(all_data)
# # 获取第一条查询结果
# first_data = cursor.fetchone()
# print(first_data)
# # 获取前n条查询结果
# dates = cursor.fetchmany(2)
# print(dates)
except Exception as e:
print(e)
except Exception as e:
print(e)
finally:
if connect != None:
connect.close()
- 结果展示:
'''
[{'cc_id': 1, 'cc_sid': 1, 'cc_cid': 1001, 'cc_date': datetime.date(2021, 9, 1), 'cc_score': Decimal('99.0')},
{'cc_id': 2, 'cc_sid': 2, 'cc_cid': 1002, 'cc_date': datetime.date(2021, 9, 2), 'cc_score': Decimal('74.0')},
{'cc_id': 3, 'cc_sid': 3, 'cc_cid': 1003, 'cc_date': datetime.date(2021, 9, 3), 'cc_score': Decimal('84.0')},
{'cc_id': 4, 'cc_sid': 4, 'cc_cid': 1004, 'cc_date': datetime.date(2021, 9, 4), 'cc_score': Decimal('94.0')},
{'cc_id': 5, 'cc_sid': 1, 'cc_cid': 1002, 'cc_date': datetime.date(2021, 9, 1), 'cc_score': Decimal('55.0')},
{'cc_id': 6, 'cc_sid': 1, 'cc_cid': 1003, 'cc_date': datetime.date(2021, 9, 1), 'cc_score': Decimal('85.0')},
{'cc_id': 7, 'cc_sid': 1, 'cc_cid': 1004, 'cc_date': datetime.date(2021, 9, 1), 'cc_score': Decimal('75.0')},
{'cc_id': 8, 'cc_sid': 2, 'cc_cid': 1001, 'cc_date': datetime.date(2021, 9, 1), 'cc_score': Decimal('35.0')},
{'cc_id': 9, 'cc_sid': 2, 'cc_cid': 1003, 'cc_date': datetime.date(2021, 9, 1), 'cc_score': Decimal('90.0')},
{'cc_id': 10, 'cc_sid': 2, 'cc_cid': 1004, 'cc_date': datetime.date(2021, 9, 1), 'cc_score': Decimal('60.0')},
{'cc_id': 11, 'cc_sid': 3, 'cc_cid': 1001, 'cc_date': datetime.date(2021, 9, 1), 'cc_score': Decimal('55.0')},
{'cc_id': 12, 'cc_sid': 3, 'cc_cid': 1002, 'cc_date': datetime.date(2021, 9, 3), 'cc_score': Decimal('75.0')},
{'cc_id': 13, 'cc_sid': 3, 'cc_cid': 1004, 'cc_date': datetime.date(2021, 9, 3), 'cc_score': Decimal('95.0')},
{'cc_id': 14, 'cc_sid': 4, 'cc_cid': 1001, 'cc_date': datetime.date(2021, 9, 3), 'cc_score': Decimal('75.0')},
{'cc_id': 15, 'cc_sid': 4, 'cc_cid': 1002, 'cc_date': datetime.date(2021, 9, 3), 'cc_score': Decimal('85.0')},
{'cc_id': 16, 'cc_sid': 4, 'cc_cid': 1003, 'cc_date': datetime.date(2021, 9, 3), 'cc_score': Decimal('75.0')}]
'''
- 总结:在python中操作数据库,其中的sql语句在python中执行就相当于一个事务,只有提交才会生效,所以python操作数据库进行增加或者删除或者修改语句最后都要有一条提交指令(连接名.commit())。
3、SQL注入与防SQL注入:
- 介绍如下:
- 总结:SQL注入其实就是利用sql语句传入时候的漏洞,入侵数据库。