如何通过主键从数据库提取数据,在Delphi中实现胜者姓名写入tblGames
问题:通过玩家ID获取姓名并写入比赛表Winner列
我有一个包含tblGames和tblPlayers两个表的数据库,想要仅通过ID(主键)提取玩家的姓名。我已经梳理出逻辑:判断比赛胜负得到胜者ID,再从tblPlayers中获取该ID对应的姓名与姓氏,组合为WinnerName后写入tblGames的Winner列。但我对Delphi和数据库操作不太熟悉,没法正确实现这个逻辑,附上我的逻辑代码和Delphi代码片段,寻求解决方法。
梳理的逻辑代码
if P1Score > P2Score then winner := P1ID else winner := P2ID winner in tblGames = the ID of winner in tblPlayers WinnerName := first name of Winner + surname of Winner in tblPlayers
Delphi代码片段
BEGIN if (StrToInt(P1_score) - StrToInt(P2_score) = 0) then Draw := True else Draw:= False; if StrtoInt(P1_Score) > StrToInt(P2_Score) then winnerID := P1_ID else winnerID := P2_ID; with dmTournament do begin tblGames.Insert; tblGames['Player1_Id'] := StrToInt(P1_ID); tblGames['Player2_ID'] := StrToInt(P2_ID); tblGames['Player1_score'] := StrToInt(P1_Score); tblGames['Player2_Score'] := StrToInt(P2_Score); tblGames['Draw'] := Draw; tblGames['Winner'] := WinnerName; //How do I get WinnerName(?) tblGames.Post; end; END;
解决方法
要获取WinnerName,可以通过查询tblPlayers表匹配胜者ID,下面提供两种常用实现方式:
方式一:用TTable的Locate方法查找
BEGIN // 保留原有的平局与胜者ID判断逻辑 var P1ScoreInt, P2ScoreInt: Integer; // 用TryStrToInt避免非数字输入导致崩溃 if not (TryStrToInt(P1_Score, P1ScoreInt) and TryStrToInt(P2_Score, P2ScoreInt)) then begin ShowMessage('分数输入无效,请输入数字'); Exit; end; Draw := (P1ScoreInt = P2ScoreInt); if P1ScoreInt > P2ScoreInt then winnerID := P1_ID else winnerID := P2_ID; with dmTournament do begin var WinnerName: string; if not Draw then begin var WinnerIDInt: Integer; if TryStrToInt(winnerID, WinnerIDInt) and tblPlayers.Locate('ID', WinnerIDInt, [loCaseInsensitive]) then begin // 替换为你实际的姓名、姓氏字段名 WinnerName := tblPlayers['FirstName'].AsString + ' ' + tblPlayers['Surname'].AsString; end else begin WinnerName := '未知玩家'; end; end else begin WinnerName := '平局'; // 平局场景可根据业务需求调整内容 end; // 插入比赛记录 tblGames.Insert; tblGames['Player1_Id'] := P1ScoreInt; tblGames['Player2_ID'] := P2ScoreInt; tblGames['Player1_score'] := P1ScoreInt; tblGames['Player2_Score'] := P2ScoreInt; tblGames['Draw'] := Draw; tblGames['Winner'] := WinnerName; tblGames.Post; end; END;
方式二:用SQL查询获取姓名
如果更习惯SQL操作,可以用查询语句直接获取:
BEGIN // 保留原有的平局与胜者ID判断逻辑 var P1ScoreInt, P2ScoreInt: Integer; if not (TryStrToInt(P1_Score, P1ScoreInt) and TryStrToInt(P2_Score, P2ScoreInt)) then begin ShowMessage('分数输入无效,请输入数字'); Exit; end; Draw := (P1ScoreInt = P2ScoreInt); if P1ScoreInt > P2ScoreInt then winnerID := P1_ID else winnerID := P2_ID; with dmTournament do begin var WinnerName: string; if not Draw then begin var WinnerIDInt: Integer; if TryStrToInt(winnerID, WinnerIDInt) then begin // 假设使用ADOQuery组件,替换为你实际的查询组件 with ADOQuery1 do begin Close; SQL.Text := Format('SELECT FirstName, Surname FROM tblPlayers WHERE ID = %d', [WinnerIDInt]); Open; if not Eof then WinnerName := FieldByName('FirstName').AsString + ' ' + FieldByName('Surname').AsString else WinnerName := '未知玩家'; Close; end; end else begin WinnerName := '未知玩家'; end; end else begin WinnerName := '平局'; end; // 插入比赛记录 tblGames.Insert; tblGames['Player1_Id'] := P1ScoreInt; tblGames['Player2_ID'] := P2ScoreInt; tblGames['Player1_score'] := P1ScoreInt; tblGames['Player2_Score'] := P2ScoreInt; tblGames['Draw'] := Draw; tblGames['Winner'] := WinnerName; tblGames.Post; end; END;
注意事项
- 请将代码中的
FirstName、Surname、ID替换为你数据库表中实际的字段名。 - 平局场景下
Winner字段的内容可根据业务需求调整,比如设为空字符串。 - 始终用
TryStrToInt替代StrToInt,避免非数字输入导致程序崩溃。
内容的提问来源于stack exchange,提问作者KindHeliCoptr
相关产品推荐
相关产品推荐

