First of all I'm sorry I don't speak very good english. I'm currently developing an android app with eclipse and I have problem while fetching data from database. This is the method to read data from database. I put this method in a class named SQLiteAdapter:
public Cursor getQuestionEachLevel(long levelId)
{
return sqLiteDatabase.query(MYDATABASE_TABLE, new String[]{KEY_ROWID, KEY_ANSWER,
KEY_QUESTION, KEY_HINT, KEY_FLAG, KEY_LEVEL}, KEY_LEVEL + "=" + levelId,
null, null, null, null, null);
}
And this is the code that going to call that method and fetch the data. I put this code in another class.
int x=0;
String getId[] = null;
String getAnswer[] = null;
String getQuestion[] = null;
String getHint[] = null;
String getFlag[]= null;
String getLevel[]= null;
mySQLiteAdapter = new SQLiteAdapter(this);
mySQLiteAdapter.openToRead();
Cursor c = mySQLiteAdapter.getQuestionEachLevel(getLevelKey);
if(c.moveToFirst()){
do{
getId[x] = c.getString(0);
getAnswer[x] = c.getString(1);
getQuestion[x] = c.getString(2);
getHint[x] = c.getString(3);
getFlag[x] = c.getString(4);
getLevel[x] = c.getString(5);
x++;
}while(c.moveToNext());
}
mySQLiteAdapter.close();
I want to put each of the data from the database column into specific Variable because I'm going to use it later. Eclipse doesn't show any error but when I try to run this app on my phone, the app crash while fetching the data. Anyone know what's wrong with this?
Post your logcat, by all means. But in the meantime
int x=0;
String getId[] = null;
String getAnswer[] = null;
String getQuestion[] = null;
String getHint[] = null;
String getFlag[]= null;
String getLevel[]= null;
and afterwards
do{
getId[x] = c.getString(0);
getAnswer[x] = c.getString(1);
getQuestion[x] = c.getString(2);
getHint[x] = c.getString(3);
getFlag[x] = c.getString(4);
getLevel[x] = c.getString(5);
x++;
}while(c.moveToNext());
is always pretty problematic... (your arrays aren't initialized and you're attempting to access them. I'd advise this change:
int x=0;
List<String> getId = new ArrayList<String>();
List<String> getAnswer = new ArrayList<String>();
List<String>getQuestion = new ArrayList<String>();
List<String> getHint = new ArrayList<String>();
List<String>getFlag= new ArrayList<String>();
List<String>getLevel = new ArrayList<String>();
and afterwards
do{
getId.add( c.getString(0));
getAnswer.add( c.getString(1));
getQuestion.add(c.getString(2));
getHint.add(c.getString(3));
getFlag.add(c.getString(4));
getLevel.add(c.getString(5));
x++;
}while(c.moveToNext());
You must initialize all the arrays before using it.
int x=0;
String getId[] = null;
String getAnswer[] = null;
String getQuestion[] = null;
String getHint[] = null;
String getFlag[]= null;
String getLevel[]= null;
mySQLiteAdapter = new SQLiteAdapter(this);
mySQLiteAdapter.openToRead();
Cursor c = mySQLiteAdapter.getQuestionEachLevel(getLevelKey);
if(c.moveToFirst()){
int numOfRows = c.getCount();
getId = new String[numOfRows];
getAnswer = new String[numOfRows];
getQuestion = new String[numOfRows];
getHint = new String[numOfRows];
getFlag = new String[numOfRows];
getLevel = new String[numOfRows];
do{
getId[x] = c.getString(0);
getAnswer[x] = c.getString(1);
getQuestion[x] = c.getString(2);
getHint[x] = c.getString(3);
getFlag[x] = c.getString(4);
getLevel[x] = c.getString(5);
x++;
}while(c.moveToNext());
}
mySQLiteAdapter.close();
Related
I am unable to get recurring events from google calandar. Can any one help me with this.
I have also used "Instances" table to get the recurring events but not. Can any one guide me how to fetch data from "Instances" table. It should be a great help for me.
I used following code to read events.
Uri uri = CalendarContract.Events.CONTENT_URI;
String[] projection = new String[]{
CalendarContract.Events._ID,
CalendarContract.Events.DTSTART,
CalendarContract.Events.DTEND,
CalendarContract.Events.ALL_DAY,
CalendarContract.Events.TITLE,
CalendarContract.Events.EVENT_COLOR,
CalendarContract.Events.CALENDAR_ID,
CalendarContract.Events.DESCRIPTION,
CalendarContract.Events.ORGANIZER,
// If status = 2 it is cancelled by organizer
CalendarContract.Events.STATUS,
// If self attendee status = 2 it is declined by user
CalendarContract.Events.SELF_ATTENDEE_STATUS,
CalendarContract.Events.RDATE,
CalendarContract.Events.LAST_DATE
};
String[] calendarID;
for (int i = 0; i < EmailId.size(); i++) {
calendarID = new String[]{EmailId.get(i)};
Cursor c = context.getContentResolver().query(uri, projection,
CalendarContract.Instances.CALENDAR_ID + " = ?", calendarID, CalendarContract.Events.DTSTART);
if (c.moveToFirst()) {
do {
Evnt e = new Evnt();
e._id = c.getLong(0);
e.startDate = "";
e.startDate = getDate(c.getString(1));
e.startTime = "";
e.startTime = getTime(c.getString(1));
e.endDate = "";
e.endDate = getDate(c.getString(2));
e.endTime = "";
e.endTime = getTime(c.getString(2));
e.all_day = c.getInt(3);
e.title = c.getString(4);
e.color = c.getInt(5);
e.calendar_id = c.getLong(6);
e.desc = c.getString(7);
e.organizer = c.getString(8);
e.nine = c.getString(9);
e.ten = c.getString(10);
e.eleven = c.getString(11);
e.twelve = "";
e.twelve = getDate(c.getString(12));
ourEvents.add(e);
} while (c.moveToNext());
}
I want to read bookname field in database, but it does not work in this class.
How can I use this class?
public Person readPerson(String bookkname) {
Person person = null;
String[] columns = new String[]{"id" ,"bookname"};
String selection = "bookname=?";
String[] selectionArgs = new String[]{ bookkname};
String groupBy = null;
String having = null;
String orderBy = null;
String limit = null;
SQLiteDatabase database = null;
try {
database = sqLiteOpenHelper.getWritableDatabase();
Cursor cursor = database.query("tbl_persons", columns, selection, selectionArgs, groupBy, having, orderBy, limit);
if(cursor != null && cursor.moveToFirst()) {
int idIndex = 0;
int payIndex = 1;
int lastunitIndex = 2;
int lastlessenIndex = 3;
int booknameIndex = 4;
int maxfreeunitIndex = 5;
long personId = cursor.getLong(idIndex);
String personpay = cursor.getString(payIndex);
String personlastunit = cursor.getString(lastunitIndex);
String personlastlessen = cursor.getString(lastlessenIndex);
String personbookname = cursor.getString(booknameIndex);
String personmaxfreeunit = cursor.getString(maxfreeunitIndex);
person = new Person();
person.setId(personId);
person.setpay(personpay);
person.setlastunit(personlastunit);
person.setlastlessen(personlastlessen);
person.setbookname(personbookname);
person.setmaxfreeunit(personmaxfreeunit);
}
} catch (Exception ex) {
Log.d("Database", "Exception:" + ex.getMessage());
} finally {
if (database != null && database.isOpen()) {
database.close();
}
}
return person;
}
I try call readperson class like inner but cant give data from data base or give to me packagename
Please tell to me way of calling this class ?
PersonDatabaseAdapter databaseAdapteer = new PersonDatabaseAdapter(Main2Activity.this);
Person persoon = new Person();
databaseAdapteer = new PersonDatabaseAdapter(this);
username = databaseAdapteer.readPerson(pass.toString());
From my table I want to get the number of each row. For example, if I have 5 rows, I want to get 1,2,3,4,5.
I have this method, but I get only the number 1:
private void listaAvvio() {
SQLiteDatabase db = new DatabaseHelper(getActivity()).getReadableDatabase();
final List<Dettaglio1> dettagli1 = new ArrayList<Dettaglio1>();
String tabella_op = "SELECT .....";
Cursor cur = db.rawQuery(tabella_op, null);
while (cur.moveToNext()) {
Dettaglio1 d1 = new Dettaglio1();
d1.id = cur.getString(0);
d1.FIELD1= cur.getString(1);
d1.FIELD2= cur.getString(2);
d1.FIELD3= cur.getString(3);
d1.NUMBER_OF_SINGLE_ROW++;
dettagli1.add(d1);
}
cur.close();
db.close();
...
}
You are creating a new Dettaglio1 object in every loop iteration, so the NUMBER_OF_SINGLE_ROW in that object is always the same.
You have to use a separate variable to remember the row number:
int row_no = 0;
while (cur.moveToNext()) {
Dettaglio1 d1 = new Dettaglio1();
d1.id = cur.getString(0);
...
row_no++;
d1.NUMBER_OF_SINGLE_ROW = row_no;
dettagli1.add(d1);
}
Hi i had fetched all datas from the database and now i have to filter the data by matching its email id and display the filtered data in the listview but i am facing problem as i am getting the first value repeated again and again. please help me i am new in android and i am stuck on this.
I am also sending my code.
for(int i = 0;i<C_parts.size();i++){
if(C_parts.get(i).email.toString().equals(Global.useremailid.toString()))
Cursor cursor = db.query(TABLE_ACCOUNT, new String[] { ACCT_NAME,
ACCT_HEAD_NAME, OPEN_BAL, EMAIL }, EMAIL + "=?",
new String[] { String.valueOf(Global.useremailid) }, null, null, null,
null);
System.out.println("value of cursor:->"+cursor);
if (cursor != null)
cursor.moveToNext();
String acc_name = cursor.getString(0);
String acc_head_name = cursor.getString(1);
String openbal = cursor.getString(2);
String amail=cursor.getString(3);
System.out.println("bfr accountid:->"+acc_name);
System.out.println("bfr accountname:->"+acc_head_name);
System.out.println("bfr headname:->"+openbal);
C_parts1.add(new Account(acc_name,acc_head_name,openbal, amail));
}
}
c_adapter = new CustomAdapter(this, R.layout.list_item, C_parts1);
if (list1 == null)
list1 = (ListView) findViewById(R.id.lv1);
list1.setAdapter(c_adapter);
registerForContextMenu(list1);
list1.invalidate();
}
Change
for(int i = 0;i<C_parts.size();i++){
if(C_parts.get(i).email.toString().equals(Global.useremailid.toString()))
to
for(int i = 0;i<C_parts.size();i++){
if(C_parts.get(i).email.toString().equals(Global.useremailid.toString())) {
Your Cursor code will be executed only if equals, but the other code will be always executed and since the cursor is the same of the previous iteration it will repeat the same data.
It's because if you don't add { } after a if only the first line will be part of the if.
Same here
if (cursor != null)
cursor.moveToNext();
String acc_name = cursor.getString(0);
String acc_head_name = cursor.getString(1);
String openbal = cursor.getString(2);
String amail=cursor.getString(3);
System.out.println("bfr accountid:->"+acc_name);
System.out.println("bfr accountname:->"+acc_head_name);
System.out.println("bfr headname:->"+openbal);
C_parts1.add(new Account(acc_name,acc_head_name,openbal, amail));
}
add a { after if (cursor != null) and make sure every {} are OK.
while(cursor.moveToNext()){
String acc_name = cursor.getString(0);
String acc_head_name = cursor.getString(1);
String openbal = cursor.getString(2);
String amail=cursor.getString(3);
System.out.println("bfr accountid:->"+acc_name);
System.out.println("bfr accountname:->"+acc_head_name);
System.out.println("bfr headname:->"+openbal);
System.out.println("bfr email:->"+amail);
C_parts1.add(new Account(acc_name,acc_head_name,openbal, amail));
System.out.println("value of c_parts1 on for loop:->"+C_parts1.toString());
}
I am trying to get book data from my database but I keep running into a NullPointerException. I have a double array Entries that I want filled with a book item and its 5 corresponding details. I know I have extra elements in the InventoryAdapter array that I dont use in this method, but I keep it there since I use it elsewhere.
The error occurs at Entries[i][0] = InventoryAdapter.getInventoryByISBN(records[i][0])[1]; and by extension at Cursor cursor = db.rawQuery(query, new String[] {ISBN});
Why would the error be at the Cursor? I would have thought the error would probably occur with calling an array location that doesn't exist.
Main code
int numEntries = 2;
Entries = new String[numEntries][5];
int totalCount = 0;
if (BuildConfig.DEBUG)
Log.d(debug.LOG, "numEntries="+numEntries);
Toast.makeText(ReportsByDateScreen.this, "numEntries="+numEntries, Toast.LENGTH_LONG).show();
// Set up search array
for(int i = 0; i < numEntries; i++)
{
Entries[i][0] = InventoryAdapter.getInventoryByISBN(records[i][0])[1];
Entries[i][1] = InventoryAdapter.getInventoryByISBN(records[i][0])[2];
Entries[i][2] = InventoryAdapter.getInventoryByISBN(records[i][0])[4];
Entries[i][3] = InventoryAdapter.getInventoryByISBN(records[i][0])[3];
for(int j = 0; j < records.length; j++)
{
if(records[j][0].equals(InventoryAdapter.getInventoryByISBN(records[i][0])[0]))
{
totalCount+=Integer.parseInt(InventoryAdapter.getInventoryByISBN(records[i][0])[8]);
}
}
Entries[i][4] = ((Integer)totalCount).toString();
reportArray.add(new ReportsItem(records[i][0],Entries[i]));
totalCount = 0;
}
InventoryAdapter
public static String[] getInventoryByISBN(String ISBN)
{
String[] entry = new String[9];
//Query
String query = "select * from INVENTORY where ISBN = ?";
Cursor cursor = db.rawQuery(query, new String[] {ISBN});
if(cursor.getCount()<1) // title Not Exist
{
cursor.close();
for(int i = 0; i < 9; i++)
entry[i] = "Not Found";
return entry;
}
cursor.moveToFirst();
//put data into respective variable
int publish = cursor.getInt(cursor.getColumnIndex("PUBLISH_DATE"));
String publishdate = ((Integer)publish).toString();
String title = cursor.getString(cursor.getColumnIndex("TITLE"));
String author = cursor.getString(cursor.getColumnIndex("AUTHOR"));
String callNumber = cursor.getString(cursor.getColumnIndex("CALL_NUMBER"));
int available = cursor.getInt(cursor.getColumnIndex("AVAILABLE_COUNT"));
String availablecount = ((Integer)available).toString();
int inventory = cursor.getInt(cursor.getColumnIndex("INVENTORY_COUNT"));
String inventorycount = ((Integer)inventory).toString();
int due = cursor.getInt(cursor.getColumnIndex("DUE_PERIOD"));
String dueperiod = ((Integer)due).toString();
int checkoutcount = cursor.getInt(cursor.getColumnIndex("COUNT"));
String count = ((Integer)checkoutcount).toString();
//combine variables into one array
entry[0] = ISBN;
entry[1] = title;
entry[2] = author;
entry[3] = publishdate;
entry[4] = callNumber;
entry[5] = availablecount;
entry[6] = inventorycount;
entry[7] = dueperiod;
entry[8] = count;
cursor.close();
return entry;
}
There is 2 different possibilities here :
ISBN is null (and by extension records[i][0] is null too);
db is null
I'll look into that if I were you.
Use the debugger.
I can tell you much because you don't post the code of your init of the SQLiteDatabase.