5.4.4实战演练——绿豆通讯录

image
源代码下载:https://wwvn.lanzoul.com/ilwCw2su3j9c
sqliteexpert_58599软件下载:https://wwvn.lanzoul.com/iwJNC2su3luf
5.4 sqlite数据库的创建 (代码编辑器是android studio 3.5.3,请提供项目的目录和具体的操作步骤说明。)
MyApplication/
├── app/
│ ├── src/
│ │ ├── main/
│ │ │ ├── java/
│ │ │ │ └── com.example.myapplication/
│ │ │ │ ├── MainActivity.java
│ │ │ │ └── DBHelper.java
│ │ │ ├── res/
│ │ │ │ ├── layout/
│ │ │ │ │ └── activity_main.xml
│ │ │ │ └── values/
│ │ │ │ └── strings.xml
│ │ │ └── AndroidManifest.xml
│ │ └── test/
│ │ └── java/
│ │ └── com.example.myapplication/
│ │ └── ExampleUnitTest.java
│ └── build.gradle
├── build/
├── gradle/
├── .gradle/
├── .idea/
├── .gitignore
├── build.gradle
├── gradlew
├── gradlew.bat
├── settings.gradle
└── README.md
DBHelper.java 代码如下:

package com.example.myapplication;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DBHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "contacts.db"; // 数据库名称
private static final int DATABASE_VERSION = 1; // 数据库版本

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

@Override
public void onCreate(SQLiteDatabase db) {
    // 创建表的 SQL 语句
    String CREATE_TABLE = "CREATE TABLE IF NOT EXISTS contacts (" +
            "id INTEGER PRIMARY KEY AUTOINCREMENT, " +
            "name TEXT, " +
            "phone TEXT)";
    db.execSQL(CREATE_TABLE); // 执行创建表的 SQL 语句
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // 如果数据库版本升级,删除旧表并重新创建
    db.execSQL("DROP TABLE IF EXISTS contacts");
    onCreate(db);
}

}

activity_main.xml 代码如下:

<!-- 标题 -->
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"

    android:textSize="24sp"
    android:textColor="#FFFFFF"
    android:layout_gravity="center_horizontal"
    android:paddingBottom="16dp"/>

<!-- 姓名输入框 -->
<EditText
    android:id="@+id/editTextName"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="请输入姓名"
    android:layout_marginTop="100dp"
   android:layout_marginBottom="8dp"/>

<!-- 电话输入框 -->
<EditText
    android:id="@+id/editTextPhone"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="请输入手机号码"
    android:layout_marginBottom="16dp"/>

<!-- 操作按钮 -->
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:gravity="center">

    <Button
        android:id="@+id/buttonInsert"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="添加"
        android:layout_marginEnd="8dp"/>

    <Button
        android:id="@+id/buttonQuery"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="查询"
        android:layout_marginEnd="8dp"/>

    <Button
        android:id="@+id/buttonUpdate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="修改"
        android:layout_marginEnd="8dp"/>

    <Button
        android:id="@+id/buttonDelete"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="删除"/>
</LinearLayout>

MainActivity.java 代码如下:

<!-- 标题 -->
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"

    android:textSize="24sp"
    android:textColor="#FFFFFF"
    android:layout_gravity="center_horizontal"
    android:paddingBottom="16dp"/>

<!-- 姓名输入框 -->
<EditText
    android:id="@+id/editTextName"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="请输入姓名"
    android:layout_marginTop="100dp"
   android:layout_marginBottom="8dp"/>

<!-- 电话输入框 -->
<EditText
    android:id="@+id/editTextPhone"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="请输入手机号码"
    android:layout_marginBottom="16dp"/>

<!-- 操作按钮 -->
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:gravity="center">

    <Button
        android:id="@+id/buttonInsert"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="添加"
        android:layout_marginEnd="8dp"/>

    <Button
        android:id="@+id/buttonQuery"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="查询"
        android:layout_marginEnd="8dp"/>

    <Button
        android:id="@+id/buttonUpdate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="修改"
        android:layout_marginEnd="8dp"/>

    <Button
        android:id="@+id/buttonDelete"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="删除"/>
</LinearLayout>
posted @ 2025-04-06 17:11  大胡子编程  阅读(235)  评论(0)    收藏  举报