-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBASE_DIR.patch
More file actions
164 lines (161 loc) · 6.05 KB
/
BASE_DIR.patch
File metadata and controls
164 lines (161 loc) · 6.05 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
Index: 04更高级/08Python操作mysql/05预编译SQL.py
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- 04更高级/08Python操作mysql/05预编译SQL.py (revision )
+++ 04更高级/08Python操作mysql/05预编译SQL.py (revision )
@@ -0,0 +1,60 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+# Created by fengL on 2017/9/18
+import pymysql
+
+
+#创建数据库连接
+conn = pymysql.connect(host="127.0.0.1",port=3306,user="root",passwd="Root",db="pymysql",charset="utf8")
+
+#创建游标
+cursor = conn.cursor()
+
+# name = input("请输入用户名:")
+# print(name)
+# age = int(input("请求输入年龄:"))
+# print(age)
+#执行SQL,返回结果为受影响的行数
+#一定要使用这样的方式,而避免使用字符串拼接的方式,为了防止SQL注入。
+# r = cursor.execute("insert into user(name) values (%s)",name)
+#如果要传入多个参数,就通过传入一个元组的方式
+# r = cursor.execute("insert into user(name,age) values (%s,%s)",(name,age))
+
+#批量执行SQL:通过传入一个list,里面存多个元组。
+# l = [("age",10),("feng",55),("fengluian",88)]
+#然后调用cursor.executemany方法
+# r = cursor.executemany("insert into user(name,age) values (%s,%s)",l)
+
+#增删改查都可以使用cursor.execute方法来执行。
+
+#update
+# r = cursor.execute("update user set name=%s where name =%s",("fengLian","feng"))
+#提交SQL,查询时不用进行commit操作。
+# conn.commit()
+
+
+#查询操作:查询所欲结构
+data = cursor.execute("select * from user where name =%s",("fengLian"))
+#取所有记录
+# r = cursor.fetchall()
+#获取多个记录
+# r = cursor.fetchmany(2)
+#获取一条记录:fetchone类似指针,每取一次往下移动一条记录。
+r = cursor.fetchone()
+print(r)
+#
+r = cursor.fetchone()
+print(r)
+r = cursor.fetchone()
+print(r)
+
+#游标实际就是指针,我们可以直接操作指针,来移动
+#mode模式可以取值为:absolute(指针回到绝对位置,);relative(相对位置1向下移动一个位置,-1向上移动一个位置。)
+cursor.scroll(0,mode="absolute")
+
+# 关闭游标
+cursor.close()
+# 关闭连接
+conn.close()
+
+#注意:数据库分页和游标获取记录不冲突的,数据库分页是在数据库里面进行的,而游标只是把数据库里面的数据拿到内存厘里面,通过游标的相应fetch查询来以不同的方式获取这些分页后的数据。
\ No newline at end of file
Index: 03高级语法/05装饰器-闭包/使用装饰器实现网站登录.py
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- 03高级语法/05装饰器-闭包/使用装饰器实现网站登录.py (revision )
+++ 03高级语法/05装饰器-闭包/使用装饰器实现网站登录.py (revision )
@@ -0,0 +1,25 @@
+# #!/usr/bin/env python
+# # -*- coding: utf-8 -*-
+# # Created by fengL on 2017/9/18
+#
+# login_status = False
+#
+# def login():
+# if login_status:
+# username = input("username:")
+# password = input("passwd:")
+# if user == username and password = password:
+# print("welcome.....")
+# home()
+# login_status = True
+# else:
+# pass
+#
+# #使用装饰器:在各个方法上,都添加上login装饰器,那么就不用访问网站中的每个地方都进要输入用户名和密码进行登录。
+# @login
+# def home():
+# print("welcome to home page")
+#
+# @login
+# def book():
+# print("welcome to book page")
\ No newline at end of file
Index: 03高级语法/06生成器/利用yield实现为并发.py
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- 03高级语法/06生成器/利用yield实现为并发.py (revision )
+++ 03高级语法/06生成器/利用yield实现为并发.py (revision )
@@ -0,0 +1,4 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+# Created by fengL on 2017/9/18
+
Index: 04更高级/08Python操作mysql/02获取新创建数据自增ID.py
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- 04更高级/08Python操作mysql/02获取新创建数据自增ID.py (revision 5a718f4983ce9c19cfffd8cc6a2265bf7b58e649)
+++ 04更高级/08Python操作mysql/02获取新创建数据自增ID.py (revision )
@@ -6,12 +6,13 @@
import pymysql
-conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='123', db='t1')
+conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='Root', db='pymysql',charset="utf8")
cursor = conn.cursor()
-cursor.executemany("insert into hosts(host,color_id)values(%s,%s)", [("1.1.1.11", 1), ("1.1.1.11", 2)])
+cursor.executemany("insert into user(name,age) values(%s,%s)", [("1.1.1.11", 1)])
conn.commit()
cursor.close()
conn.close()
# 获取最新自增ID
-new_id = cursor.lastrowid
\ No newline at end of file
+new_id = cursor.lastrowid
+print(new_id)
\ No newline at end of file
Index: 04更高级/08Python操作mysql/06通过游标获取字典数据.py
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- 04更高级/08Python操作mysql/06通过游标获取字典数据.py (revision )
+++ 04更高级/08Python操作mysql/06通过游标获取字典数据.py (revision )
@@ -0,0 +1,15 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+# Created by fengL on 2017/9/18
+#1:默认通过游标获取的数据是元组类型的。
+import pymysql
+conn = pymysql.connect(host="127.0.0.1",port=3306,user="root",passwd="Root",db="pymysql",charset="utf8")
+#2:设置游标为字典类型
+cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
+r = cursor.execute("select * from user")
+data = cursor.fetchall()
+print(data)
+conn.commit()
+cursor.close()
+conn.close()
+