如何在 Android 中干净地处理多个数据库表?

发布于 2024-11-07 20:02:32 字数 1062 浏览 0 评论 0原文

我正在尝试创建一个 Android 应用程序,可以处理玩过的游戏及其分数的历史记录,但当我从记事本教程展开时,这变成了一个重复语句的老鼠巢,如下所示:

private static final String GAME_TABLE_CREATE =
"create table " + GAME_TABLE + " (_id integer primary key autoincrement, " 
+ "title text not null);";
private static final String PLAY_TABLE_CREATE =
    "create table " + PLAY_TABLE + " (_id integer primary key autoincrement, " 
    + "game_id integer, game_date date);";
private static final String PLAYER_TABLE_CREATE =
    "create table " + PLAYER_TABLE + " (_id integer primary key autoincrement, " 
    + "name text not null);";
private static final String SCORE_TABLE_CREATE =
    "create table " + SCORE_TABLE + " (_id integer primary key autoincrement, " 
    + "game_id int, player_id int, score int);";

   ...

public void onCreate(SQLiteDatabase db) {
db.execSQL(GAME_TABLE_CREATE);
db.execSQL(PLAY_TABLE_CREATE);
db.execSQL(PLAYER_TABLE_CREATE);
db.execSQL(SCORE_TABLE_CREATE);
}

这对于可读性来说似乎是一场噩梦和可维护性。关于如何更好地管理多个 SQL 表,并将这些类型的列表变成漂亮的干净循环,有什么建议吗?我正在考虑尝试通过资源字符串数组来做到这一点,但一直无法弄清楚如何管理它。

I'm trying to create an Android app that can handle a history of played games and their scores, but as I expand from the Notepad tutorial, this is turning into a rat's nest of repeated statements like this:

private static final String GAME_TABLE_CREATE =
"create table " + GAME_TABLE + " (_id integer primary key autoincrement, " 
+ "title text not null);";
private static final String PLAY_TABLE_CREATE =
    "create table " + PLAY_TABLE + " (_id integer primary key autoincrement, " 
    + "game_id integer, game_date date);";
private static final String PLAYER_TABLE_CREATE =
    "create table " + PLAYER_TABLE + " (_id integer primary key autoincrement, " 
    + "name text not null);";
private static final String SCORE_TABLE_CREATE =
    "create table " + SCORE_TABLE + " (_id integer primary key autoincrement, " 
    + "game_id int, player_id int, score int);";

   ...

public void onCreate(SQLiteDatabase db) {
db.execSQL(GAME_TABLE_CREATE);
db.execSQL(PLAY_TABLE_CREATE);
db.execSQL(PLAYER_TABLE_CREATE);
db.execSQL(SCORE_TABLE_CREATE);
}

This seems like a nightmare for readability and maintainability. Any advice on how to better manage multiple SQL tables, and turn these kinds of lists into nice clean loops? I was thinking about trying to do it via resource string-arrays, but haven't been able to figure out how to manage that.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

黎歌 2024-11-14 20:02:32

我在 Android 世界中没有遇到过这个问题,但过去在 Web 世界中却遇到过。我说过,对于初学者来说,您可能需要查看 ORM 来管理您的语句。 此处似乎有一些可能有所帮助。

I haven't had to deal with this in the Android world but have in the past in the Web world. I'd said for starters you may want to look at an ORM for managing you statements. There seems to be some pointers here that might help.

极致的悲 2024-11-14 20:02:32

我通过使用语句 //formatter: off (并在之后将其打开)来关闭 Eclipse 中的格式化(您必须使用 Helios 或更高版本)来管理此问题。 这个问题< /a> 将为您提供更多相关信息。

我还将所有初始 SQL 语句(CREATE、触发器等)放入静态 String 数组中。这样,您的所有语句都可以在 onCreate 中使用此代码运行:

public static final String[] SQL_CREATE_TABLES = new String[] { TABLE_1, TABLE_2};

@Override
public void onCreate(SQLiteDatabase db) {
     for (String sql : SQL_CREATE_TABLES)
         db.execSQL(sql);
}

SQL 语句也可以放置在 strings.xml 资源文件中,但我认为这不会有助于保持可读性或可维护性。

I manage this by turning off formatting in Eclipse (you must be using Helios or higher) by using the statement //formatter: off (and turning it on after). This SO question will give you more information on that.

I also place all my initial SQL statements (CREATE, Triggers, etc.) inside a static String array. This way all your statements can be run with this code in your onCreate:

public static final String[] SQL_CREATE_TABLES = new String[] { TABLE_1, TABLE_2};

@Override
public void onCreate(SQLiteDatabase db) {
     for (String sql : SQL_CREATE_TABLES)
         db.execSQL(sql);
}

The SQL statements could also be placed in the strings.xml resource file, but I don't think that would help maintain readability or maintainability.

画骨成沙 2024-11-14 20:02:32

刚开始时我遇到了同样的问题,在 Android 应用程序内管理创建表作为语句变得非常麻烦,尤其是随着开发的进展尝试更新架构。对我来说真正有效的方法是将数据库的副本放在资产文件夹中,然后将数据库复制到数据文件夹中。第一次启动 DBAdapter 时,您可以检查数据库是否已存在,如果不存在,则可以复制数据库。当表需要预先填充数据或预先加载数据以进行测试时,这也很方便。它使在基于普通/GUI 的数据库工具中管理数据库模式变得更加容易。
有很多 Android 复制数据库代码示例,简单的 Google 搜索应该会让您找到正确的方向。
希望这有帮助,
干杯,

I ran into this same issue when first starting out, managing the create tables as statements inside the Android app became really cumbersome, especially trying to update the schemas as development progressed. What worked really well for me was simply placing a copy of my database in the assets folder and then copy the database to the data folder. The first time your DBAdapter starts you can check if the database already exists, if it doesn't you can then copy your database over. This is also handy where tables need to be prepopulated with data, or pre loaded with data for testing. It made life so much easier to manage the database schemas in a normal/GUI based database tool.
There are lots of Android copy database code examples, a simple Google search should set you in the right direction.
Hope this helps,
Cheers,

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文