91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

android怎么查詢并顯示數據庫

小億
156
2023-11-10 19:28:37
欄目: 編程語言

在Android中,可以使用SQLite數據庫來存儲和查詢數據。以下是一個簡單的示例,演示如何查詢并顯示數據庫中的數據:

  1. 首先,在Android項目的app/build.gradle文件中添加SQLite依賴項:
dependencies {
    implementation 'androidx.sqlite:sqlite:2.1.0'
    implementation 'androidx.sqlite:sqlite-framework:2.1.0'
}
  1. 創建一個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);
    }
}
  1. 在需要顯示數據庫的地方,實例化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();
    }

}
  1. 在布局文件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中顯示數據。請確保在查詢數據庫后關閉游標和數據庫連接,以避免資源泄漏。

0
隆回县| 那曲县| 和平县| 日喀则市| 霍林郭勒市| 循化| 湛江市| 红河县| 治县。| 安丘市| 湘阴县| 桦川县| 莱州市| 元阳县| 长阳| 肇源县| 建阳市| 那坡县| 和硕县| 陇西县| 迭部县| 澄迈县| 渝北区| 长乐市| 二连浩特市| 双城市| 临泽县| 平果县| 界首市| 亳州市| 镇坪县| 双流县| 衡水市| 兰州市| 鲁山县| 沽源县| 信丰县| 阳朔县| 台北县| 容城县| 瑞金市|