ディレクトリ
pymysql運用データベース
シンプルな操作
import pymysql # pip install pymysql
# 连接数据库的参数
conn = pymysql.connect(host= 'localhost',
user='root',
password = '123',
database='db3',
charset = 'utf8')
# cursor = conn.cursor() # <pymysql.cursors.Cursor object at 0x000000000A0E2C50>,游标对象,默认返回的值是元祖类型
cursor = conn.cursor(cursor = pymysql.cursors.DictCursor) # <pymysql.cursors.DictCursor object at 0x000000000A0E2C88> 返回的值是字典类型
# print(cursor)
sql = "select name from userinfo where id>10"
cursor.execute(sql)
res= cursor.fetchall() # #取出所有的数据 返回的是列表套字典
# res = cursor.fetchone() # 只获取一条数据
# res = cursor.fetchmany(size) # 可指定参数, 取出size条数据 返回的是列表套字典
print(res)
cursor.close()
conn.close()
'''
[{'name': '丁丁'}, {'name': '星星'}, {'name': '格格'}, {'name': '张野'}, {'name': '程咬金'}, {'name': '程咬银'}, {'name': '程咬铜'}, {'name': '程咬铁'}]
'''
SQLインジェクションの問題
import pymysql
user = input('输入用户名:').strip()
password = input('输入密码:').strip()
# 连接数据库的参数
conn = pymysql.connect(host= 'localhost',
user='root',
password = '123',
database='db3',
charset = 'utf8')
# cursor = conn.cursor() # <pymysql.cursors.Cursor object at 0x000000000A0E2C50>,游标对象,默认返回的值是元祖类型
cursor = conn.cursor(cursor = pymysql.cursors.DictCursor) # <pymysql.cursors.DictCursor object at 0x000000000A0E2C88> 返回的值是字典类型
# print(cursor)
sql = "select * from user where name='%s' and password='%s'"%(user,password)
print(sql)
# exit()
cursor.execute(sql)
res= cursor.fetchall() # #取出所有的数据 返回的是列表套字典
# res = cursor.fetchone() # 只获取一条数据
# res = cursor.fetchmany(size) # 可指定参数, 取出size条数据 返回的是列表套字典
print(res)
if res:
print('登录成功!')
else:
print('登录失败!')
cursor.close()
conn.close()
'''
输入用户名:zhang' #
输入密码:123
select * from user where name='zhang' #' and password='123'
[{'name': 'zhang', 'password': '123 '}]
登录成功!
'''
'''
输入用户名:zhang' or 1=1 #
输入密码:123
select * from user where name='zhang' or 1=1 #' and password='123'
[{'name': 'zhang', 'password': '123 '}]
登录成功!
'''
# 输入用户名的时候,输入‘#’将后面的密码验证部分注释掉了,语句变成了select * from user where name='zhang' 或者 select * from user where name='zhang' or 1=1,直接跳过了密码的验证环节,出现了sql语句的注入问题。
SQLインジェクションの問題解決策
# 出现sql注入问题主要是我们对用户输入没有做合法性验证,
# 方法1:如果对用户输入的值进行判断或者转译,检查完之后或者转译后就不会出现此类问题了。这样低级安全问题不该出现!
# 方法2:将参数以元组或列表的形式传入execute中,让它帮我们转译和检验;即:cursor.execute(sql,(user,password))
import pymysql
user = input('输入用户名:').strip()
password = input('输入密码:').strip()
# 连接数据库的参数
conn = pymysql.connect(host= 'localhost',
user='root',
password = '123',
database='db3',
charset = 'utf8')
# cursor = conn.cursor() # <pymysql.cursors.Cursor object at 0x000000000A0E2C50>,游标对象,默认返回的值是元祖类型
cursor = conn.cursor(cursor = pymysql.cursors.DictCursor) # <pymysql.cursors.DictCursor object at 0x000000000A0E2C88> 返回的值是字典类型
# print(cursor)
sql = "select * from user where name=%s and password=%s"
print(sql)
# exit()
cursor.execute(sql,(user,password))
res= cursor.fetchall() # #取出所有的数据 返回的是列表套字典
# res = cursor.fetchone() # 只获取一条数据
# res = cursor.fetchmany(size) # 可指定参数, 取出size条数据 返回的是列表套字典
print(res)
if res:
print('登录成功!')
else:
print('登录失败!')
cursor.close()
conn.close()
'''
输入用户名:zhang
输入密码:123
select * from user where name=%s and password=%s
[{'name': 'zhang', 'password': '123 '}]
登录成功!
'''
'''
输入用户名:zhang' #
输入密码:123
select * from user where name=%s and password=%s
()
登录失败!
'''
SQLインジェクションの問題の概要テンプレート
产生的原因:
因为过于相信用户输入的内容, 根本没有做任何的检验
解决的方法:
sql = "select * from user where name=%s and password=%s"
# execute帮我们做字符串拼接,我们无需且一定不能再为%s加引号了
cursor.execute(sql, (user, pwd))
连接:
### 连接数据库的参数
conn = pymysql.connect(host='localhost',user='root',password='123qwe',database='test',charset='utf8')
# cursor = conn.cursor() ### 默认返回的值是元祖类型
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) ### 返回的值是字典类型 (*********)
查:
fetchall() : 取出所有的数据 返回的是列表套字典
fetchone() : 取出一条数据 返回的是字典
fetchmany(size) : 取出size条数据 返回的是列表套字典
pymysqlデータベース(CRUD)を使用して操作、conn.commit()
# 插入
import pymysql
# 连接数据库的参数
conn = pymysql.connect(host= 'localhost',
user='root',
password = '123',
database='db3',
charset = 'utf8')
cursor = conn.cursor(cursor = pymysql.cursors.DictCursor)
sql = "insert into user(name,password) values(%s,%s)"
print(sql) # insert into user(name,password) values(%s,%s)
# cursor.execute(sql,("tank",'qwe')) # 插入单条
data=[('小王','111'),('小张','222'),('小李','333'),('小张','444'),]
cursor.executemany(sql,data) # 插入多条
conn.commit()
cursor.close()
conn.close()
# 插入单条情况下
'''
mysql> select * from user;
+-------+----------+
| name | password |
+-------+----------+
| zhang | 123 |
| tank | qwe |
+-------+----------+
2 rows in set (0.00 sec)
'''
# 插入多条情况下
'''
mysql> select * from user;
+--------+----------+
| name | password |
+--------+----------+
| zhang | 123 |
| tank | qwe |
| tank | qwe |
| 小王 | 111 |
| 小张 | 222 |
| 小李 | 333 |
| 小张 | 444 |
+--------+----------+
7 rows in set (0.00 sec)
'''
# 修改
先给user表增加一个id索引字段;
mysql> alter table user add id int primary key auto_increment FIRST;
Query OK, 0 rows affected (0.50 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> select * from user;
+----+--------+----------+
| id | name | password |
+----+--------+----------+
| 1 | zhang | 123 |
| 2 | tank | qwe |
| 3 | tank | qwe |
| 4 | 小王 | 111 |
| 5 | 小张 | 222 |
| 6 | 小李 | 333 |
| 7 | 小张 | 444 |
+----+--------+----------+
7 rows in set (0.00 sec)
# 进行修改操作 将 id=6 的记录的 name 字段的名字修改为 ‘小李子’
import pymysql
# 连接数据库的参数
conn = pymysql.connect(host= 'localhost',
user='root',
password = '123',
database='db3',
charset = 'utf8')
cursor = conn.cursor(cursor = pymysql.cursors.DictCursor)
sql = "update user set name = %s where id = %s"
cursor.execute(sql,('小李子',6)) # 插入多条
conn.commit()
cursor.close()
conn.close()
'''
mysql> select * from user;
+----+-----------+----------+
| id | name | password |
+----+-----------+----------+
| 1 | zhang | 123 |
| 2 | tank | qwe |
| 3 | tank | qwe |
| 4 | 小王 | 111 |
| 5 | 小张 | 222 |
| 6 | 小李子 | 333 |
| 7 | 小张 | 444 |
+----+-----------+----------+
7 rows in set (0.00 sec)
'''
# 删除
import pymysql
# 连接数据库的参数
conn = pymysql.connect(host= 'localhost',
user='root',
password = '123',
database='db3',
charset = 'utf8')
cursor = conn.cursor(cursor = pymysql.cursors.DictCursor)
sql = "delete from user where id>%s"
cursor.execute(sql,6) # 插入多条
conn.commit()
cursor.close()
conn.close()
'''
mysql> select * from user;
+----+-----------+----------+
| id | name | password |
+----+-----------+----------+
| 1 | zhang | 123 |
| 2 | tank | qwe |
| 3 | tank | qwe |
| 4 | 小王 | 111 |
| 5 | 小张 | 222 |
| 6 | 小李子 | 333 |
+----+-----------+----------+
6 rows in set (0.00 sec)
'''
指数
1.なぜインデックスを持っています
一般的なアプリケーションは、読んで、約10の比率で書く:1、およびめったに入れないと、本番環境での更新操作の一般的なパフォーマンスの問題は、我々はほとんど遭遇し、最も問題、またはいくつかの複雑ですしたがって、クエリ文を最適化するクエリ操作は、明らかに最優先です。クエリをスピードアップするために言えば、我々はインデックスを言及する必要があり
2.インデックスとは何ですか?
また、「キー」と呼ばれるMySQLのインデックスでは、すぐにレコードを検索するために使用されるデータ構造のストレージエンジンです。良好なパフォーマンスのためのインデックスは
、重大である場合は特に、より多くのテーブル内のデータの量、パフォーマンスの指標より重要な効果。
インデックスの最適化は、クエリのパフォーマンスを最適化するための最も有効な手段である必要があります。インデックスは、簡単に数桁クエリのパフォーマンスを向上させることができます。
辞書インデックスは、あなたがシーケンサーのテーブルを使用しない場合は、単語を確認したい場合は、あなたが確認する1つのページから数百ページが必要になり、シーケンサテーブルに相当します。
インデックスの使用の長所と短所3
あまりにも多くのインデックス場合は、アプリケーションのパフォーマンスが影響を受ける可能性があります。インデックスは、あまりにも、アプリケーションの性能にとって重要であるバランスを見つけるために、クエリのパフォーマンスへの影響を生成します。もちろん、インデックスも可能ではないですが、私はこのような問題が発生した:ディスクの使用率が100%になっている示してiostatの特定のMySQLサーバの場合、分析は、開発者にあまりにも多くのインデックスを追加することにより、削除することを発見した後、いくつかの不要なインデックスの後、ディスク使用量はすぐに20%に低下しました。目に見える指標は非常に技術的な内容である加えます。
利点:クエリのパフォーマンスを向上させる;欠点:インデックスを追加した後のディスクスペースの多くを取るだろう
4.インデックスの原則
B +ツリー
基本的にこのインデックス機構と、私たちは常に同じを使用することができ、常にランダムイベントはイベントの順序になっている間、すなわち、データへのアクセスの範囲を狭めるたいことであなたが望む最終結果をフィルタリングするにはデータ型をロックする方法を見つけます
# 插入3000,000条数据做实验 哈哈哈哈 !!!!
import pymysql
# 连接数据库的参数
conn = pymysql.connect(host= 'localhost',
user='root',
password = '123',
database='db3',
charset = 'utf8')
cursor = conn.cursor(cursor = pymysql.cursors.DictCursor)
l=[]
for i in range(3000000):
res=(f'小张{i}',str(i))
l.append(res)
sql = "insert into user(name,password) values(%s,%s)"
cursor.executemany(sql,l) # 插入多条
conn.commit()
cursor.close()
conn.close()
'''
| 1033019 | 小张2829 | 2829 |
| 1033020 | 小张2830 | 2830 |
| 1033021 | 小张2831 | 2831 |
| 1033022 | 小张2832 | 2832 |
| 1033023 | 小张2833 | 2833 |
| 1033024 | 小张2834 | 2834 |
| 1033025 | 小张2835 | 2835 |
| 1033026 | 小张2836 | 2836 |
| 1033027 | 小张2837 | 2837 |
| 1033028 | 小张2838 | 2838 |
| 1033029 | 小张2839 | 2839 |
| 1033030 | 小张2840 | 2840 |
| 1033031 | 小张2841 | 2841 |
| 1033032 | 小张2842 | 2842 |
| 1033033 | 小张2843 | 2843 |
| 1033034 | 小张2844 | 2844 |
| 1033035 | 小张2845 | 2845 |
| 1033036 | 小张2Ctrl-C -- sending "KILL QUERY 1" to server ...
846 Ctrl-C -- query aborted.
| 2846 |
+---------+---------------+----------+
3000006 rows in set (2.46 sec)
'''
種の5.インデックス
索引的种类:(**************************)
主键索引: 加速查找 + 不能重复 + 不能为空 primary key
唯一索引: 加速查找 + 不能重复 unique(name)
联合唯一索引:unique(name, email)
例子:
zekai [email protected]
zekai [email protected]
普通索引: 加速查找 index (name)
联合索引: index (name, email)
インデックスを作成します
主キー索引
主键索引:
新增主键索引:
create table xxx(
id int auto_increment ,
primary key(id)
)
改:
alter table xxx change id id int auto_increment primary key;
alter table xxx modify id int auto_increment primary key; # 同上
alter table t1 add primary key (id);
删除主键索引:
mysql> alter table t1 drop primary key;
インデックスのみ
唯一索引:
新增:
1.
create table t2(
id int auto_increment primary key,
name varchar(32) not null default '',
unique u_name (name)
)charset utf8
2.
CREATE UNIQUE INDEX 索引名 ON 表名 (字段名) ;
create unique index ix_name on t2(name);
3.
alter table t2 add unique index ix_name (name)
删除:
alter table t2 drop index u_name;
一般的な指標
普通索引:
新增:
1.
create table t3(
id int auto_increment primary key,
name varchar(32) not null default '',
index u_name (name)
)charset utf8
2.
CREATE INDEX 索引名 ON 表名 (字段名) ;
create index ix_name on t3(name);
3.
alter table t3 add index ix_name (name)
删除:
alter table t3 drop index u_name;