hi
I want to store the result of request in a table. I want the result of the method is table howa to do that? My code contains error.
public array getResult_libelle(int id)
{
array tab[] = null;
try
{
Cursor c = null;
c = db.rawQuery("select libelle from favori where _id="+id, null);
c.moveToFirst();
tab = c.getString(c.getColumnIndex("libelle"));
c.close();
}
catch(Exception e)
{
e.printStackTrace();
}
return tab;
}
public ArrayList <String> getResult_libelle(int id)
{
ArrayList <String> tab = new ArrayList <Stringr>();
try
{
Cursor c = null;
c = db.rawQuery("select libelle from favori where _id="+id, null);
c.moveToFirst();
for(int i=0;i<c.getCount();i++){
tab.add(c.getString(c.getColumnIndex("libelle")));
c.moveToNext();
}
c.close();
}
catch(Exception e)
{
e.printStackTrace();
}
return tab;
}
First of all, your cursor will already be having only the column named libelle so I don't think there should be a need to explicitly filter it out.
I guess, you should directly use getString(i) to get data of each row out, one by one, from the cursor
give that a try.
Related
Here I want to store data in ArrayList visible Sections please help me...
Here's my code.
public DataSingleton() {
try {
db = openOrCreateDatabase("/sdcard/Download/Nanhikali_db.db", Context.MODE_PRIVATE, null);
Cursor c = db.rawQuery("select label from nanhikali where level = 1", null);
if (c.moveToFirst()) {
while (c.moveToNext()) {
String lb = c.getString(c.getColumnIndex("label"));
visibleSections.add(lb);
visibleSections = new ArrayList<>();
}
}
}
catch(Exception e)
{
}
Your column string maybe wrong:
Try like this.
if(c.moveToFirst()){
while(c.moveToNext()){
String lb = c.getString(c.getColumnIndex("label"));
visibleSections.add(lb);
}
}
Not "lebel".
You are re assigning the array list every time your loop iterates.
remove visibleSections = new ArrayList<>(); from your loop.
and use While Loop as
while(!cursor.isAfterLast())
{
//get All data here then
cursor.moveToNext();
}
I want to get values but the function always returns null. Even though I debug and there is value inside variable rv.
This is my method:
public ArrayList<String> getList(int id) {
try {
ArrayList<String> rv = new ArrayList<String>();
open();
Cursor c = db.rawQuery("select * from reviews where IDRE="+id, null);
if(c.moveToFirst() || c.getColumnCount()==1) {
rv.add(String.valueOf(c.getInt(c.getColumnIndex("IDRE"))));
rv.add(String.valueOf(c.getInt(c.getColumnIndex("ID_FK"))));
rv.add(c.getString(c.getColumnIndex("DATE")));
rv.add(c.getString(c.getColumnIndex("TYPE")));
rv.add(String.valueOf(c.getInt(c.getColumnIndex("COST"))));
rv.add(c.getString(c.getColumnIndex("SERVICE")));
rv.add(c.getString(c.getColumnIndex("ATMOSPHERE")));
rv.add(c.getString(c.getColumnIndex("OVERALL")));
rv.add(c.getString(c.getColumnIndex("COMMENT")));
}
c.close();
close();
return rv;
}catch(Exception e) {
return null;
}
}
Some logging would help determine if you're throwing an error and ending up in that catch block, as #Daniel Nugent is saying.
But I think the issue is with your if expression. c.moveToFirst is going to move your cursor to the first row of your data source, and then return true unless that data source is empty, so the only time that if block does not occur is when your data source is empty. The only way c.getColumnCount()==1 is having any effect on the evaluation of your expression is if you have a table with only one column and no rows. Let us know what you're trying to achieve with that, and add some logging, and we'll be better able to help you.
I have edited your code ...and given two example , you can refer any one...
public ArrayList<String> getList(int id) {
try {
ArrayList<String> rv = new ArrayList<String>();
open();
Cursor c = db.rawQuery("select * from reviews where IDRE="+id, null);
if(c==null)
return null;
c.moveToFirst();
rv.add(String.valueOf(c.getInt(c.getColumnIndex("IDRE"))));
rv.add(String.valueOf(c.getInt(c.getColumnIndex("ID_FK"))));
rv.add(c.getString(c.getColumnIndex("DATE")));
rv.add(c.getString(c.getColumnIndex("TYPE")));
rv.add(String.valueOf(c.getInt(c.getColumnIndex("COST"))));
rv.add(c.getString(c.getColumnIndex("SERVICE")));
rv.add(c.getString(c.getColumnIndex("ATMOSPHERE")));
rv.add(c.getString(c.getColumnIndex("OVERALL")));
rv.add(c.getString(c.getColumnIndex("COMMENT")));
c.close();
close();
return rv;
}catch(Exception e) {
return null;
}
}
Second way:
public ArrayList<String> getList(int id) {
try {
ArrayList<String> rv = new ArrayList<String>();
open();
String[] columns=new String[]{"IDRE","ID_FK","DATE","TYPE","COST","SERVICE","ATMOSPHERE","OVERALL","COMMENT"};
Cursor c=sql_db.query("reviews", columns, "IDRE"+"=?", new String[] {String.valueOf(id)}, null, null, null);
if(c==null)
return null;
c.moveToFirst();
rv.add(String.valueOf(c.getInt(c.getColumnIndex("IDRE"))));
rv.add(String.valueOf(c.getInt(c.getColumnIndex("ID_FK"))));
rv.add(c.getString(c.getColumnIndex("DATE")));
rv.add(c.getString(c.getColumnIndex("TYPE")));
rv.add(String.valueOf(c.getInt(c.getColumnIndex("COST"))));
rv.add(c.getString(c.getColumnIndex("SERVICE")));
rv.add(c.getString(c.getColumnIndex("ATMOSPHERE")));
rv.add(c.getString(c.getColumnIndex("OVERALL")));
rv.add(c.getString(c.getColumnIndex("COMMENT")));
c.close();
close();
return rv;
}catch(Exception e) {
return null;
}
}
In my application i am showing data from database in a table view.My requirement is that from database i have to retrieve the data which will fall in the current month.I Have written the query but it is coming as 0.Actually i have 1 entry in the database with today's date,so my query should return that data,but it is showing as 0.Please help me.Thanks in advance.
My query is as follows:
public String addgroupincome(String grp) throws SQLException
{
long sum=0;
Cursor cursor1 = db.rawQuery(
"SELECT SUM("+(KEY_TOTAL)+") FROM incomexpense WHERE date= Strftime('%Y-%m','now') AND category='Income' AND groups='"+grp+"'",null);
if(cursor1.moveToFirst())
{
sum = cursor1.getLong(0);
}
cursor1.close();
String housetotal=String.valueOf((long)sum);
return housetotal;
}
I am getting that total and showing in atextview in table layout..
final String houtotal=db.addgroupincome(group1);
housetotal.setText(houtotal);
Most probably nothing wrong with the query but the way you pass the query result to ListView. Can you show how you do it? Perhaps I could help.
Or you could take a look here or here
public int getCount() {
DBHelper dbHelper = DBHelper.getDBAdapterInstance(this);
int count = 0;
try {
dbHelper.openDataBase();
String query = "select count(1) from t_model where upper(brandName) = upper('"
+ selectedBrand + "') order by modelName ASC";
Cursor cursor = dbHelper.selectRecordsCursor(query, null);
if (cursor.moveToFirst()) {
count = cursor.getInt(0);
}
cursor.close();
cursor = null;
} catch (Exception e) {
e.printStackTrace();
} finally {
dbHelper.close();
}
return count;
}
and for the TextView should be as simple as
TextView tvCount = (TextView) findViewById(R.id.tvCount);
tvCount.setText("Count : " + getCount);
If you are having trouble debugging your query. Try http://sqlitebrowser.sourceforge.net/ or http://www.sqliteexpert.com/
Why don't you try by giving column names of your table in your query..might it work out for you..specify the columns which you want to retrive..
if (cursor.moveToNext()) {
cursor.moveToFirst();
while (cursor.isAfterLast() == false) {
Log.i(ID, cursor.getInt(0) + "");
cursor.moveToNext();
}
cursor.close();
} else {
cursor.close();
return null;
}
Try This Method....
I am new to android . I have an application working with SQLite DB.
I need to push values from database to an arraylist of type object.
The code i used is given here.
private ArrayList<objectname> objectList = new ArrayList<objectname>();
//function used to get values from database
public ArrayList<objectname> getResults() {
MyDb db = new MyDb(this); //my database helper file
db.open();
Cursor c = db.getAllValues(); //function to retrieve all values from a table- written in MyDb.java file
if (c.moveToFirst())
{
do {
String date = c.getString(c.getColumnIndex("date"));
try
{
ob = new objectname();
ob.setDate(c.getString(c.getColumnIndex("date")));// setDate function is written in Class file
objectList.add(ob);
}
catch (Exception e) {
Log.e(MY_DEBUG_TAG, "Error " + e.toString());
}
} while (c.moveToNext());
}
db.close();
return objectList;
}
This is not working correctly. Not shown any errors but i didn't get values in to the arraylist objectList
Please help me..Thanks in advance..
try this:
private ArrayList<objectname> objectList = getResults();
private ArrayList<objectname> getResults() {
MyDb db = new MyDb(this); //my database helper file
db.open();
ArrayList<objectname> resultList = new ArrayList<objectname>();
Cursor c = db.getAllValues(); //function to retrieve all values from a table- written in MyDb.java file
while (c.moveToNext())
{
String date = c.getString(c.getColumnIndex("date"));
try
{
ob = new objectname();
ob.setDate(date);// setDate function is written in Class file
resultList.add(ob);
}
catch (Exception e) {
Log.e(MY_DEBUG_TAG, "Error " + e.toString());
}
}
c.close();
db.close();
return resultList;
}
I had a similar issue with retrieving values from the db using cursors and displaying on screen.. The below solution works for me..
Cursor cur = db.getAllValues();
int index = c.getColumnIndex("date");
cur.moveToFirst();
while (cur.isAfterLast() == false) {
System.out.println(cur.getString(index)); // For debug purposes
String date = cur.getString(index);
try {
ob = new objectname();
ob.setDate(date);
resultList.add(ob);
} catch (Exception e) {
Log.e(MY_DEBUG_TAG, "Error " + e.toString());
}
cur.moveToNext();
}
cur.close();
Probably your query is drawing a blank and moveToFirst is returning false.
What about logging the value of c.getCount() before your if statement?
I have certain data in sqlite and want to fetch the data into an array of string, I am getting the first column successfully but cant able to fetch the next column data. Here is my code.
SQLiteDatabase db = contactsdbHelper.getWritableDatabase();
//db.openDatabase(path, factory, flags)
int columnIndex = 1; // Whichever column your float is in
Cursor cursor1= getData();
String[] values=new String[cursor.getCount()+1];
try{
if(cursor1.moveToFirst())
{
for(int i=0;i<cursor1.getCount();i++ )
{
values[i]=cursor1.getString(columnIndex);
//Log.i("HI", "piyush");
Log.i("StartTime"+i, values[i]);
cursor1.moveToNext();
}
}
}
catch(ArrayIndexOutOfBoundsException e){
Log.i("Exception Generated", e.toString());
}
String[] values1=new String[cursor.getCount()+1];
try{
if(cursor1.moveToNext())
{
for(int i=0;i<cursor1.getCount();i++ )
{
values1[i]=cursor1.getString(columnIndex+1);
Log.i("EndTime"+i, values[i]);
cursor1.moveToNext();
}
}
}
catch(ArrayIndexOutOfBoundsException e){
Log.i("Exception Generated", e.toString());
}
Why not fill both arrays at once?
SQLiteDatabase db = contactsdbHelper.getWritableDatabase();
//db.openDatabase(path, factory, flags)
int columnIndex = 1; // Whichever column your float is in
Cursor cursor1= getData();
String[] values=new String[cursor.getCount()+1];
String[] values1=new String[cursor.getCount()+1];
try{
if(cursor1.moveToFirst())
{
for(int i=0;i<cursor1.getCount();i++ )
{
values[i]=cursor1.getString(columnIndex);
values1[i]=cursor1.getString(columnIndex+1);
Log.i("StartTime"+i, values[i]);
cursor1.moveToNext();
}
}
}
catch(ArrayIndexOutOfBoundsException e){
Log.i("Exception Generated", e.toString());
}
But that whole thing looks kinda rubbish ... what are you trying to achieve?
You will need to call moveToFirst() on the Cursor again before you can start reading the second column.
Specifically the second if should be:
if (cursor1.moveToFirst())
Edit - usually data is in a database for a reason: you don't want to fetch it all into memory in one big array most of the time.
Could you be looking for a CursorAdapter and ListView combination to display the data in a list?
Try reading the column values by their column name.
Cursor cursor = ...
if(cursor.moveToFirst()) {
do {
values[i] = cursor.getString(cursor.getColumnIndex("columnName"));
values1[i] = cursor.getString(cursor.getColumnIndex("otherColumnName"));
} while(cursor.moveToNext());
}
where "columnName" and "otherColumnName" are the names of the columns of your table which you want to fill into the arrays.
Cursor cur = db.query(TABLENAME, null, null, null, null, null, null);
cur.moveToFirst();
int a[] = new int[100];
for (int i = 0; i < cur.getCount(); i++)
{
a[i] = cur.getInt(0);
cur.moveToNext();
}
Here is my code to return all the data in the table in list. But, it is not working.
I have called this method from CheckData class which in turn is called by main class via intent.
public List<String[]> selectAll()
{
List<String[]> list = new ArrayList<String[]>();
Cursor cursor = db
.query(TABLE_NAME, null, null, null, null, null, null);
int x = 0;
if (cursor.moveToFirst())
{
do
{
String[] b1 = new String[]
{
cursor.getString(1),
cursor.getString(2)
};
list.add(b1);
x = x + 1;
}
while (cursor.moveToNext());
}
if (cursor != null && !cursor.isClosed())
{
cursor.close();
}
cursor.close();
return list;
}
My database contains 3 columns - id(integer primary key), symbol(text) and company_name(text). My database name is AppDB and table name is scrip.