繁体   English   中英

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

[英]How do i get and use information from a Sqlite database in android

这是我的代码以及我的CarsEntry的CarsDbHelper和CarsContract。

它没有问题,但是从这里我不确定如何使用已放入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();
}

对于如何将这些数据输入到TextView中,我仍然有些困惑。

您输入内容...是不是要从中获取价值?

首先,“ helper”对象的目的是帮助您不公开SQLiteDatabase对象。

在助手中addCar(Car car)一个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和一个Adapter(例如CursorAdapter)

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

首先,您应该在onCreate Function中创建数据库实例。

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();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM