android sqlite查询数据
时间: 2023-05-31 08:19:38 浏览: 84
### 回答1:
Android中使用SQLite查询数据可以通过以下步骤实现:
1. 创建SQLiteOpenHelper类,继承自SQLiteOpenHelper,实现onCreate()和onUpgrade()方法。
2. 在onCreate()方法中创建数据库表。
3. 在查询数据的Activity中,创建SQLiteOpenHelper对象,并调用getReadableDatabase()方法获取可读的数据库对象。
4. 调用query()方法进行查询,该方法返回一个Cursor对象,可以通过该对象获取查询结果。
5. 遍历Cursor对象,获取查询结果。
示例代码如下:
1. 创建SQLiteOpenHelper类
public class MyDatabaseHelper extends SQLiteOpenHelper {
private static final String DB_NAME = "my_database.db";
private static final int DB_VERSION = 1;
public MyDatabaseHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE IF NOT EXISTS student (_id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, age INTEGER)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS student");
onCreate(db);
}
}
2. 查询数据的Activity中使用SQLiteOpenHelper
public class MainActivity extends AppCompatActivity {
private MyDatabaseHelper dbHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dbHelper = new MyDatabaseHelper(this);
SQLiteDatabase db = dbHelper.getReadableDatabase();
Cursor cursor = db.query("student", null, null, null, null, null, null);
if (cursor.moveToFirst()) {
do {
int id = cursor.getInt(cursor.getColumnIndex("_id"));
String name = cursor.getString(cursor.getColumnIndex("name"));
int age = cursor.getInt(cursor.getColumnIndex("age"));
Log.d("MainActivity", "id: " + id + ", name: " + name + ", age: " + age);
} while (cursor.moveToNext());
}
cursor.close();
}
@Override
protected void onDestroy() {
super.onDestroy();
dbHelper.close();
}
}
以上代码实现了查询student表中的所有数据,并输出到Logcat中。在实际开发中,可以根据需要修改查询条件和输出方式。
### 回答2:
Android平台中数据库是应用程序不可或缺的一部分,SQLite作为轻量级的嵌入式数据库,被广泛用于Android平台的应用程序中。SQLite不仅有高效的操作性能,而且还有易于开发和维护的优点。本文将讲解Android平台中SQLite的查询操作。
一、查询数据表中所有数据
查询所有数据是SQLite查询的最基本的操作,通过调用query方法可以很容易地完成这项工作,具体步骤如下:
1、调用getReadableDatabase方法获取读写权限的数据库
2、定义一个字符串数组作为查询的返回结果,为查询数据表中所有列
3、调用query方法查询数据表,将返回结果保存到游标中
4、通过游标遍历查询结果,并将结果保存到相应的数据结构中
以下是具体实现代码:
```
public ArrayList<Person> getAll() {
ArrayList<Person> persons = new ArrayList<Person>();
SQLiteDatabase db = getReadableDatabase();
String[] columns = {
KEY_ID,
KEY_NAME,
KEY_AGE,
KEY_GENDER,
KEY_PHONE_NUMBER
};
Cursor cursor = db.query(TABLE_NAME, columns, null,
null, null, null, null);
if (cursor.moveToFirst()) {
do {
Person person = new Person();
person.setId(cursor.getInt(cursor.getColumnIndex(KEY_ID)));
person.setName(cursor.getString(cursor.getColumnIndex(KEY_NAME)));
person.setAge(cursor.getInt(cursor.getColumnIndex(KEY_AGE)));
person.setGender(cursor.getInt(cursor.getColumnIndex(KEY_GENDER)));
person.setPhoneNumber(cursor.getString(cursor.getColumnIndex(KEY_PHONE_NUMBER)));
persons.add(person);
} while (cursor.moveToNext());
}
cursor.close();
db.close();
return persons;
}
```
在上述代码中,我们定义了一个名为getAll的方法,用于查询数据表中所有数据,它返回一个ArrayList类型的数据结构,其中存储了查询结果。我们首先获取了一个具有读写权限的数据库,然后定义了一个字符串数组表示了要查询的所有列。接着,我们调用了query方法查询了数据表,将查询结果保存到了游标中。最后,我们通过游标遍历了查询结果,并将结果保存到一个ArrayList数据结构中。
二、查询数据表中特定的数据
除了查询所有数据以外,我们还可以通过修改query方法的参数,来查询数据表中符合特定条件的数据,例如查询年龄大于等于20岁的人员信息。具体实现步骤如下:
1、定义一个字符串数组作为查询的返回结果,为要查询的数据表中所有列
2、定义一个字符串数组作为查询条件,查询条件语句为"age>=?"
3、定义一个字符串数组作为查询条件的参数,代表查询条件中的问号,此处为"20"
4、调用query方法,获取查询结果
5、通过游标遍历查询结果,并将结果保存到相应的数据结构中
以下是查询年龄大于等于20岁的特定人员的具体实现代码:
```
public ArrayList<Person> getFilteredByAge(int age) {
ArrayList<Person> persons = new ArrayList<Person>();
SQLiteDatabase db = getReadableDatabase();
String[] columns = {
KEY_ID,
KEY_NAME,
KEY_AGE,
KEY_GENDER,
KEY_PHONE_NUMBER
};
String selection = KEY_AGE + "> =?";
String[] selectionArgs = {String.valueOf(age)};
Cursor cursor = db.query(TABLE_NAME, columns, selection,
selectionArgs, null, null, null);
if (cursor.moveToFirst()) {
do {
Person person = new Person();
person.setId(cursor.getInt(cursor.getColumnIndex(KEY_ID)));
person.setName(cursor.getString(cursor.getColumnIndex(KEY_NAME)));
person.setAge(cursor.getInt(cursor.getColumnIndex(KEY_AGE)));
person.setGender(cursor.getInt(cursor.getColumnIndex(KEY_GENDER)));
person.setPhoneNumber(cursor.getString(cursor.getColumnIndex(KEY_PHONE_NUMBER)));
persons.add(person);
} while (cursor.moveToNext());
}
cursor.close();
db.close();
return persons;
}
```
可以看到,在查询年龄大于等于20岁的人员信息时,我们定义了一个名为getFilteredByAge的方法。该方法接受年龄参数,用于筛选出符合条件的数据。我们首先定义了一个查询结果字符串数组和查询语句字符串数组,接着定义了一个查询条件参数,代表了查询条件中的问号。然后,我们调用了query方法,获取了符合条件的查询结果,并将其保存到游标中。最后,我们通过游标遍历了查询结果,并将结果保存到一个ArrayList数据结构中。
总结
本文主要讲解了在Android平台中,如何使用SQLite查询数据表中的数据。无论是查询所有数据,还是查询特定的数据,都可以通过修改query方法的参数来实现,十分简单易懂。当然,如果查询条件比较复杂,我们还可以使用select语句完成更为复杂的查询操作。
### 回答3:
Android中的SQLite是一种轻量级的数据库存储技术,它允许应用程序使用内嵌式数据库来存储数据。SQLite数据库可用于存储小型数据集,如应用程序和游戏的配置设置,或特定任务所需的数据。SQLite查询数据的过程包括以下步骤:
1.创建一个SQLiteOpenHelper类的子类。这个类负责打开和管理SQLite数据库。在这个类中,需要创建数据库、指定表格、定义列和数据类型。
2.编写可执行查询的工具方法。在这个方法中,可以对表格执行标准SQL语句,如SELECT、INSERT、UPDATE和DELETE。
3.使用SQLiteDatabase类对象调用工具方法,从而查询数据。
4.将查询结果从Cursor对象中提取出来,并将其显示在界面上。
在Android中,最常见的SQLite查询数据是SELECT语句。下面是一个简单的代码示例,它演示了如何查询SQLite数据库中的数据并在ListView中显示它们:
public ArrayList<String> getAllNames() {
ArrayList<String> namesList = new ArrayList<>();
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_NAME, new String[] {NAME_COLUMN},
null, null, null, null, null);
if (cursor.moveToFirst()) {
do {
String name = cursor.getString(cursor.getColumnIndex(NAME_COLUMN));
namesList.add(name);
} while (cursor.moveToNext());
}
cursor.close();
db.close();
return namesList;
}
以上代码中,getAllNames()方法返回一个字符串数组,其中包含从表格中检索到的所有名称。该数组由SQLiteOpenHelper类的子类继承,使用db.query()方法从表格中查询数据,返回一个Cursor对象。使用while循环迭代检索到的结果,并将每个结果添加到ArrayList中。最后,关闭Cursor和数据库,并返回结果列表。
总的来说,Android中的SQLite查询是一个简单的过程,需要创建一个SQLiteOpenHelper类的子类来管理和打开SQLite数据库,编写可执行的查询语句,然后使用SQLiteDatabase类对象从数据库中检索数据。使用Cursor对象提取检索到的结果,并在应用程序的用户界面上展示它们。