Skip to content
Advertisement

How to create a leaderboard as a database

I need to create a database which is a top 5 leaderboard, however, what I tried didn’t work – it brought up many different errors (code below):

import sqlite3

winner_name = input("Name: ")
winner_score = input("Score: ")

db = sqlite3.connect('C:UsersuserDesktopWORKCSNEAPython')

c = db.cursor()

c.execute("""CREATE TABLE Leaderboard
(Place,text,
Name, text,
Score, text)
""")

c.execute("""INSERT INTO Leaderboard
             VALUES ("1",winner_name,winner_score)""")

db.commit()
c.execute('SELECT * FROM Leaderboard')
row = c.fetchone()
print(row)
db.close()

Problems:

  1. The 4th line beginning “db = ” comes up with an error on the brackets. I initially thought it had to do with the fact that I am trying to make a database and the program got confused, however, I am not sure.

  2. On the line about halfway through beginning “VALUES”, I want to make winner_name and winner_score the variable from the start of the code, but I am not sure if I have done it right or wrong (it is highlighted green like speech not black).

Thank you for your help.

Advertisement

Answer

take a look on next things

  1. sqlite3.connect should receive db file name , for example example.db (not a folder as in your case)

  2. creating database have a next syntax, in your case comma is unnecessary between Place andtext: Place,next (wrong) -> Place text (correct)

  3. in inserting into table line it needs to pass values (in your case you are passing string):

c.execute(f"""INSERT INTO Leaderboard
             VALUES ("1","{winner_name}",{winner_score})""")

(make a notice to put quotes " for winner_name, because db awaiting text for this value)

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement