<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/creat"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Create database"/>
<Button
android:id="@+id/add"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Add data"/>
<Button
android:id="@+id/updata"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Updata data"/>
<Button
android:id="@+id/deletedata"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Delete data"/>
</LinearLayout>
private void queryStudents() {
// 相当于 select * from students 语句
Cursor cursor = mSQLiteDatabase.query(SQLiteDbHelper.TABLE_STUDENT, null,
"cls_id > ? and id >= 1", new String[]{"3"},
null, null, null, null);
// 不断移动光标获取值
while (cursor.moveToNext()) {
// 直接通过索引获取字段值
int stuId = cursor.getInt(0);
// 先获取 name 的索引值,然后再通过索引获取字段值
String stuName = cursor.getString(cursor.getColumnIndex("name"));
Log.e("", "id: " + stuId + " name: " + stuName);
}
// 关闭光标
cursor.close();
}