프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / DTI_PID / SG_DbHelper.py @ 936111d1

이력 | 보기 | 이력해설 | 다운로드 (5.03 KB)

1
import sqlite3
2
import os
3
import sys
4
import symbol
5
import SymbolBase
6
from sqlite3 import Error
7

    
8
class SG_DbHelper():
9
    ROOT_DIR = ""
10
    DB_PATH = "db"
11
    DB_NAME = "ITI_PID.db"
12

    
13
    CREATE_SYMBOLS_TABLE_SQL = '''
14
        CREATE TABLE 'Symbol' (
15
            'uid'        INTEGER PRIMARY KEY AUTOINCREMENT,
16
                'symId'        INTEGER NOT NULL,
17
                'name'        TEXT NOT NULL,
18
                'type'        TEXT,
19
                'path'        TEXT,
20
                'threshold'        NUMERIC NOT NULL DEFAULT 0.4,
21
                'minMatchPoint'        INTEGER NOT NULL DEFAULT 0,
22
                'isDetectOrigin'        INTEGER NOT NULL DEFAULT 0,
23
                'rotationCount'        INTEGER NOT NULL DEFAULT 4,
24
                'ocrOption'        INTEGER NOT NULL DEFAULT 0,
25
                'isContainChild'        INTEGER NOT NULL DEFAULT 0,
26
                'originalPoint'        TEXT,
27
                'connectionPoint'        TEXT,
28
                'baseSymbol'        TEXT,
29
                'additionalSymbol'        TEXT
30
        );
31
    '''
32

    
33
    INSERT_SYMBOL_SQL = '''
34
        INSERT INTO Symbol(symId, name, type, path, threshold, minMatchPoint, isDetectOrigin, rotationCount, ocrOption, isContainChild, originalPoint, connectionPoint, baseSymbol, additionalSymbol) 
35
        VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);
36
    '''
37

    
38
    UPDATE_SYMBOL_SQL = '''
39
        UPDATE Symbol
40
        SET
41
            symId = ?, name = ?, type = ?, path = ?, threshold = ?, minMatchPoint = ?, isDetectOrigin = ?,
42
            rotationCount = ?, ocrOption = ?, isContainChild = ?, originalPoint = ?, connectionPoint = ?,
43
            baseSymbol = ?, additionalSymbol = ?
44
        WHERE uid = ?
45
    '''
46

    
47
    def __init__(self, rootDir):
48
        print("DB Helper __init__")
49
        self.ROOT_DIR = rootDir
50
        
51
        self.dbFullPath = self.ROOT_DIR+"/"+self.DB_PATH+"/"+self.DB_NAME
52
        #if not os.path.isfile(dbFullPath):
53
        #    print("DB doesn't exist")
54

    
55
        try:
56
            conn = sqlite3.connect(self.dbFullPath)
57
            print(sqlite3.version)
58
            
59
            cursor = conn.cursor()
60
            cursor.execute(self.CREATE_SYMBOLS_TABLE_SQL)
61
        except Error as e:
62
            print(e)
63
        finally:
64
            conn.close()
65

    
66
    def isExistData(self, fieldName, data):
67
        rows = None
68
        try:
69
            conn = sqlite3.connect(self.dbFullPath)
70
            cursor = conn.cursor()
71
            sql = ""
72
            if isinstance(data, str):
73
                sql = "SELECT * FROM Symbol WHERE " + fieldName + " = '"+ data +"'"
74
            else:
75
                sql = "SELECT * FROM Symbol WHERE " + fieldName + " = "+ str(data) +""
76
            cursor.execute(sql)
77
            rows = cursor.fetchall()
78
        except Error as e:
79
            print(e)
80
        finally:
81
            conn.close()
82
            if rows is not None and len(rows) > 0:
83
                return True
84
            else:
85
                return False
86

    
87
    def isExistFileName(self, imageDir):
88
        rows = None
89
        try:
90
            conn = sqlite3.connect(self.dbFullPath)
91
            cursor = conn.cursor()
92
            sql = "SELECT * FROM Symbol WHERE path = '"+ imageDir +"'"
93
            cursor.execute(sql)
94
            rows = cursor.fetchall()
95
        except Error as e:
96
            print(e)
97
        finally:
98
            conn.close()
99
            if rows is not None and len(rows) > 0:
100
                return True
101
            else:
102
                return False
103

    
104
    def insertSymbol(self, symbol):
105
        isAdded = False
106
        try:
107
            conn = sqlite3.connect(self.dbFullPath)
108
            
109
            cursor = conn.cursor()
110
            query = (symbol.getId(), symbol.getName(), symbol.getType(), symbol.getPath(), symbol.getThreshold()
111
                           , symbol.getMinMatchCount(), symbol.getIsDetectOnOrigin(), symbol.getRotationCount()
112
                           , symbol.getOcrOption(), symbol.getIsContainChild()
113
                           , symbol.getOriginalPoint(), symbol.getConnectionPoint()
114
                           , symbol.getBaseSymbol(), symbol.getAdditionalSymbol())
115
            cursor.execute(self.INSERT_SYMBOL_SQL, query)
116
            conn.commit()
117
            isAdded = True
118
        except Error as e:
119
            print(e)
120
        finally:
121
            conn.close()
122
            return (isAdded, symbol.getName(), symbol.getPath())
123
        
124
    def updateSymbol(self, symbol):
125
        isUpdated = False
126
        try:
127
            conn = sqlite3.connect(self.dbFullPath)
128
            
129
            cursor = conn.cursor()
130
            query = (symbol.getId(), symbol.getName(), symbol.getType(), symbol.getPath(), symbol.getThreshold()
131
                           , symbol.getMinMatchCount(), symbol.getIsDetectOnOrigin(), symbol.getRotationCount()
132
                           , symbol.getOcrOption(), symbol.getIsContainChild()
133
                           , symbol.getOriginalPoint(), symbol.getConnectionPoint()
134
                           , symbol.getBaseSymbol(), symbol.getAdditionalSymbol(), symbol.getUid())
135
            cursor.execute(self.UPDATE_SYMBOL_SQL, query)
136
            conn.commit()
137
            isUpdated = True
138
        except Error as e:
139
            print(e)
140
        finally:
141
            conn.close()
142
            return (isUpdated, symbol.getName(), symbol.getPath())