Querying Multiple Fields in a Database - android

For example, I have this table:
I am using the following method to query from that table:
public ArrayList<ArrayList<String>> getAllViandCategory() {
ArrayList<ArrayList<String>> data2 = new ArrayList<ArrayList<String>>();
ArrayList<String> data = new ArrayList<String>();
// Select All Query
String selectQuery = "SELECT * FROM " + "tbl_viand_category";
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
data.add(cursor.getString(0));
data.add(cursor.getString(1));
data2.add(data);
} while (cursor.moveToNext());
}
return data2;
}
I am not getting the desired output. I am getting this: [1,Fish,2,Vegetables,3,Meat,4,Rice,5,Etc] instead of [[1,Fish],[2,Vegetables],[3,Meat],[4,Rice],[5,Etc]]

What you have is a List of Lists. The outer list has objects which has lists.
// Code to add a list object into outer list.
data2.add(data);
You will notice that you have created the data object just once. So basically the same object is inserted into data2 again and again.
You need to create a new data object for each iteration.
// New Code
if (cursor.moveToFirst()) {
do {
data = new ArrayList<String>();
data.add(cursor.getString(0));
data.add(cursor.getString(1));
data2.add(data);
} while (cursor.moveToNext());
}
Hope that helps :D

Related

I want to retrieve data from SQL lite as an array of object using android

public List<Report> selectAll() {
List<Report> list = new ArrayList<Report>();
Report report = new Report();
int id,Temp;
String Date,Tank,Tankuse;
SQLiteDatabase db = this.getWritableDatabase();
String query = "select ID, Date,Temp,Tank,Tankuse from "+TABLE_NAME;
Cursor cursor = db.rawQuery(query , null);
if (cursor.moveToFirst()) {
id=cursor.getInt(0);
report.setID(id);
Date=cursor.getString(1);
report.setDateTime(Date);
Temp=cursor.getInt(2);
report.setTemp(Temp);
Tank=cursor.getString(3);
report.setTank(Tank);
Tankuse=cursor.getString(4);
report.setTank(Tankuse);
list.add(report);
while (cursor.moveToNext());
}
return list;
}
how i can retrieve the data as array of objects this is my first time Using SQL lite i use 3 tires architecture in asp.net but i don't know how that's work here so can some tell me how or what i should change in my code to make it return an array of object ?
Example:
public List<Contact> getAllContacts() {
List<Contact> contactList = new ArrayList<Contact>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_CONTACTS;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Contact contact = new Contact();
contact.setID(Integer.parseInt(cursor.getString(0)));
contact.setName(cursor.getString(1));
contact.setPhoneNumber(cursor.getString(2));
// Adding contact to list
contactList.add(contact);
} while (cursor.moveToNext());
}
// return contact list
return contactList;
}

Bind Arrayadapter to textview android

i want to display data from sqlite to textview,here is my code.What i have dont wrong.It is not displaying the data no error but crashes
main class
private void loadTextViewData()
{
DatabaseHandler db = new DatabaseHandler(getApplicationContext());
List<String> lables = db.getAllLabels();
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,android.R.layout.test_list_item, lables);
text.setText(??);
}
sql class
public List<String> getAllLabels(){
List<String> labels = new ArrayList<String>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_LABELS;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
labels.add(cursor.getString(1));
} while (cursor.moveToNext());
}
// closing connection
cursor.close();
db.close();
// returning lables
return labels;
}
instead of ArrayAdapter use for example SimpleCursorAdapter (or any subclass of CursorAdapter)

List Data Format

I'm fetching Data form Table as list and adding that to sub list but i'm getting output that i didn't expect. I don't know how to Fix that so that i can get the output what i need
I have attached the code please help me to solve this issue??
public List<List<String>> fetchData(){
List<List<String>> main= new ArrayList<List<String>>();
List<String> sub= new ArrayList<String>();
String qry="SELECT * FROM "+myTable+"";
SQLiteDatabase db= this.getReadableDatabase();
Cursor cursor=db.rawQuery(qry, null);
if (cursor.moveToFirst()) {
do {
System.out.println("-TblPOmaster-");
sub.add(cursor.getString(0));
sub.add(cursor.getString(1));
sub.add(cursor.getString(2));
main.add(sub);
System.out.println("Data:"+main);
} while (cursor.moveToNext());
}
cursor.close();
db.close();
return main;
}
Output what i'm Getting Now
Data:[[a1,a2,a3]]
Data:[[a1,a2,a3,b1,b2,b3],[a1,a2,a3,b1,b2,b3]]
Data:[[a1,a2,a3,b1,b2,b3,c1,c2,c3],[a1,a2,a3,b1,b2,b3,c1,c2,c3],[a1,a2,a3,b1,b2,b3,c1,c2,c3]]
Output what i need
Data:[[a1,a2,a3]]
Data:[[a1,a2,a3],[b1,b2,b3]]
Data:[[a1,a2,a3],[b1,b2,b3],[c1,c2,c3]]
if (cursor.moveToFirst()) {
do {
List<String> sub= new ArrayList<String>();
System.out.println("-TblPOmaster-");
sub.add(cursor.getString(0));
sub.add(cursor.getString(1));
sub.add(cursor.getString(2));
main.add(sub);
System.out.println("Data:"+main);
} while (cursor.moveToNext());
}
cursor.close();
db.close();
return main;
}
try to declare sub list inside the cursor and it will work fine

how to get all IDs from a sqlite database

I have a database manager class where i build a table that has two columns. the first column being an integer, and i am trying to write a method that will return all values in that column as a list:
public List<Integer> getAllStyleIDs(){
List<Integer> results = null;
SQLiteDatabase db = this.getReadableDatabase();
// Select All Query
String selectQuery = "select * from " + DBConstants.allStyles;
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
results.add(cursor.getInt(0));
} while (cursor.moveToNext());
}
cursor.close();
db.close();
return results;
}
but i am getting a null point exception
any ideas?
Your list results is null, then you attempt to add to it. Try this:
List<Integer> results = new ArrayList<Integer>();
First thing: the list hasn't been initialized.
Second thing: using raw queries is not recommended:
Try something like this:
List<Integer> results = new ArrayList<Integer>();
this.database = this.getReadableDatabase();
String [] columns = {_ID}; // name of the column
Cursor cursor = this.database.query(COURSES_TABLE, columns, null, null, null, null, null);
int iId = cursor.getColumnIndex(_ID);
for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext())
{
Integer rowId = cursor.getInt(iName);
results.add(rowId);
}
cursor.close();
this.database.close();
return results;

How to store object return by a cursor into a arraylist?

I am taking object from sqlite database with the help of cursor. And I would like to store them into an arraylist. THe problem is that I dont know the size of my returned data in advance. So How do I put them into an arraylist?
code:
public Student findAll()
{
db = helper.getWritableDatabase();
Cursor cursor = db.rawQuery("select sid, name, age from t_student", null
);
if(cursor.moveToNext())
return new Student(cursor.getInt(cursor.getColumnIndex("sid")), cursor.getString(cursor.getColumnIndex("name")), cursor.getInt(cursor.getColumnIndex("age")));
return null;
}
Main:
ArrayList<Student> studentArrayList = new ArrayList<Student>();
studentArrayList.add(dao.findAll()); //doing this will only return the first object from the database
According to the ArrayList docs
Each ArrayList instance has a capacity. The capacity is the size of the array used to store the elements in the list. It is always at least as large as the list size. As elements are added to an ArrayList, its capacity grows automatically. The details of the growth policy are not specified beyond the fact that adding an element has constant amortized time cost.
This will help you to understand ArrayList in a better way.
public List<Student> findAll() {
List<Student> studentArrayList = new ArrayList<Student>();
db = helper.getWritableDatabase();
Cursor cursor = db.rawQuery("select sid, name, age from t_student", null
);
while(cursor.moveToNext()) {
studentArrayList.add(new Student(cursor.getInt(cursor.getColumnIndex("sid")), cursor.getString(cursor.getColumnIndex("name")), cursor.getInt(cursor.getColumnIndex("age"))));
}
return studentArrayList ;
}
ArrayList<Student> studentArrayList = new ArrayList<Student>();
studentArrayList=findAll();
Try this:
public List<Student> findAll() {
List<Student> studentArrayList = new ArrayList<Student>();
db = helper.getWritableDatabase();
Cursor cursor = db.rawQuery("select sid, name, age from t_student", null
);
while(cursor.moveToNext()) {
studentArrayList.add(new Student(cursor.getInt(cursor.getColumnIndex("sid")), cursor.getString(cursor.getColumnIndex("name")), cursor.getInt(cursor.getColumnIndex("age"))));
}
return studentArrayList ;
}
Your problem is that instead of:
if(c.moveToNext()) {
// ...
}
you have to iterate with a while loop:
while(c.moveToNext()) {
// ...
}
Thus:
List<Student> myList = new ArrayList<String>();
Cursor c = ...
while(c.moveToNext()) {
myList.add(new Student(cursor.getInt(cursor.getColumnIndex("sid")), cursor.getString(cursor.getColumnIndex("name")), cursor.getInt(cursor.getColumnIndex("age")));
}
c.close();

Categories

Resources