charlie s
charlie s

Reputation: 93

How to add a second table using sqlite after keep getting sqlite error

I am trying to create a database to link with a game I am making but I keep getting the error of: sqlite3.OperationalError: near "/": syntax error. The database works fine with the first table but when i added the second table it wont run and will get that error. Any ideas of how to fix this problem?

This is how I make the database:

class CreateDB:
    def __init__(self):
        self.conn = sqlite3.connect("TRON.db")
        self.c = self.conn.cursor()

    def createTables(self, Tables):
        for tableName, field in Tables.items():
            self.c.execute('CREATE TABLE IF NOT EXISTS ' + tableName + ' (' + field + ')')
            self.conn.commit()

def main():
    """main function"""

    db = CreateDB()
    Tables = {"AccountDetails": '''PlayerID integer,
                                Username text,
                                Password text,
                                primary key (PlayerID)''',
              "Player": '''PlayerID integer,
              Username text,
              TotalWins integer,
              TotalLosess integer,
              TotalGames integer,
              TotalPoints integer,
              Win/lose_Ratio real,
              primary key (PlayerID)'''}

    db.createTables(Tables)

main()

Upvotes: 0

Views: 27

Answers (1)

Serge Ballesta
Serge Ballesta

Reputation: 149075

The problem in in the Win/lose_Ratio field. As it contains a / that can represent an operator, it has to be quoted to be used in a field name:

Tables = {"AccountDetails": '''PlayerID integer,
                            ...
                            primary key (PlayerID)''',
          "Player": '''PlayerID integer,
          ...
          TotalPoints integer,
          "Win/lose_Ratio" real,
          primary key (PlayerID)'''}

Upvotes: 1

Related Questions