select 数据按照Sqlite中的Row
select data according to Row in Sqlite
image
我想获取整行我正在使用此代码获取我想要的数据但是它在 temp_address1 处给我空指针异常这里是代码
如上图所示,我希望整行匹配 phone 号。我怎样才能达到那个结果? phone这里number是主键
public class 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);
}
第二个 class 是
public class 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();
}
你可以像下面的代码那样做
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();
}
image 我想获取整行我正在使用此代码获取我想要的数据但是它在 temp_address1 处给我空指针异常这里是代码
如上图所示,我希望整行匹配 phone 号。我怎样才能达到那个结果? phone这里number是主键 public class 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);
}
第二个 class 是
public class 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();
}
你可以像下面的代码那样做
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();
}