-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathitermysqldata.py
More file actions
43 lines (31 loc) · 938 Bytes
/
itermysqldata.py
File metadata and controls
43 lines (31 loc) · 938 Bytes
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
#!/usr/bin/env python
#coding:utf-8
import pymysql
class src:
def __init__(self,host,user,passwd,dbname):
self.conn = pymysql.connect(host,user,passwd,dbname, use_unicode=True, charset="utf8")
self.cursor = self.conn.cursor()
self.data = self.getdata()
def __iter__(self):
return self
def __next__(self):
self.i = self.data.__next__()
if self.i:
return self.i
raise StopIteration
@property
def totalnum(self):
sql = 'select count(*) from table'
self.cursor.execute(sql)
totalnum = self.cursor.fetchall()
return totalnum[0][0]
def getdata(self):
sql = 'select * from table'
self.cursor.execute(sql)
data = self.cursor.fetchall()
self.cursor.close()
self.conn.close()
yield from data
s = src('127.0.0.1', 'user', 'pass', 'dbname')
for i in s:
print(i)