This repository has been archived by the owner on May 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
DataBaseManager.py
108 lines (93 loc) · 2.77 KB
/
DataBaseManager.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
import sqlite3
import copy
class DBHandler(object):
def __init__(self, FileName, InMem = False):
self.File = FileName if not InMem else ":memory:"
self.Database = sqlite3.connect(self.File)
def __YieldCommand(self, SubKeyInfo):
Keys = SubKeyInfo.keys()
for Key in Keys:
Last = ""
Info = SubKeyInfo[Key]
Type = Info[0].upper()
IsNull = "" if Info[1] else "NOT NULL"
ItemList = [Key, Type, IsNull]
Complete = " ".join(ItemList).rstrip(" ") + ","
yield Complete + Last
def setStruct(self, Struct):
self.Struct = Struct
self.IndexInfo = Struct['index']
self.SubKeyInfo = Struct['others']
return True
def createTable(self, TableName):
Database = self.Database
Head = "CREATE TABLE %s" % TableName
SubKeyInfo = copy.deepcopy(self.SubKeyInfo)
CommandIter = self.__YieldCommand(SubKeyInfo)
IndexInfo = self.IndexInfo
Last = ""
if IndexInfo:
Index = set(IndexInfo.keys()).pop()
IndexType = IndexInfo[Index].upper()
First = "(" + Index + " %s PRIMARY KEY NOT NULL," % IndexType
else:
First = "("
try:
while True:
Last += CommandIter.__next__()
except StopIteration:
Command = First + Last.rstrip(',') + ");"
Command = Head + Command
self.Execute(Command)
return Command
def execute(self, Command):
Database = self.Database
Get = Database.execute(Command)
# Database.commit()
return Get
def insert(self, TableName, InsertValue, InsertSeq = None):
Database = self.Database
InsertSeq = "" if not InsertSeq else str(InsertSeq)
InsertValue = str(InsertValue)
Command = "INSERT INTO %s %s VALUES %s;" % (TableName, InsertSeq, InsertValue)
self.Execute(Command)
return Command
def Query(self, TableName):
# 未完成
Database = self.Database
Command = "SELECT * FROM %s" % TableName
Result = self.Execute(Command)
return Result
def Commit(self):
self.Database.commit()
return True
def GetConn(self):
return self.Database
def GetCursor(self):
return self.Database.cursor()
def allTables(self)
Cursor = self.GetCursor()
Cursor.execute(SELECT name FROM sqlite_master WHERE type='table';)
TableTuple = Cursor.fetchall()
self.TableList = [Element[0] for Element in TableTuple]
return self.TableList
def haveTable(self, TableName)
TableList = self.Tables()
if TableName in TableList
return True
return False
def countRecordsFrom(self, TableName)
Command = SELECT count() FROM %s % TableName
Result = self.Execute(Command)
Result = Result.fetchall()[0][0]
return Result
def countAllRecords(self):
Result = 0
for table in self.allTables():
Result += self.countRecordsFrom(table)
return Result
def __del__(self):
try:
self.Database.close()
except:
pass