Skip to content

Commit 94c1a91

Browse files
committed
chore: update table name
1 parent 3c47c79 commit 94c1a91

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

mysqlclient_example.py

+10-10
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ def get_mysqlclient_connection(autocommit: bool = True) -> MySQLdb.Connection:
3939
def mysqlclient_recreate_table() -> None:
4040
with get_mysqlclient_connection() as connection:
4141
with connection.cursor() as cur:
42-
cur.execute("DROP TABLE IF EXISTS player;")
42+
cur.execute("DROP TABLE IF EXISTS players;")
4343
cur.execute(
4444
"""
45-
CREATE TABLE player (
45+
CREATE TABLE players (
4646
`id` VARCHAR(36),
4747
`coins` INTEGER,
4848
`goods` INTEGER, PRIMARY KEY (`id`)
@@ -52,25 +52,25 @@ def mysqlclient_recreate_table() -> None:
5252

5353

5454
def create_player(cursor: Cursor, player: tuple) -> None:
55-
cursor.execute("INSERT INTO player (id, coins, goods) VALUES (%s, %s, %s)", player)
55+
cursor.execute("INSERT INTO players (id, coins, goods) VALUES (%s, %s, %s)", player)
5656

5757

5858
def get_player(cursor: Cursor, player_id: str) -> tuple:
59-
cursor.execute("SELECT id, coins, goods FROM player WHERE id = %s", (player_id,))
59+
cursor.execute("SELECT id, coins, goods FROM players WHERE id = %s", (player_id,))
6060
return cursor.fetchone()
6161

6262

6363
def get_players_with_limit(cursor: Cursor, limit: int) -> list[tuple]:
64-
cursor.execute("SELECT id, coins, goods FROM player LIMIT %s", (limit,))
64+
cursor.execute("SELECT id, coins, goods FROM players LIMIT %s", (limit,))
6565
return cursor.fetchall()
6666

6767

6868
def bulk_create_player(cursor: Cursor, players: list[tuple]) -> None:
69-
cursor.executemany("INSERT INTO player (id, coins, goods) VALUES (%s, %s, %s)", players)
69+
cursor.executemany("INSERT INTO players (id, coins, goods) VALUES (%s, %s, %s)", players)
7070

7171

7272
def get_count(cursor: Cursor) -> None:
73-
cursor.execute("SELECT count(*) FROM player")
73+
cursor.execute("SELECT count(*) FROM players")
7474
return cursor.fetchone()[0]
7575

7676

@@ -111,22 +111,22 @@ def simple_example() -> None:
111111
def trade(connection: MySQLdb.Connection, sell_id: str, buy_id: str, amount: int, price: int) -> None:
112112
# This function should be called in a transaction.
113113
with connection.cursor() as cursor:
114-
cursor.execute("SELECT coins, goods FROM player WHERE id = %s FOR UPDATE", (sell_id,))
114+
cursor.execute("SELECT coins, goods FROM players WHERE id = %s FOR UPDATE", (sell_id,))
115115
_, sell_goods = cursor.fetchone()
116116
if sell_goods < amount:
117117
print(f"sell player {sell_id} goods not enough")
118118
connection.rollback()
119119
return
120120

121-
cursor.execute("SELECT coins, goods FROM player WHERE id = %s FOR UPDATE", (buy_id,))
121+
cursor.execute("SELECT coins, goods FROM players WHERE id = %s FOR UPDATE", (buy_id,))
122122
buy_coins, _ = cursor.fetchone()
123123
if buy_coins < price:
124124
print(f"buy player {buy_id} coins not enough")
125125
connection.rollback()
126126
return
127127

128128
try:
129-
update_player_sql = "UPDATE player set goods = goods + %s, coins = coins + %s WHERE id = %s"
129+
update_player_sql = "UPDATE players set goods = goods + %s, coins = coins + %s WHERE id = %s"
130130
# deduct the goods of seller, and raise his/her the coins
131131
cursor.execute(update_player_sql, (-amount, price, sell_id))
132132
# deduct the coins of buyer, and raise his/her the goods

0 commit comments

Comments
 (0)