android怎么查询并显示数据库

   2025-02-15 6820
核心提示:在Android中,可以使用SQLite数据库来存储和查询数据。以下是一个简单的示例,演示如何查询并显示数据库中的数据:首先,在Andro

在Android中,可以使用SQLite数据库来存储和查询数据。以下是一个简单的示例,演示如何查询并显示数据库中的数据:

首先,在Android项目的app/build.gradle文件中添加SQLite依赖项:
dependencies {    implementation 'androidx.sqlite:sqlite:2.1.0'    implementation 'androidx.sqlite:sqlite-framework:2.1.0'}
创建一个DatabaseHelper类来管理数据库的创建和升级:
import android.content.Context;import android.database.Cursor;import android.database.sqlite.SQLiteDatabase;import android.database.sqlite.SQLiteOpenHelper;public class DatabaseHelper extends SQLiteOpenHelper {    private static final String DATABASE_NAME = "mydatabase.db";    private static final int DATABASE_VERSION = 1;    private static final String TABLE_NAME = "mytable";    private static final String COLUMN_ID = "id";    private static final String COLUMN_NAME = "name";    private static final String COLUMN_AGE = "age";    public DatabaseHelper(Context context) {        super(context, DATABASE_NAME, null, DATABASE_VERSION);    }    @Override    public void onCreate(SQLiteDatabase db) {        String createTableQuery = "CREATE TABLE " + TABLE_NAME + "("                + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"                + COLUMN_NAME + " TEXT,"                + COLUMN_AGE + " INTEGER)";        db.execSQL(createTableQuery);    }    @Override    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {        String dropTableQuery = "DROP TABLE IF EXISTS " + TABLE_NAME;        db.execSQL(dropTableQuery);        onCreate(db);    }    public Cursor getAllData() {        SQLiteDatabase db = this.getReadableDatabase();        return db.query(TABLE_NAME, null, null, null, null, null, null);    }}
在需要显示数据库的地方,实例化DatabaseHelper类并查询数据:
import androidx.appcompat.app.AppCompatActivity;import android.database.Cursor;import android.os.Bundle;import android.widget.TextView;public class MainActivity extends AppCompatActivity {    private TextView textViewData;    private DatabaseHelper databaseHelper;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);                textViewData = findViewById(R.id.text_view_data);        databaseHelper = new DatabaseHelper(this);        Cursor cursor = databaseHelper.getAllData();        StringBuilder stringBuilder = new StringBuilder();        if (cursor.moveToFirst()) {            do {                String name = cursor.getString(cursor.getColumnIndex(DatabaseHelper.COLUMN_NAME));                int age = cursor.getInt(cursor.getColumnIndex(DatabaseHelper.COLUMN_AGE));                stringBuilder.append("Name: ").append(name).append(", Age: ").append(age).append("\n");            } while (cursor.moveToNext());        }        textViewData.setText(stringBuilder.toString());        cursor.close();        databaseHelper.close();    }}
在布局文件activity_main.xml中添加一个TextView控件用于显示数据:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    android:padding="16dp">    <TextView        android:id="@+id/text_view_data"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:textAppearance="?android:attr/textAppearanceMedium" /></LinearLayout>

这样,当应用程序启动时,它将查询数据库并在TextView中显示数据。请确保在查询数据库后关闭游标和数据库连接,以避免资源泄漏。

 
 
更多>同类维修知识
推荐图文
推荐维修知识
点击排行
网站首页  |  关于我们  |  联系方式  |  用户协议  |  隐私政策  |  网站留言