Skip to content

Commit 429a4c8

Browse files
committed
Updates Python编程之MySQLdb模块.md
Auto commit by GitBook Editor
1 parent 38e0950 commit 429a4c8

File tree

1 file changed

+31
-5
lines changed

1 file changed

+31
-5
lines changed

Python编程之MySQLdb模块.md

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,19 @@
22

33

44
## python执行环境:
5+
56
解释器环境与选项;
7+
68
python解释器启动:
9+
710
python [options] [-c cmd | filename | - ] [args]
811

912
![python_options](/images/python_options.png)
1013

14+
---
15+
1116
## python解释器环境变量
17+
1218
![python_variables](/images/python_variables.png)
1319

1420
## python代码的测试、调试与探查
@@ -18,7 +24,7 @@ python [options] [-c cmd | filename | - ] [args]
1824
内置函数help()或对象的默认方法__doc__可以显示这些文档字符串;
1925

2026
Example:
21-
27+
```python
2228
In [22]: def Sum(num1,num2):
2329
...: """the sumary of num1 and num2.
2430
...: >>> Sum(2,5)
@@ -44,7 +50,9 @@ Out[24]: 'the sumary of num1 and num2.\n >>> Sum(2,5)\n 7\n >>> Sum(12,
4450
```
4551

4652
## doctest模块:
53+
4754
doctest模块允许在文档字符串内潜入注释以显示各种语句的期望行为,尤其是函数和方法的结果。
55+
4856
- 此处的文档字符串看起来如同一个交互式shell会话;
4957
- 可用于测试文档是否与程序主体保持同步,或基于文档对程序本身做测试;
5058

@@ -110,12 +118,14 @@ Out[3]: TestResults(failed=1, attempted=2)
110118
```
111119

112120
创建可自测试的模块:
121+
113122
在模块的尾部添加如下代码即可:
114123
if __name__ == '__main__':
115124
import doctest
116125
doctest.testmod()
117126

118127
此类模块在python解释器中直接运行时即能进行自我测试;
128+
119129
```python
120130
sslinux@sslinux-pygo:~$ python test.py
121131
**********************************************************************
@@ -133,14 +143,17 @@ Got:
133143
```
134144

135145
python:
146+
136147
unitest 统一测试框架
137148

138149

139150
## Python访问MariaDB
140151

141152
依赖于模块:MySQLdb --依赖于--> easy_install
153+
142154
编译、安装;
143155

156+
```bash
144157
setuptools.1.1.1.tar.gz
145158
tar xf setuptools.1.1.1.tar.gz
146159
cd setuptools
@@ -156,14 +169,18 @@ python setup.py install
156169
vim /etc/ld.so.conf.d/mysql.conf
157170
# 添加MySQL或MariaDB的库文件目录;
158171
ldconfig # 使其生效;
172+
```
173+
159174

160175
```python
161176
import MySQLdb as mysql
162177
```
178+
163179
### 创建MySQL连接:
164-
![MySQL_Connections](/images/mysql_connections.png)
165180

181+
![MySQL_Connections](/images/mysql_connections.png)
166182

183+
```python
167184
conn = mysql.connect(host='10.60.4.167',user='sslinux',passwd='sslinux',db='mydb')
168185
conn.stat() # 获取服务器状态;
169186
conn.ping() # 测试连通性;
@@ -192,8 +209,13 @@ cur.execute(sqlins)
192209

193210
cur.execute('select * form t1')
194211
cur.fetchall()
212+
```
213+
214+
---
195215

196216
# 批量插入:
217+
218+
```python
197219
sqlins = 'insert into t1 (name) value ("tom"),("stu1"),("stu2")'
198220

199221
sqlins = 'insert into t1 (name) value (%s)' # 无论数据类型为何,都用%s;
@@ -206,9 +228,10 @@ sqlins = 'insert into t2 (id,name,gerder,age) values (%s,%s,%s,%s)'
206228
cur.execute(sqlins,(0,'tom','male',30))
207229
cur.execute('select * from t2')
208230
cur.fetchall()
209-
231+
```
210232

211233
# 一次插入多行数据;
234+
```python
212235
datains = ((2,'str2','female',17),(3,'stu3','male',27))
213236
cur.executemany(sqlins,datains)
214237

@@ -222,9 +245,12 @@ a[2][2]
222245

223246
cur.close() #关闭游标;
224247
conn.close() # 关闭连接;
248+
```
225249

250+
### 使用流程:
251+
252+
```python
226253

227-
使用流程:
228254
import MySQLdb as mysql
229255

230256
建立连接:
@@ -251,7 +277,7 @@ cur = conn.curson()
251277
关闭:
252278
cur.close()
253279
conn.close()
254-
280+
```
255281

256282
## 练习:
257283
把/etc/passwd文件中的内容的每一行存储在MariaDB的表中;

0 commit comments

Comments
 (0)