Android Studio开发输入保存到数据库的APP

在移动应用开发中,保存用户输入数据到数据库是常见的需求。本文将介绍如何使用Android Studio开发一个简单的应用,实现用户输入数据保存到数据库的功能。

准备工作

首先,你需要安装Android Studio并创建一个新的项目。在项目中,我们将使用SQLite作为本地数据库存储用户输入的数据。

创建数据库

在Android Studio中,我们可以使用SQLiteOpenHelper类来创建和管理数据库。以下是一个简单的示例代码,用于创建一个名为"UserDB"的数据库,并在其中创建一个名为"User"的表格,用于存储用户的姓名和年龄信息。

public class DBHelper extends SQLiteOpenHelper {
    private static final String DATABASE_NAME = "UserDB";
    private static final int DATABASE_VERSION = 1;

    private static final String TABLE_NAME = "User";
    private static final String COLUMN_ID = "id";
    private static final String COLUMN_NAME = "name";
    private static final String COLUMN_AGE = "age";

    private static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + "("
            + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
            + COLUMN_NAME + " TEXT,"
            + COLUMN_AGE + " INTEGER"
            + ")";

    public DBHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_TABLE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
        onCreate(db);
    }
}

在上述代码中,我们创建了一个名为DBHelper的类,继承自SQLiteOpenHelper。在该类中,我们定义了数据库的名称、版本号、表格名称和列名,并实现了创建数据库和升级数据库的方法。

添加用户界面

接下来,我们需要为用户提供一个界面,用于输入姓名和年龄信息。在布局文件中添加两个EditText用于输入姓名和年龄,以及一个Button用于保存数据到数据库。

<EditText
    android:id="@+id/editTextName"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="Name"/>

<EditText
    android:id="@+id/editTextAge"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="Age"/>

<Button
    android:id="@+id/buttonSave"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Save"/>

保存数据到数据库

在Activity中,我们需要获取用户输入的姓名和年龄信息,并将其保存到数据库中。以下是保存数据的示例代码:

Button buttonSave = findViewById(R.id.buttonSave);
buttonSave.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        String name = ((EditText) findViewById(R.id.editTextName)).getText().toString();
        int age = Integer.parseInt(((EditText) findViewById(R.id.editTextAge)).getText().toString());

        DBHelper dbHelper = new DBHelper(MainActivity.this);
        SQLiteDatabase db = dbHelper.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put("name", name);
        values.put("age", age);

        db.insert("User", null, values);
        db.close();

        Toast.makeText(MainActivity.this, "Data saved successfully", Toast.LENGTH_SHORT).show();
    }
});

在上述代码中,我们首先获取用户输入的姓名和年龄信息,然后创建一个ContentValues对象,将数据插入到数据库中,并显示保存成功的提示信息。

查询数据库

如果我们想要查看数据库中保存的数据,可以添加一个Button用于查询并显示数据。以下是查询数据库的示例代码:

Button buttonView = findViewById(R.id.buttonView);
buttonView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        DBHelper dbHelper = new DBHelper(MainActivity.this);
        SQLiteDatabase db = dbHelper.getReadableDatabase();

        Cursor cursor = db.rawQuery("SELECT * FROM User", null);
        if (cursor.moveToFirst()) {
            do {
                String name = cursor.getString(cursor.getColumnIndex("name"));
                int age = cursor.getInt(cursor.getColumnIndex("age"));

                Log.d("User", "Name: " + name + ", Age: " + age);
            } while (cursor.moveToNext());
        }
        cursor.close();
        db.close();
    }
});

在上述代码中,我们查询了User表格中的所有数据,并通过Log输出到控制台。

总结

通过以上的步骤,我们实现了一个简单的Android应用,可以将用户输入的姓名和年龄信息保存