zhangguosheng1121

一、Navicat使用

下载与安装:

链接:https://pan.baidu.com/s/1nL8lAL8lFtEXNg7Qi_Hs_w
提取码:s2e7

 

1、测试+链接数据库,新建库

2、新建表,新增字段+类型+约束

3、设计表:外键

4、新建查询 

5、建立表模型

 

二、python操作数据库

pymysql使用步骤:

①与数据库服务器建立链接

②获取游标对象(用于发送和接收数据)

③用游标执行sql语句

④使用fetch方法来获取执行的结果

⑤关闭链接:先关游标再关链接

import pymysql

# 创建一个链接得到一个链接对象
conn = pymysql.connect(
    host=\'127.0.0.1\',
    port=3306,
    user=\'root\',
    password=\'3822515\',
    database=\'day41\',
    charset=\'utf8\'
)

# 获取游标对象 pymysql.cursors.DictCursor返回的结果为字典,默认是元组类型
cursor = conn.cursor(pymysql.cursors.DictCursor)

# 执行sql, 如果是select 语句返回的是查询的条数
res = cursor.execute(\'select * from class\')
print(res)

# 获取查询的结果
# print(cursor.fetchall())      # 获取所有查询到的数据
print(cursor.fetchone())        # 一个一个的查# cursor.scroll(1, \'absolute\')  # 绝对移动,参照开始位置
cursor.scroll(1, \'relative\')    # 相对移动,往后移动一个位置# 关闭链接
cursor.close()
conn.close()

 

三、sql注入

1、SQL注入

千万不要手动拼接(关键性参数)查询条件,查:校验用户和密码是否正确

import pymysql

conn = pymysql.connect(
    host=\'127.0.0.1\',
    port=3306,
    user=\'root\',
    password=\'3822515\',
    database=\'day41_1\',
    charset=\'utf8\',
    
)
cursor = conn.cursor(pymysql.cursors.DictCursor)
username = input(\'username:>>>\')
password = input(\'password:>>>\')

sql = \'select * from userinfo where name=%s and password=%s\'    # sql注入,千万不要手动拼接(关键性参数)查询条件
print(sql)

res = cursor.execute(sql, (username, password))

if res:
    pass
else:
    print(\'用户名和密码错误\')

2、增:往表userinfo内增加用户名和密码

import pymysql

conn = pymysql.connect(
    host=\'127.0.0.1\',
    port=3306,
    user=\'root\',
    password=\'3822515\',
    database=\'day41_1\',
    charset=\'utf8\',
    autocommit=True   # 和下面的conn.commit()是同一个意思,选一个就好
)
cursor = conn.cursor(pymysql.cursors.DictCursor)
username = input(\'username:>>>\')
password = input(\'password:>>>\')

sql = \'insert into userinfo(name,password) values(%s,%s)\'
res = cursor.execute(sql, (username, password))
# conn.commit()    # 确认数据无误之后 commit之后才会将数据真正修改到数据库
print(sql)

if res:
    pass
else:
    print(\'用户名和密码错误\')

3、改:修改指定userinfo表中id的用户名或者密码

import pymysql

conn = pymysql.connect(
    host=\'127.0.0.1\',
    port=3306,
    user=\'root\',
    password=\'3822515\',
    database=\'day41_1\',
    charset=\'utf8\',
    autocommit=True  # 此行和下面的conn.commit()意思相同,任选一种即可
)

cursor = conn.cursor(pymysql.cursors.DictCursor)
sql = "update userinfo set name=\'jerry\' where id =4"
res = cursor.execute(sql)
# conn.commit()  # 确认数据无误之后 commit之后才会将数据真正修改到数据库
print(sql)
if res:
    pass
else:
    print(\'用户名或密码错误!\')

 


 

 

分类:

技术点:

相关文章:

  • 2021-09-24
  • 2021-05-01
  • 2021-12-03
  • 2021-11-23
  • 2018-04-26
猜你喜欢
  • 2021-09-08
  • 2021-12-07
  • 2021-12-07
  • 2021-12-10
  • 2021-07-07
  • 2021-12-07
  • 2021-12-20
相关资源
相似解决方案