-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnbarBase.py
116 lines (95 loc) · 3.49 KB
/
AnbarBase.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
import numbers
import mysql.connector
from mysql.connector import ProgrammingError
import sys
from Khadang import *
from DegJet import *
from StaticsBase import *
import traceback
class AnbarBase(DegJet):
def connect(self):
try:
self.conn = mysql.connector.connect(host=self.host, port=self.port, user=self.user, password=self.password,
database=self.database)
if self.conn.is_connected():
print('Connected to MySQL database')
else:
print('be ga raft')
assert ('Khiar: Connection Nashod')
except ProgrammingError as e:
sexyError(e)
def __init__(self,host='192.168.5.17',user='c',password='22111357',port=3306,database='Moozmar'):
super().__init__()
self.database = database
self.host=host
self.port=port
self.user=user
self.password=password
self.stmt=''
self.params=''
# self.connect()
def setParams(self,params=UNIN):
if params==UNIN or params=='' or params==None :
self.params =UNIN
elif isinstance(params, numbers.Number):
self.params=(params,)
elif isinstance(params,str):
self.params =(params,)
elif isinstance(params,list):
self.params=tuple(params)
elif isinstance(params,dict):
self.params=tuple(params.values())
elif isinstance(params,tuple):
self.params=params
return self.params
def setStmt(self,stmt=''):
self.stmt=stmt
return self.stmt
def exec(self,stmt='',params=UNIN,all=0):
self.setStmt( stmt)
self.setParams(params)
ret=''
try:
cursor = self.conn.cursor(prepared=True)
if self.params==('<!NO!>',) or self.params=='<!NO!>':
ret = cursor.execute(self.stmt)
ret = cursor.fetchall()
else:
print(self.stmt, self.params)
ret = cursor.execute(self.stmt, self.params)
ret = cursor.fetchall()
self.conn.commit()
cursor.close()
except Exception as e:
print(traceback.format_exc())
sexyError(e)
if (isinstance(ret,list)):
if len(ret)==1:
print
return ret
def bezar(self,table,colnames,vals):
self.setParams(vals)
if isinstance(colnames, list):
colnames=virgool(colnames)
valPlaceHolders=berin(len(vals),',%s',v=0)[1:]
else:
colnames = colnames
valPlaceHolders = '%s'
self.stmt = INSRTCNDTION.format(TABLE=table, COLNAMES=colnames, VALUES=valPlaceHolders)
ret =self.exec(self.stmt,self.params)
return ret
def begir(self,table,colnames,conCol=UNIN,condition=UNIN,limit=1):
if isinstance(colnames, list):
colnames = virgool(colnames, sql=1)
else:
colnames = mySQLTypedFormat(colnames)
if condition==UNIN:
self.stmt=stmtSlctNoCndtion.format(TABLE=table, COLNAMES=colnames)
self.params=''
ret = self.exec(self.stmt)
else:
self.stmt = stmtSlctCndtion.format(TABLE=table, COLNAMES=colnames, CONDITION='%s',CONCOL=conCol)
self.setParams(condition)
print(self.stmt)
ret = self.exec(self.stmt, self.params)
return ret