M
M
Maxim Savichev2015-07-04 11:01:36
Java
Maxim Savichev, 2015-07-04 11:01:36

How to pull data from SQLITE database and populate two dimensional array with it in JAVA?

Good afternoon,
There is some data table "table" with ID and NAME elements.
How can I pull these values ​​from the database into a two-dimensional array in JAVA?
Basically my example:

sumID= db.execSQL(SELECT sum(id) FROM table);
int arr[sumID][2];
arr = new int[sumID][sumID];
for (int i = 0; i <= sumID - 1; i++) {
  arr[i][0]= // как вывести элемент id из bd?
arr[i][1]= //как вывести элемент name из bd?
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
O
Oleg Gamega, 2015-07-04
@MaksimSa

in the base id is not necessarily sequential so the array is not suitable for you, use HashMap

Cursor c = db.query("table_name", null, null, null, null, null, null);
if(c!=null&&c. moveToFirst()){
   do{
       long id = c.getLong(c.getColumnIndexOrThrow ("_id"));
       String name  = c.getString(c.getColumnIndexOrThrow ("name"));
      // добавляете в hashMap или куда вам удобнее 
   }while(c.moveToNext());
}

C
cthulhudx, 2015-07-04
@cthulhudx

JDBC driver for sqlite: SQLite JDBC
Connecting/Querying Examples:
Java JDBC using SQLite/Connecting
Connect to SQLite via JDBC

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question