image 我想得到整行我使用此代码来获取我想要的数据,但它在temp_address1给我空指针异常这里是代码
如上所述,我希望得到整行匹配的电话号码。我怎么能得到这个结果?电话号码是这里的主要关键 公共类SqliteHelpers扩展了SQLiteOpenHelper {
public static final String DATABASE_NAME = "NotesDatabase.db";
public static final int DATABASE_VERSION = 1;
public static final String TABLE_NAME = "notesDb";
public static final String NUMBER = "PhoneNumber";
public static final String NOTES = "Note";
public static final String PICTURE = "Picture";
public static final String TABLE_CREATE =
"CREATE TABLE " +
TABLE_NAME + "(" +
NUMBER + " TEXT PRIMAY KEY , " +
NOTES + " TEXT UNIQUE , " +
PICTURE + " TEXT " + " ) ";
public SqliteHelpers(Context context) {
super(context, DATABASE_NAME, null, 1);
getReadableDatabase();
getWritableDatabase();
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(TABLE_CREATE);
Log.i("DBHelpers", "table created");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS" + TABLE_NAME);
}
和第二堂课是
公共类DataBaseHelpers {
private SQLiteDatabase mDbHelper;
private SqliteHelpers mSqliteHelper;
private Cursor mCursor;
public DataBaseHelpers(Context context) {
mSqliteHelper = new SqliteHelpers(context);
}
void createNewEntry(String column, String value) {
ContentValues values = new ContentValues();
values.put(column, value);
mDbHelper.insert(SqliteHelpers.TABLE_NAME, null, values);
Log.i("Sqlite", "create entry");
}
void deleteItem(String column, String value) {
mDbHelper.delete(SqliteHelpers.TABLE_NAME, column + " = ?", new String[]{value});
Log.i("sqlite", "Entry deleted");
}
void closeDatabase() {
mSqliteHelper.close();
Log.i("sqlite", "close database");
}
void retrieveDate(String value) {
String query = "SELECT * FROM notesDb WHERE PhoneNumber ="
+ value;
mCursor = mDbHelper.rawQuery(query, null);
if (mCursor.moveToFirst()) {
do {
String temp_address = mCursor.getString(mCursor.getColumnIndex(SqliteHelpers.NUMBER));
String temp_address1 = mCursor.getString(mCursor.getColumnIndex(SqliteHelpers.NOTES));
System.out.println(temp_address);
System.out.println(temp_address1);
} while (cursor.moveToNext());
}
mDbHelper.close();
}
答案 0 :(得分:2)
你可以像下面的代码
那样做public void rec(int _id){
String query = "SELECT * FROM notesDb WHERE PhoneNumber ="
+ _id;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(query, null);
if (cursor.moveToFirst()) {
do {
String temp_address = c.getString(0);
String temp_address1 = c.getString(1);
System.out.println(temp_address);
System.out.println(temp_address1);
} while (cursor.moveToNext());
}
db.close();
}