Get the App
SLTechnology News&Howtos  ›  Database  › 

Record a piece of python code (modify database string)

Shulou Source: shulou.com Published: 2022-06-01 13:44:05 10月03日 Update

Scene: due to the development of disgusting account login settings, and in view of the habits of Japanese players, they have just begun to log in to the game as tourists. If there is a change of package or equipment, the account ID needs to be replaced from the database. At first, it is manual, and then py is implemented, encapsulated as exe, which is directly executed in dos. The best way to improve is, of course, to meet with web with one click, and now the code runs away as a record.

# coding=utf-8import pymysqlimport tracebackimport simplejsonimport sys# Exchange def exchange (connect_index, file_name): try: # read database configuration fp = open ("config.json") connect_info = simplejson.load (fp) ["db_list"] fp.close () # create link conn = pymysql.connect (connect_ in [connect _ index] ["ip"]) Port=int (connect_ InfoConnect [index] ["port"]), user=str (connect_ InfoConnect [connect _ index] ["user"]), passwd=str (connect_ InfoConnect [connect _ index] ["passwd"]), db=str (connect_ InfoConnect [connect _ index] ["db_name"]) Charset='utf8') # get cursor cur = conn.cursor () # read swap list fp = open (str (file_name)) exchange_data = simplejson.load (fp) fp.close () # start exchanging for item in exchange_data: # Read account_id player_datas = [] for i in range (2): temp_data = {} cur.execute ("select `id`) for two values `account_ id` from `room`where `"+ str (item [" key "] [I]) +"` ='"+ str (item [" value "] [I]) +"'") result = cur.fetchall () temp_data [" id "] = result [0] [0] temp_data ["account_id"] = result [0] [1] player_datas.append (temp_data) # change account_id to avoid conflicts for player_data in player_datas: cur.execute ("update `room`set `account_ id` ='" + str (player) _ data ["account_id"]) + "_ 'where `id` ='" + str (player_data ["id"]) + "'") conn.commit () # Exchange temp_data = player_datas [0] ["account_id"] player_datas [0] ["account_id"] = player_datas [1] ["account_id"] player _ datas [1] ["account_id"] = temp_data for player_data in player_datas: cur.execute ("update `room`set `account_ id` ='" + str (player_data ["account_id"]) + "'where `id` ='" + str (player_data ["id"]) + "') conn.commit () conn.close () ) except Exception: traceback.print_exc () if _ _ name__ ='_ _ main__': try: if len (sys.argv) < 3: print "arg error" sys.exit (1) exchange (int (sys.argv [1])) Sys.argv [2]) except Exception: traceback.print_exc ()

Some of the json files haven't been sorted out yet. If you have any questions, ask the blogger, and he won't tell you anyway.

Tags: Data database account login code disgusting two cursor scene manual file way is in best tourists players users questions devices identity Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Shulou Technology vpn Apple MariaDB