· 6 years ago · Mar 07, 2019, 11:14 PM
1class Config:
2 def __init__(self, serverId=0, region="eu", language="en"):
3 self.serverId = serverId
4 self.region = region
5 self.language = language
6
7CREATE TABLE IF NOT EXISTS Config(
8ServerId BIGINT UNIQUE,
9region NVARCHAR(255),
10language NVARCHAR(255)
11);
12
13def getConfig(id, con=connect()):
14 try:
15 cursor = con.cursor()
16 sql = 'SELECT region,language FROM config WHERE ServerId = %s'
17 val = (id,)
18 cursor.execute(sql,val)
19 rows = cursor.fetchone()
20 if rows is None:
21 config = Config(serverId=id)
22 sql = 'INSERT INTO Config(ServerID,region,language) VALUES(%s,%s,%s)',
23 val = (int(config.serverId), str(config.region), str(config.language))
24 cursor.execute(sql,val)
25 con.commit()
26 con.close()
27 return config
28 else:
29 return Config(serverId=id, region=rows[0], language=rows[1])
30 except Exception as e:
31 log.writeLog("getConfig", str(e))
32
33config = Config(serverId=id)
34 sql = 'INSERT INTO Config(ServerID,region,language) VALUES(%s,%s,%s)',
35 val = (int(config.serverId), str(config.region), str(config.language))
36 cursor.execute(sql,val)
37
38def connect():
39 mydb = mysql.connector.connect(host=Secret.dbAddr,user=Secret.dbUser,passwd=Secret.dbPwd,database=Secret.dbName)
40 return mydb