forked from tmotagam/sqlite-electron
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsqlite.py
More file actions
147 lines (128 loc) · 4.54 KB
/
sqlite.py
File metadata and controls
147 lines (128 loc) · 4.54 KB
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
'''
sqlite-electorn server executing the sql queries of the nodejs/electron processes
Copyright (C) 2022 Motagamwala Taha Arif Ali
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
'''
import sys, json, sqlite3
def connect(db):
'''
This is an Internal function used to connect to the database specified by the nodejs Process
It takes the path of the database as parameter and returns true on connecting or returns error on exception
'''
try:
conn = sqlite3.connect(db)
return conn
except Exception as e:
return 'Error: ' + str(e)
def executeQuery(db, sql, fetch, values):
'''
This the function for executing the queries sent by the nodejs process and return true or arrays or error on exceptions
'''
conn = connect(db)
try:
if fetch == 'all':
if type(values) is not list or values == []:
cursor = conn.execute(sql)
data = cursor.fetchall()
conn.commit()
conn.close()
return data
else:
cursor = conn.execute(sql, (values))
data = cursor.fetchall()
conn.commit()
conn.close()
return data
if fetch == '1':
if type(values) is not list or values == []:
cursor = conn.execute(sql)
data = cursor.fetchone()
conn.commit()
conn.close()
return data
else:
cursor = conn.execute(sql, (values))
data = cursor.fetchone()
conn.commit()
conn.close()
return data
if fetch == '':
if type(values) is not list or values == []:
cursor = conn.execute(sql)
conn.commit()
conn.close()
return True
else:
cursor = conn.execute(sql, (values))
conn.commit()
conn.close()
return True
else:
if type(values) is not list or values == []:
cursor = conn.execute(sql)
data = cursor.fetchmany(int(fetch))
conn.commit()
conn.close()
return data
else:
cursor = conn.execute(sql, (values))
data = cursor.fetchmany(int(fetch))
conn.commit()
conn.close()
return data
except Exception as e:
return 'Error: ' + str(e)
def executeMany(db, sql, values):
'''
This function executes single query on multiple value arrays return true or return error on exception
'''
conn = connect(db)
try:
conn.executemany(sql, (values))
conn.commit()
conn.close()
return True
except Exception as e:
return 'Error: ' + str(e)
def executeScript(db, sqlScript):
'''
This function executes sql scripts and returns true on success or error on exception
'''
conn = connect(db)
try:
with open(sqlScript, 'r') as sql_file:
sql = sql_file.read()
conn.executescript(sql)
conn.commit()
conn.close()
return True
except Exception as e:
try:
conn.executescript(sqlScript)
conn.commit()
conn.close()
return True
except Exception as e:
return 'Error: ' + str(e)
def main():
'''
The main driver function reading from the input send by nodejs process and executing the sql queries on the database returning the data in JSON format
'''
lines = sys.stdin.readlines()
why = json.loads(lines[0])
if why[0] == 'executeQuery':
print(json.dumps(executeQuery(why[1], why[2], why[3], why[4])))
elif why[0] == 'executeMany':
print(json.dumps(executeMany(why[1], why[2], why[3])))
elif why[0] == 'executeScript':
print(json.dumps(executeScript(why[1], why[2])))
main()