public String[] getNewsLink(String prodcuttye) {
Cursor cursor = mDb.query(SQLITE_TABLE, new String[] { "productname" }, "ProdctType='"
+ prodcuttye + "'", null, null, null, null, null);
String[] result = new String[cursor.getCount()];
int i = 0;
if (cursor.moveToFirst()) {
do {
result[i] = cursor.getString(cursor.getColumnIndex("productname"));
i++;
} while (cursor.moveToNext());
}
if ((cursor != null) && !cursor.isClosed()) {
cursor.close();
mDb.close();
}
return result;
}
This function for fetch data in String array i am trying to excute this Query am getting data when i am call the in Sqlite browser[ select productname from MyShopingTable where ProdctType= 'Basmati Rice'] But when i try to run this Query in function i am getting cursor count 0 so i am unable to get data please tell me where am doing wrong please suggest me
Try changing your query like following :
Cursor c = mDb.query("SELECT * FROM MyShopingTable where ProdctType = ?",new String[]{prodcuttye})
See if that works in your case...
Related
public String[] getAllDescription() {
db = this.getReadableDatabase();
cursor = db.query(NOTIFICATIONTABLE, new String[]{"notification_id", "notification_title",
"notification_description", "notification_isread", "date"}, null, null, null,
null, null);
String[] result = new String[cursor.getCount()];
int i = 0;
if (cursor.moveToFirst()) {
do {
result[i] = cursor.getString(cursor.getColumnIndex("notification_description"));
i++;
} while (cursor.moveToNext());
}
if ((cursor != null) && !cursor.isClosed()) {
cursor.close();
db.close();
}
return result;
}
this is my code currently i am getting from 0 to n data in staring array i want to get in reverse array data from n to 0 so that i can Print please suggest me how i will get data
Try this method :
public ArrayList<String> getAllDescription()
{
ArrayList<String> array_list = new ArrayListString>();
SQLiteDatabase db = this.getReadableDatabase();
Cursor res = db.rawQuery("SELECT * " + " FROM " + NOTIFICATIONTABLE+ " DESC;", null);
res.moveToFirst();
while (res.isAfterLast() == false)
{
array_list.add(hashmap);
res.moveToNext();
}
return array_list;
}
Or If you want to reverse arrayList then you can also do it By just a single line of code .
Collections.reverse("yourArrayList");
Hope it will help you.
i am getting from 0 to n data in staring array i want to get in
reverse array data from n to 0
Why not using ASC or DESC in db query to get result in sequence as want according to notification_description column.
do it as:
cursor = db.query(NOTIFICATIONTABLE,
new String[]{"notification_id",
"notification_title",
"notification_description",
"notification_isread",
"date"}, null, null, null,
null, "notification_description DESC");
You can insert the data from end of theString[]...
public String[] getAllDescription() {
db = this.getReadableDatabase();
cursor = db.query(NOTIFICATIONTABLE, new String[]{"notification_id", "notification_title",
"notification_description", "notification_isread", "date"}, null, null, null,
null, null);
String[] result = new String[cursor.getCount()];
int i = 0;
if (cursor.moveToFirst()) {
do {
///////////////////////
result[(cursor.getCount()-1)-i] = cursor.getString(cursor.getColumnIndex("notification_description"));
///////////////////////
i++;
} while (cursor.moveToNext());
}
if ((cursor != null) && !cursor.isClosed()) {
cursor.close();
db.close();
}
return result;
}
public ArrayList < PatientInfo > getAllPatients(String username) {
ArrayList < PatientInfo > patients = new ArrayList < PatientInfo > ();
open();
Cursor cursor = db.query(UserTable, null, null, null, null, null, AccessedDate);
while (cursor != null && cursor.moveToNext()) {
try {
} catch (Exception ex) {
//Log.e("XXX", ex.getMessage());
}
}
if (cursor != null)
cursor.close();
close();
Collections.reverse(patients);
return patients;
}
I am getting username as argument in my method, how can i query out my table based on username and get the user specific result.
As you can see from the documentation, the third and fourth parameters to query() are the selection and selection args.
So you want something like this:
Cursor cursor = db.query(UserTable, null,
"username=?", new String[] {username},
null, null, AccessedDate);
Edit the third parameter as needed to match the actual name of the relevant column in your table.
Hi i'm new to android programming and i have created a sample app which allows the user to get data from the database. however its not displaying the data, it doesn't have any error message its just not displaying it. database is confirmed that there is data. please check my code maybe i forgot something here. thanks
public void onClick(View arg){
name = txtNameS.getText().toString();
if(arg.getId()==R.id.btnfortune){
searchRecord(count);
lblmessageS1.setText(name); // this is just for me to check if it will be displayed and it is.
lblmessageS2.setText(message);
}
}
public void searchRecord(int count) throws SQLException {
Cursor rsCursor;
String [] rsFields = {"mesNum","Message"};
rsCursor = dbM.dbase.query("MessageFile", rsFields, "mesNum = " + count, null, null, null, null, null);
rsCursor.moveToFirst();
if (rsCursor.isAfterLast()==false){
message = rsCursor.getString(1);
}
rsCursor.close();
}
by the way count is initialized as 1. and there are 10 records in the database. and there are 2 columns in the database the mesNum and Message, what i want is to display only the message column.
// SQLiteDatabase sqldb
Cursor rsCursor= sqldb.rawQuery("your query", null);
if (rsCursor!= null) {
if (rsCursor.moveToFirst()) {
do {
// do here for get data message = rsCursor.getString(1);
}while (cursor.moveToNext());
}
cursor.close();
}
add only one column name in your array
String [] rsFields = {"Message"};
cursor = dbM.dbase.query(true,"MessageFile", rsFields, "yourcolumn= "+count, null, null, null, null, null);
while( cursor != null && cursor.moveToNext() )
{
cursor.getString(0);
}
cursor.close();
The code to returns all the data in the table in list.But this isn't working.
I've called this method from CheckData class which is called by main class
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 Data Base name is AppDB and table name is scrip.
Here are the good tutorial to LEARN use of Android-SQLite.
so,I also advise you the same As I answered here
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.