像这样声明变量有什么问题吗?
所以这里有两个问题。
在我的数据库中,我需要 18 个字段,因为高尔夫有 18 个洞(因此每个洞需要存储 18 个击球数。) 其次,我需要存储每个洞的码数(所以还有 18 个洞。)
我的问题是,现在我将它们全部声明如下:
public static final String KEY_H1 = "h1";
public static final String KEY_H1Y = "h1y";
public static final String KEY_H2 = "h2";
public static final String KEY_H2Y = "h2y";
public static final String KEY_H3 = "h3";
public static final String KEY_H3Y = "h3y";
等
h1 是洞 1 h1y 为 1 洞码数
有更好的方法吗?此外,在创建时我必须调用 h1 h1y h2 h2y h3 h3y 等。
提前致谢!
编辑: 1.这会给系统带来很大的负担,导致应用程序崩溃或运行速度变慢吗? 2、以后更新数据库会不会很痛苦?
So there are sort of 2 questions here.
In my database I need 18 fields because in golf there are 18 holes (so 18 to store hits for each hole.) Second I need to store the yardage for each hole (so thats 18 more.)
My question is, right now I am declaring them all as follows:
public static final String KEY_H1 = "h1";
public static final String KEY_H1Y = "h1y";
public static final String KEY_H2 = "h2";
public static final String KEY_H2Y = "h2y";
public static final String KEY_H3 = "h3";
public static final String KEY_H3Y = "h3y";
etc.
h1 being hole 1
h1y being hole 1 yardage
Is there a better way to do this? Additionally, in the on create I will have to call h1 h1y h2 h2y h3 h3y etc.
Thanks in advance!
EDIT: 1. Will this tax the system a lot in terms of, cause the application to bomb out or run slower?
2. In the future, will this make it a pain to update the database?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用“洞得分”表来代替“高尔夫比赛得分”表。然后,该表可以包含一个表示洞的 int 列、另一个表示码数的列以及另一个 game_id 列。
You could instead of having a "golf game score" table, have a "hole score" table. This table could then contain an int column for the hole, another column for yardage, and another game_id column.
针对Kabuku的答案:假设对于给定的一轮对于高尔夫,您想了解球员有多少只小鸟、老鹰、柏忌等;或者你想找出哪些洞的标准杆得分最高,哪些洞的得分最差,等等。你真的想将相同的代码编写 18 次,每个洞一次吗?或者您是否希望能够编写一次代码,作为循环或对漏洞的查询,以找到结果?
这只是您非常非常希望将孔号作为参数而不是为每个孔创建单独的静态变量或列的原因之一。
要回答问题的另一部分——声明大量公共静态最终变量是否会减慢系统速度——答案是否定的。但在这种特殊情况下,它最终会减慢你的速度。
Tacking onto Kabuku's answer: Suppose for a given round of golf, you want to find out how many birdies, eagles, bogies, etc. the player had; or you want to find out which holes had the best score against par, which had the worst, etc. Do you really want to write the same code 18 times, once for each hole; or do you want to be able to write the code once, as a loop or query over the holes, to find the result?
This is just one reason why you really, really want to make the hole number a parameter, instead of creating separate static variables or columns for each hole.
To answer the other part of your question -- would it slow down the system to declare a lot of public static final variables -- the answer is no. But in this particular case, it would eventually slow you down.
我只是将其存储为字符串,因为您有从 x 到 y 的字段,这些字段应该始终采用某种数组形式。我认为最简单的方法是将其存储为:
hits:yardage attempts:yardage attempts:yardage
etc...
在您的数据库中,然后用
:
和分割字符串。
(根据您的编辑,是的,您当前的方式可能会使更新数据库结构变得不必要的困难。)
I would just store it as a string, since you have fields for something from x to y, which should always be in some sort of array form. The easiest way, I think, is storing it as:
hits:yardage hits:yardage hits:yardage
etc...
In your database, then splitting the string by
:
and.
(And in response to your edit, yes, your current way will probably make it unnecessarily difficult to update the database structure.)
我有两张桌子,一张游戏桌和一张洞记分表。
游戏桌上会有日期和课程。
洞得分表将具有洞号、码数、得分以及比赛表中比赛记录的_id。
I'd have two tables, a game table, and a hole score table.
The game table would have the date and course.
The hole score table would have the hole number, the yardage, the score and the _id of the record for the game in the game table.