-
Notifications
You must be signed in to change notification settings - Fork 0
/
cn_sqlite.py
183 lines (148 loc) · 5.29 KB
/
cn_sqlite.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import sqlite3
from icecream import ic
import sqlite3
from sqlite3 import Error
class CnSQLite:
"""
SQlite handling class.
Structure:
{
connection: connection to SQlite,
path: path to SQlite DB,
create_connection: initialize connection method
}
"""
def __init__(self,
path,
):
self.connection = None
self.path = path
self.create_connection(path)
# INIT
def create_connection(self, path):
"""
Init SQlite connection.
Params:
path - path to sqlite database
"""
try:
self.connection = sqlite3.connect(path)
print("Connection to SQLite DB successful")
except Error as e:
print(f"The error '{e}' occurred")
# COMMON
def select_query(self, query):
cursor = self.connection.cursor()
try:
cursor.execute(query)
result = cursor.fetchall()
return result
except Error as e:
print(f"The error '{e}' occurred")
def executemany_query(self, query, many_list):
with self.connection:
try:
cur = self.connection.cursor()
cur.executemany(
query, many_list
)
self.connection.commit()
except Error as e:
print(f"The error '{e}' occurred")
def execute_query(self, query):
with self.connection:
try:
cur = self.connection.cursor()
cur.execute(query)
self.connection.commit()
except Error as e:
print(f"The error '{e}' occurred")
# LANG TABLE
def create_language_table(self):
query = """
CREATE TABLE IF NOT EXISTS language(
word_id INT PRIMARY KEY,
word_text TEXT);
"""
self.execute_query(query)
def insert_language_word(self, word):
query = "INSERT OR IGNORE INTO language(word_text) VALUES(?);"
with self.connection:
try:
cur = self.connection.cursor()
cur.execute(
query, (word,)
)
self.connection.commit()
except Error as e:
print(f"The error '{e}' occurred")
def select_language_words(self):
read_query = """
SELECT word_text FROM language
"""
return convert_tuples_list_to_string_list(self.select_query(read_query))
# ALPHABET TABLE
def create_alphabet_table(self):
query = """
CREATE TABLE IF NOT EXISTS alphabet(
char_id INTEGER PRIMARY KEY,
char TEXT,
frequency_in_lang FLOAT);
"""
self.execute_query(query)
def insert_alphabet_chars(self, alphabet_dict):
query = "INSERT OR IGNORE INTO alphabet(char) VALUES(?);"
self.executemany_query(query, alphabet_dict)
def update_alphabet_frequency(self, alphabet_dict):
query = "UPDATE alphabet SET frequency_in_lang=? WHERE char=?;"
self.executemany_query(query, alphabet_dict)
def select_all_alphabet_chars(self):
read_query = "SELECT char FROM alphabet"
return convert_tuples_list_to_string_list(self.select_query(read_query))
def select_alphabet_char_frequency(self, char):
read_query = "SELECT frequency_in_lang FROM alphabet WHERE char='" + char + "'"
return convert_tuples_list_to_string_list(self.select_query(read_query))
# LEXEMS TABLE
def create_lexems_table(self):
query = """
CREATE TABLE IF NOT EXISTS lexems(
lexem_id INTEGER PRIMARY KEY,
lexem TEXT,
lexem_length INTEGER,
lexem_count_in_language INTEGER);
"""
self.execute_query(query)
def insert_lexems(self, lexems_list):
"""
:param lexems_list: lexem,lexem_length,lexem_count_in_language
"""
query = "INSERT OR IGNORE INTO lexems(lexem,lexem_count_in_language,lexem_length) VALUES(?,?,?);"
self.executemany_query(query, lexems_list)
def update_lexems_table(self, lexems_list):
"""
:param lexems_list: [tuple(lexem_count_in_language,lexem_length,lexem)]
:return:
"""
query = """UPDATE lexems SET lexem_count_in_language=?,lexem_length=? WHERE lexem=?;"""
self.executemany_query(query, lexems_list)
def select_lexem_value(self, lexem: str, value_name):
"""
:param lexem:
:param value_name:
:return: list of strings
"""
query = "SELECT " + value_name + " FROM lexems where lexem='" + lexem + "'"
return convert_tuples_list_to_string_list(self.select_query(query))
###########
def db_test(self):
# self.create_language_table()
# self.create_alphabet_table()
# self.create_lexems_table()
pass
def convert_tuples_list_to_string_list(tuples_list):
string_list = []
for record in list(tuples_list):
string_list.append(list(record)[0])
return string_list
db = CnSQLite('cn_sqlite/crazynames.sqlite')
db.db_test()