I have a function getMoverList() that needs to SELECT Names from a sorted sqlite3 column:
def getMoverList():
conn = get_db_connection()
cur = conn.cursor()
cur.execute('SELECT Name, CAST (Points AS int) as Points, FROM table ORDER BY Points DESC')
moverList = []
for Name in cur.fetchall():
playerName = str(Name[0])
moverList.append(compareRanks(playerName))
return moverList
conn.close()
The function has to get the Names of the db column, pass each Name to another function compareRanks(playerName) to get a wanted value (RankingDifference) and make another sorted list with wanted values.
The above code works but is pretty slow. I found alternative Python is slow when iterating over a large list that makes a way faster list:
def getMoverList():
conn = get_db_connection()
moverList = conn.execute('SELECT Name, CAST (Points AS int) as Points, FROM table ORDER BY Points DESC').fetchall()
return moverList
conn.close()
The problem is that moverList now doesnt contain strings with names but 'sqlite3.Row' objects. Tried things like
mover = getMoverList()
print (mover[0].Name)
but "'sqlite3.Row' object has no attribute 'Name'" How to do this?
Edit: Using sqlite3.row here:
def get_db_connection():
conn = sqlite3.connect('db.db')
conn.row_factory = sqlite3.Row
return conn
Edit2: getLastRank(): Here I pass a player Name to the function and want to have a look at my table for LastRank of previous backupped db. I dont have ranks saved in the db table. Instead I initially worked with flasks loop.index to show rankings and now that I want to work with it I figured out to get a rank with ROW COUNT. If I put conn.close() before the for loop with return I get 'sqlite3.ProgrammingError: Cannot operate on a closed database.'
def getLastRank(playerName):
conn = get_db_previous()
player = conn.execute("SELECT ROW_NUMBER () OVER (ORDER BY Points DESC) Rank, Name, Points FROM alltime").fetchall()
for item in player:
if playerName in item:
return item[0]
conn.close()