简体   繁体   English

如何从数据库[光标适配器]获取值并将其存储到java中的数组?

[英]How to get values from database[Cursor Adapter] and store it to an array in java?

I would like to get the values from database of a particular column by executing a query. 我想通过执行查询从特定列的数据库中获取值。 Is it possible to do it after we do it to the Cursor Adapter or can we attain the values well before itself. 在我们对光标适配器执行此操作后是否可以执行此操作,或者我们可以在它们之前很好地获得这些值。 Kindly help on this with a snippet or a guide. 请使用片段或指南为此提供帮助。

Context context = getApplicationContext(); 
final DataBaseHelper db = new DataBaseHelper(context);
...
...
db.createDataBase();
..
...try catch logic etc
....
final Cursor c = db.getAllRows();
....
c.getString(4) // String value of 5th Column in Database

Cursor Adapter to Array 光标适配器到数组

ArrayList<String> mArrayList = new ArrayList<String>(); 
c.moveToFirst(); 
while(!c.isAfterLast()) { 
     mArrayList.add(c.getString(c.getColumnIndex(DataBaseHelper.KEY_NAME)); 
     c.moveToNext(); 
} 

DataBaseHelper class has following DataBaseHelper类有以下内容

public Cursor getAllRows() 
    {
        return myDataBase.query(DATABASE_TABLE, new String[] {
                KEY_ROWID, 
                KEY_NAME,
                KEY_YEAR,
                     KEY_QUOTE,
                     KEY_REF}, 
                null, 
                null, 
                null, 
                null, 
                null);
    }

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

相关问题 如何将数据库中的表值存储在 java 的二维数组中 - how to store table values from database in a 2d array in java 如何从数组适配器获取值? - How to get values from an array adapter? 如何使用java从数组中获取值并将其存储到单独的变量中? - How to get values from an array and store it into separate variables using java? 根据jlist选定的值从数据库中获取项目并存储在数组中 - get items from database according to jlist selected values and store in array 如何使用游标适配器将数据从数据库添加到回收者视图? - How to add data from a database to a recycler view using a cursor adapter? 如何从Firebase实时数据库获取所有子值并将其存储在数组列表中 - How to get all child values from firebase realtime database and store it in an array list 如何从 Firestore 数据库中获取所有文档 ID 并将其存储到数组适配器 - How to fetch and store all document id from firestore database to array adapter 如何在数组中存储值(Java) - How to store values in an array (Java) java:如何从数据库中获取值到jTable中 - java: how to get values into jTable from database 如何存储/检索列表 <string> 使用Java从/到数据库的值 - How to store/retrieve List<string> values from/to database using java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM