def sync_tournament(): """ Synchronize tournament/Users from vchess/Users : update """ uid = sys.argv[1] vconn = create_connection(vchess_db_path) vcur = vconn.cursor() vcur.execute("SELECT name,email FROM Users WHERE id = ?", (uid, )) vrow = vcur.fetchone() vcur.close() tconn = create_connection(tournament_db_path) tcur = tconn.cursor() tcur.execute("SELECT name,email FROM Users WHERE id = ?", (uid, )) trow = tcur.fetchone() if trow[0] != vrow[0]: tconn.cursor().execute("UPDATE Users SET name = ? WHERE id = ?", (vrow[0], uid)) if trow[1] != vrow[1]: tconn.cursor().execute("UPDATE Users SET email = ? WHERE id = ?", (vrow[1], uid)) tcur.close() tconn.commit()
def extract_symbols_from_db(remove_stocks_list): # Create a db connection and extract the list of symbols. stocks_to_trade = select_all_tasks(create_connection('pirend'), 'Algo') # remove the unwanted stocks from the list. for stock_name in stocks_to_trade: if stock_name in remove_stocks_list: stocks_to_trade.remove(stock_name) # Create a db connection and extract the list of symbols to short. stocks_to_short = select_all_tasks(create_connection('pirend'), 'Short') return stocks_to_trade, stocks_to_short
def sync_tournament(): """ Synchronize tournament/Users from vchess/Users : create """ vconn = create_connection(vchess_db_path) vcur = vconn.cursor() vcur.execute("SELECT id,name,email,created,notify FROM Users") vrows = vcur.fetchall() vcur.close() tconn = create_connection(tournament_db_path) tcur = tconn.cursor() tcur.execute("DELETE FROM Users") for vuser in vrows: tcur.execute( "INSERT INTO Users (id,name,email,created,notify) VALUES (?,?,?,?,?)", vuser) tconn.commit()
def sync_tournament(): """ Synchronize tournament/Users from vchess/Users : insert """ vconn = create_connection(vchess_db_path) vcur = vconn.cursor() vcur.execute("SELECT name,email,created,notify FROM Users WHERE id = ?", (sys.argv[1], )) vrow = vcur.fetchone() vcur.close() tconn = create_connection(tournament_db_path) tcur = tconn.cursor() tcur.execute( "INSERT INTO Users (id,name,email,created,notify) VALUES (?,?,?,?,?)", (sys.argv[1], ) + vrow) tcur.close() tconn.commit()
def sync_gamestat(): """ (Incrementally) Synchronize GameStat table from Variants update """ conn = create_connection() cur = conn.cursor() cur.execute("SELECT max(vid) FROM GameStat") vid_max = cur.fetchone()[0] or 0 cur.execute("SELECT id FROM Variants WHERE id > ?", (vid_max, )) rows = cur.fetchall() for variant in rows: cur.execute("INSERT INTO GameStat(vid) VALUES (?)", (variant[0], )) conn.commit() cur.close()
def delete_users(): """ Remove users corresponding to given IDs (delete) """ tconn = create_connection(tournament_db_path) tcur = tconn.cursor() # https://stackoverflow.com/a/52479382 id_list = list(map(int, sys.argv[1])) query = "DELETE FROM Users WHERE id IN ({})".format(", ".join( "?" * len(id_list))) tcur.execute(query, id_list) # Failure: why? #tcur.executemany("DELETE FROM Users WHERE id = ?", id_list ) # Open to SQL injection: #tcur.execute("DELETE FROM Users WHERE id IN (" + sys.argv[1] + ")") tconn.commit() tcur.close()
def delete_one(param, collection_name): my_collection = create_connection(collection_name) my_collection.delete_one(param)
def read_all(collection_name): my_collection = create_connection(collection_name) for doc in my_collection.find(): print(doc)
def create_doc(doc, collection_name): my_collection = create_connection(collection_name) password = hash_password(doc['pass'].encode()) doc['pass'] = password my_collection.insert_one(doc)
first_arg = sys.argv[1] def csv_download(fname, result): """This funtion is used for downloading result as a csv file """ with open('{bd}/{fn}.csv'.format(bd=basedir, fn=fname), 'w') as file: writer = csv.writer(file, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL) for sub_list in result: rows = sub_list writer.writerow(rows) if __name__ == '__main__': database = os.path.join(basedir, 'data.db') # create a database connection conn = create_connection(database) with conn: print("Query all weather") result = select_weather_all(conn) csv_download(first_arg, result)