-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtodb.py
111 lines (94 loc) · 2.84 KB
/
todb.py
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
# -*- coding: utf-8 -*-
# transfer duco performance to database
import MySQLdb as mdb
import time
class todb():
def __init__(self):
self.db = mdb.connect(host='localhost',user='root', password='', database='duco')
self.cs= self.db.cursor()
self.verbose=False
self.tab="games"
#self.verbose=True
def zeit(self):
return time.strftime("'%Y-%m-%d %H:%M:%S'", time.localtime())
#return time.strftime("'%Y %b %d %H:%M:%S'", time.localtime())
def say(self,s):
if self.verbose:
print (str(s))
def logge(self,text,prio=5):
t="insert into log (Zeit,Prio,Text) values ("
t+=self.zeit()+","+str(prio)+",'"+text+"')"
self.execc(t)
print (">"+text)
def fetch1(self,query):
self.say ("Fetch1: "+query)
self.cs.execute(query)
result = self.cs.fetchone()
self.say ("answer: %s" % str(result))
self.db.commit()
return result
def fetch99(self,query):
self.say ("Fetch99: "+query)
self.cs.execute(query)
result = self.cs.fetchall()
self.say ("answer: %s" % str(result))
self.db.commit()
return result
def execc(self,query):
self.say ("execc: "+query)
try:
tmp=self.cs.execute(query)
except Exception as inst:
print ("ldb exec Exception "+str(inst) )
tmp="kaputt"
self.db.commit()
return tmp
def getManyOne(self,query):
#returns on col as list
tmp=self.fetch99(query)
w=[]
for t in tmp:
ts=str(t[0])
w.append(ts)
return w
def getDict(self,query):
#returns dict, col 0 is key
tmp=self.fetch99(query)
w={}
for t in tmp:
w[t[0]]=t[1]
return w
def getDictNum(self,query):
tmp=self.fetch99(query)
w={}
for t in tmp:
if t[1] is None:
w[t[0]]=0
else:
w[t[0]]=t[1]
return w
def insertPrime(self,game,datei,algo,feld):
t="insert into "+ self.tab+" (datei,game,algo,feld) values ("
t+=str(datei)+","+str(game)+",'"+algo+"',"
t+="'"+feld+"');"
self.execc(t)
def readFile(self,myName,myNum):
fil = open(myName, 'r')
count=0
while True:
count += 1
line = fil.readline().strip()
if not line:
break
print("Line{}: {}".format(count, line))
self.insertGame(count,myNum,'a',line)
fil.close()
if __name__ == "__main__":
k=todb()
k.verbose=True
if 0:
k.readFile('top95.txt',4)
if 1:
k.readFile('kudok.txt',1)
if 0:
k.insertGame(4711,2,'a','7862347864866')