android code to fetch data from database
ParsedNotificationDataSet result = new ParsedNotificationDataSet();
C开发者_StackOverflow社区ursor c = db.rawQuery("select * from notificationtable", null);
if (c.getCount() > 0) {
c.moveToFirst();
do {
result.setclassurl(c.getString(c.getColumnIndex("Id")));
result.settype(c.getString(c.getColumnIndex("type")));
result.setschool(c.getString(c.getColumnIndex("school")));
result.setdescription(c.getString(c.getColumnIndex("description")));
result.settitle(c.getString(c.getColumnIndex("title")));
result.setdatePosted(c.getString(c.getColumnIndex("datePosted")));
results.add(result);
} while (c.moveToNext());
}
Here is my code to fetch data from the database. In my opinion it should work properly but at the very first time it generates a java.lang.NullPointerException
exception. After that it runs without a problem. What modifications do I need to make to resolve this issue?
You can try the below code. I think it will work and will not give a NullPointerException
error.
Cursor c = db.rawQuery("select * from notificationtable", null);
if (c.getCount() > 0) {
while (cursor.moveToNext()) {
result.setclassurl(c.getString(c.getColumnIndex("Id")));
result.settype(c.getString(c.getColumnIndex("type")));
result.setschool(c.getString(c.getColumnIndex("school")));
result.setdescription(c.getString(c.getColumnIndex("description")));
result.settitle(c.getString(c.getColumnIndex("title")));
result.setdatePosted(c.getString(c.getColumnIndex("datePosted")));
results.add(result);
}
}
I have used database this way. You can try this way. You first create ContactDbAdapter class file like below..
public class ContactDbAdapter {
public static final String KEY_NAME = "name";
public static final String KEY_SCORE = "score";
public static final String KEY_ROWID = "_id";
private static final String DATABASE_CREATE = "create table contactdetails (_id integer primary key autoincrement,"
+ "name text not null, score integer not null);";
private static final String DATABASE_NAME = "contacts";
private static final String DATABASE_TABLE = "contactdetails";
private static final int DATABASE_VERSION = 3;
public static final String TAG = "ContactDbAdapter";
private final Context mCtx;
private DatabaseHelper mDbHelper;
private SQLiteDatabase mDb;
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS contactdetails");
onCreate(db);
}
}
public ContactDbAdapter open() throws SQLiteException {
mDbHelper = new DatabaseHelper(mCtx);
try {
mDb = mDbHelper.getWritableDatabase();
} catch (SQLiteException ex) {
mDb = mDbHelper.getReadableDatabase();
}
return this;
}
public ContactDbAdapter(Context ctx) {
this.mCtx = ctx;
}
public void close() {
mDbHelper.close();
}
public long createContact(String name, int score) {
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_NAME, name);
initialValues.put(KEY_SCORE, score);
return mDb.insert(DATABASE_TABLE, null, initialValues);
}
public int updateEntry(long _rowIndex, String name, int score) {
String where = KEY_ROWID + "=" + _rowIndex;
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_NAME, name);
initialValues.put(KEY_SCORE, score);
// TODO fill in the ContentValue based on the new object
return mDb.update(DATABASE_TABLE, initialValues, where, null);
}
public boolean deleteContact(long rowId) {
Toast.makeText(this.mCtx, "RowID:" + rowId, Toast.LENGTH_LONG).show();
return mDb.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
}
public Cursor fetchAllContacts() {
String order = KEY_SCORE + " DESC ";
return mDb.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_NAME,
KEY_SCORE}, null, null, null, null, order);
}
}
And than you can call directly this way in you activity java class.
private ContactDbAdapter mDbHelper;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mDbHelper = new ContactDbAdapter(this);
mDbHelper.open();
Cursor cursor = mDbHelper.fetchAllContacts();
startManagingCursor(cursor);
}
So this will fetch all the records from the table. And then cursor will be used in the same way as you used.
精彩评论