简体   繁体   English

在Android中从SQLite获取单个值

[英]Fetching single value from SQLite in android

I'm developing my firs app for android right now, I am using multiple tables to get and insert data. 我现在正在为Android开发我的firs应用程序,正在使用多个表来获取和插入数据。 during the development and found myself fetching data from the table with has only two columns STATS(_id, stat_name) . 在开发过程中,发现自己从只有两列STATS(_id, stat_name)的表中获取数据。 What my problem is? 我的问题是什么? I have an activity with 10 buttons, and every button correlates with one stat_name . 我有一个带有10个按钮的活动,每个按钮都与一个stat_name相关。 When users presses one of the buttons application is "going" to STATS table to get correct _id and then is inputting this _id to another table GAME_STATS(_id, PlayerId (fk), GameId(fk), StatsId(fk)(andmore)) on STATS._id = GAME_STATS.StatsId and I basicly have to do similar operation for PlayerId . 当用户按下其中一个按钮时,应用程序将“进入” STATS表以获取正确的_id ,然后将该_id输入到另一个表中GAME_STATS(_id, PlayerId (fk), GameId(fk), StatsId(fk)(andmore))STATS._id = GAME_STATS.StatsId ,我基本上必须对PlayerId做类似的操作。

Right now, I'm doing it this way: 现在,我正在这样做:

public String getStatId(String statName){
    String statId = "Error";
    Cursor c = mDb.query(STAT_TABLE, new String[] {AbstractDbAdapter.STAT_ID, AbstractDbAdapter.STAT_NAME}, AbstractDbAdapter.STAT_NAME+ " = " +statName, null, null, null, null);
    int count = c.getCount();
    if(count == 1){
        c.moveToFirst();
        statId = c.getString(c.getColumnIndex(AbstractDbAdapter.STAT_ID));
    }
    c.close();
    mDb.close();
    Log.d("FootballApp","StatId =" +statId);
    return statId;      
}

What my problem is, that I know that there SHOULD be only one value returned, and I still have to use Cursor, to do so. 我的问题是,我知道应该只返回一个值,而我仍然必须使用Cursor这样做。 Also, in my opinion, it looks way to complicated and time consuming wo write all that code just to get one id from one table. 而且,我认为,编写所有代码只是从一个表中获得一个ID看起来很复杂且耗时。 I have 9 tables in my application, and I will have to write similar method every time I need _id from different table when I have, for example, only name. 我的应用程序中有9个表,例如,当我只有名字时,每次需要来自不同表的_id时,我都必须编写类似的方法。

Can someone tell me if there is easier way to do all that? 有人可以告诉我是否有更简便的方法来完成所有这些工作? Please :) thanks! 请:)谢谢! :) :)

I think it doesn't get much simpler than that. 我认为没有比这更简单的了。 However you can make the method more generic so you can reuse the code: 但是,您可以使该方法更通用,因此可以重用代码:

public String getFromDb(String tableName, String select, String selectBy, String selectName){
    String selection = "Error";
    Cursor c = mDb.query(tableName, new String[] {select}, selectBy + "=" + selectName, null, null, null, null);
    if(c.getCount() == 1){
        c.moveToFirst();
        selection = c.getString(c.getColumnIndex(select));
    }
    c.close();
    mDb.close();
    Log.d("FootballApp", select + "=" + selection);
    return id;      
}

Example usage: 用法示例:

int statID = getFromDb(STAT_TABLE, AbstractDbAdapter.STAT_ID, AbstractDbAdapter.STAT_NAME, statName);

That is about as simple as it gets, but Cursor.moveToFirst() returns false if the cursor is empty so you can cut out the c.getCount() call and just say if(c.moveToFirst()) instead. Cursor.moveToFirst()简单,但是Cursor.moveToFirst()如果光标为空,则返回false,因此您可以删除c.getCount()调用,而只需说出if(c.moveToFirst()) That will save you a little bit of typing :) 这将节省您一点输入时间:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM