SQLite, Python: моя функция обновления чередует некоторые столбцы - PullRequest
0 голосов
/ 15 марта 2020

Так вот мой код, он довольно прямолинеен

import sqlite3
import json

result_out = {}
conn = sqlite3.connect('dvatsitodin_db')
c = conn.cursor()

new_table = input('Do you want a new table? (y/n): ', )

if new_table == 'y':
    c.execute("""CREATE TABLE games (
            Game_number text,
            Player_finished text,
            Players_cards blob,
            Players_total text,
            Dealer_finished text,
            Dealers_cards blob,
            Dealers_total text,
            Game_total text,
            Twentyone_Scored text,
            Rasdacha text,
            Gold text,
            Draw text)""")
    conn.commit()


def insert_game(game):
    c.execute("""INSERT INTO games VALUES (:Game_number, :Players_total, :Player_finished, :Players_cards,
                          :Dealers_total, :Dealers_cards, :Dealer_finished, :Game_Total, :Twentyone_Scored, :Rasdacha, :Gold, 
                          :Draw)""",
              {'Game_number': game['Game_number'],
               'Players_total': game['Players_total'],
               'Player_finished': game['Player_finished'],
               'Players_cards': json.dumps(game['Players_cards']),
               'Dealers_total': game['Dealers_total'],
               'Dealers_cards': json.dumps(game['Dealers_cards']),
               'Dealer_finished': game['Dealer_finished'],
               'Game_Total': game['Game_Total'],
               'Twentyone_Scored': game['Twentyone_Scored'],
               'Rasdacha': game['Rasdacha'],
               'Gold': game['Gold'],
               'Draw': game['Draw']})


def get_game(game_number):
    c.execute("SELECT * FROM games WHERE Game_number=:Game_number", {'Game_number': game_number})
    return c.fetchone()


def update_game(game_2):
    with conn:
        c.execute("""UPDATE games SET Game_number=:Game_number, Players_total=:Players_total,
                    Player_finished=:Player_finished, Players_cards=:Players_cards, Dealers_total=:Dealers_total,
                     Dealers_cards=:Dealers_cards, Dealer_finished=:Dealer_finished, Game_Total=:Game_Total,
                      Twentyone_Scored=:Twentyone_Scored, Rasdacha=:Rasdacha, Gold=:Gold, Draw=:Draw WHERE 
                      Game_number=:Game_number""",
                  {'Game_number': game_2['Game_number'],
                   'Players_total': game_2['Players_total'],
                   'Player_finished': game_2['Player_finished'],
                   'Players_cards': json.dumps(game_2['Players_cards']),
                   'Dealers_total': game_2['Dealers_total'],
                   'Dealers_cards': json.dumps(game_2['Dealers_cards']),
                   'Dealer_finished': game_2['Dealer_finished'],
                   'Game_Total': game_2['Game_Total'],
                   'Twentyone_Scored': game_2['Twentyone_Scored'],
                   'Rasdacha': game_2['Rasdacha'],
                   'Gold': game_2['Gold'],
                   'Draw': game_2['Draw']})


trial = {'Game_number': '5', 'Players_total': '15', 'Player_finished': 'Yes', 'Players_cards': ['J♦', 'Q♣', '10♦'],
         'Dealers_total': '8', 'Dealers_cards': ['K♦', 'K♣'], 'Dealer_finished': 'Yes', 'Game_Total': '32',
         'Twentyone_Scored': '-', 'Rasdacha': '-', 'Gold': '-', 'Draw': '-'}
trial_2 = {'Game_number': '5', 'Players_total': '20', 'Player_finished': 'Yes', 'Players_cards': ['Q♦', 'Q♣', '10♦'],
         'Dealers_total': '18', 'Dealers_cards': ['J♦', 'K♣'], 'Dealer_finished': 'Yes', 'Game_Total': '32',
         'Twentyone_Scored': '-', 'Rasdacha': '-', 'Gold': '-', 'Draw': '-'}
insert_game(game=trial)
go_1 = get_game(game_number='5')
print(go_1)

update_game(game_2=trial_2)
go = get_game(game_number='5')
print(go)

Вот вывод, когда я запускаю код:

Do you want a new table? (y/n): y
('5', '15', 'Yes', '["J\\u2666", "Q\\u2663", "10\\u2666"]', '8', '["K\\u2666", "K\\u2663"]', 'Yes', '32', '-', '-', '-', '-')
('5', 'Yes', '["Q\\u2666", "Q\\u2663", "10\\u2666"]', '20', 'Yes', '["J\\u2666", "K\\u2663"]', '18', '32', '-', '-', '-', '-')

Process finished with exit code 0

Вот что я ожидаю от второго отпечатка заявление после обновления

('5', '20', 'Yes', '["Q\\u2666", "Q\\u2663", "10\\u2666"]', '18', '["J\\u2666", "K\\u2663"]', 'Yes', '32', '-', '-', '-', '-')

Я просмотрел документацию Python SQLite, чтобы понять, что происходит, но не смог ничего объяснить. Потратил всю ночь на это, это отчасти расстраивает. Есть ли причина для этого ошибочного результата? Как я могу решить эту проблему?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...