我如何从android中的Sqlite数据库获取和使用信息

时间:2017-07-15 15:22:52

标签: java android sqlite

这是我的代码和我的CarsDbHelper,CarsContract用于我的CarsEntry。

它没有问题,但我不确定如何使用我在Sqlite数据库中放置的信息。

String current_car_name = "TOYOTA YARIS";
String current_car_colour = "GREY";
int current_car_age = 3;

CarsDbHelper mDbHelper = new CarsDbHelper(this);

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    SQLiteDatabase db = mDbHelper.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(CarsEntry.COLUMN_NAME,current_car_name);
    values.put(CarsEntry.COLUMN_COLOUR,current_car_colour);
    values.put(CarsEntry.COLUMN_AGE,current_car_age);
    long newRowId = db.insert(CarsEntry.TABLE_NAME, null, values);
}

SQLiteDatabase db = mDbHelper.getReadableDatabase();

String[] projection = {
        CarsEntry.COLUMN_NAME,
        CarsEntry.COLUMN_COLOUR,
        CarsEntry.COLUMN_AGE
};

public void button (View view) {
    String selection = CarsEntry.COLUMN_NAME + " = ?";
    String[] selectionArgs = {current_car_name};

    TextView car_name = (TextView) findViewById(R.id.name);
    car_name.setText();

    TextView car_colour = (TextView) findViewById(R.id.colour);
    car_colour.setText();

    TextView car_age = (TextView) findViewById(R.id.age);
    car_age.setText();
}

2 个答案:

答案 0 :(得分:0)

  

我仍然对如何将这些数据输入我的TextViews感到困惑。

你输入它们......你的意思是从它们中获取价值吗?

首先,“帮助器”对象的目的是帮助您不暴露SQLiteDatabase对象。

在帮助程序中创建Car类和addCar(Car car)方法。

public long addCar(Car car) {
    SQLiteDatabase db = getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(CarsEntry.COLUMN_NAME,car.getName());
    values.put(CarsEntry.COLUMN_COLOUR,car.getColour());
    values.put(CarsEntry.COLUMN_AGE,car.getAge());

    long newRowId = db.insert(CarsEntry.TABLE_NAME, null, values);
    return newRowId;
}

然后,在活动中,您需要在onCreate中设置帮助程序,而不是在

之外
CarsDbHelper mDbHelper;
TextView mCarName, mCarColour, mCarAge;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mCarName = (TextView) findViewById(R.id.name);    
    mCarColour = (TextView) findViewById(R.id.colour);
    mCarAge = (TextView) findViewById(R.id.age);

    mDbHelper = new CarsDbHelper(this);  // Moved in here
}

public void button (View view) {
    String name = mCarName.getText().toString();
    String colour = mCarColour.getText().toString();
    String age = mCarAge.getText().toString();

    Car c = new Car(name, colour, Integer.parseInt(age));
    mDbHelper.addCar(c); // This is the helper method
}

如果要从数据库中获取数据,则需要ListView和适配器(例如CursorAdapter)

如果您不想弄乱原始SQLite的详细信息,请参阅Room Persistence library

答案 1 :(得分:-1)

首先,您应该在onCreate Function中创建db实例。

Cursor cursor = db.query(
TABLE_NAME,                     // The table to query
projection,                               // The columns to return
selection,                                // The columns for the WHERE clause
selectionArgs,                            // The values for the WHERE clause
null,                                     // don't group the rows
null,                                     // don't filter by row groups
sortOrder                                 // The sort order
);

从Android Dev Site复制。

您将获得光标,按如下方式迭代:

if(!cursor.moveToFirst()) 
   //No Data 
//cache columns indexes in integers 
int idIndex = cursor.getColumnIndex("id"); 
... 
while(cursor.moveToNext()){ 
  String id = cursor.getString(id); //Same for boolean, Int, long, etc. 
  ... 
} 
//Never forget to close cursor 
cursor.close();