【问题标题】:Sqlite columns to string arraySqlite 列到字符串数组
【发布时间】:2013-07-23 21:12:07
【问题描述】:

如何将 android sqlite 数据库中的每一列放入不同的字符串数组中? 我有一个包含 4 列的数据库:id,一个用于存储日期,另一个用于存储小时,另一个用于存储 REAL 值。 我需要的是按日期获取小时和实际值列,并将小时列放在一个字符串 [] 中,将实际值列放在另一个字符串 [] 中。 然后我需要将两个字符串数组中的每一个放在两个不同的布局中。所以不一定是字符串数组,只要适合我的需要,它可以是其他任何东西。

目前我正在使用它,但我不知道如何将它进一步放入字符串数组中

public Cursor fetchCountriesByName(String inputText) throws SQLException {
    Cursor mCursor = null;
    if (inputText == null || inputText.length() == 0) {
        mCursor = this.sqliteDBInstance3.query(DB_TABLE_NAME3, new String[] {
                DB_COLUMN_1_NAME3, DB_COLUMN_2_NAME3
                }, null, null, null, null, null);

    } else {
        mCursor = this.sqliteDBInstance3.query(true, DB_TABLE_NAME3,
                new String[] { DB_COLUMN_1_NAME3,
                        DB_COLUMN_2_NAME3 },
                DB_COLUMN_3_NAME3 + " like '%" + inputText + "%'", null,
                null, null, null, null);
    }
    if (mCursor != null) {
        mCursor.moveToFirst();
    }
    return mCursor;

}

这是我的表创建:

private static final String DB_NAME3 = "usingsqlite3.db";
private static final int DB_VERSION_NUMBER3 = 1;
private static final String DB_TABLE_NAME3 = "countries3";
private static final String DB_COLUMN_1_NAME3 = "country_value3";
private static final String DB_COLUMN_2_NAME3 = "country_hour";
private static final String DB_COLUMN_3_NAME3 = "country_date";


private static final String DB_CREATE_SCRIPT3 = "create table " + DB_TABLE_NAME3 +
                        " (id3 integer primary key autoincrement, country_value3 REAL, country_hour TEXT, country_date TEXT);)";

【问题讨论】:

    标签: java android arrays string sqlite


    【解决方案1】:

    您可以使用 ArrayList 来执行此操作,例如:

    Cursor c = fetchCountriesByName("someCountry");
    ArrayList<String> data = new ArrayList<String>();
    while (c.moveToNext()) {
        data.add(c.getString(DB_COLUMN_1_NAME3));
    }
    

    然后您可以将数组列表转换为字符串数组(搜索如何执行此操作)。

    【讨论】:

    • 这是否适用于 2 列/ArrayLists ?我需要从表中获取两列,一列是小时,另一列是值。
    • 当然,您可以添加另一个数组列表并将第二列数据添加到其中: data2 = new ArrayList();然后,data2.add(c.getString(DB_COLUMN_2_NAME3) 等
    猜你喜欢
    • 1970-01-01
    • 2016-08-31
    • 2011-07-22
    • 2013-04-15
    • 2013-09-15
    • 2014-10-24
    • 2021-09-30
    • 2017-03-31
    • 1970-01-01
    相关资源
    最近更新 更多