Android Studio SQLite数据库实现奶茶店菜单管理

界面及功能:

       

  1. 第一次使用时点击最上方”CREATE DATABASE”按钮可创建一个本地数据库
  2. 输入品名及对应价格点击“插入“可插入一条商品
  3. 输入对应品名及新价格点击“更新“可更新指定品名的价格
  4. 输入品名点击“查询“可查询对应价格
    1. 不输入直接点击”查询”可查询整个菜单
  5. 输入品名点击“删除“可删除一条商品

MainActivity:

package com.example.myapplicationsqlite;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    private MyDatabaseHelper dbHelper;//定义Helper对象
    private Button insertButton,updateButton,searchButton,deleteButton;//增删改查按键
    private EditText name,price;//品名、价格输入框
    private TextView show,showPrice;//显示框
    private Button createDatabase;//创建框
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        createDatabase=findViewById(R.id.create_database);//链接页面中的控件
        insertButton=findViewById(R.id.btn_insert);
        updateButton=findViewById(R.id.btn_update);
        searchButton=findViewById(R.id.btn_search);
        deleteButton=findViewById(R.id.btn_delete);
        name=findViewById(R.id.name);
        price=findViewById(R.id.price);
        show=findViewById(R.id.tv_show);
        showPrice=findViewById(R.id.tv_showPrice);

        dbHelper = new MyDatabaseHelper(this,"NaichaStore.db",null,2);

        createDatabase.setOnClickListener(new View.OnClickListener() {//创建按钮监听
            @Override
            public void onClick(View v) {
                dbHelper.getWritableDatabase();//创建数据库
            }
        });

        insertButton.setOnClickListener(new View.OnClickListener() {//插入按钮监听
            @Override
            public void onClick(View view) {
                SQLiteDatabase db=dbHelper.getWritableDatabase();//链接数据库
                ContentValues values=new ContentValues();
                values.put("name",name.getText().toString());//读取输入的文本插入
                values.put("price",price.getText().toString());
                db.insert("Naicha",null,values);//调用插入函数
                Log.d("new","insert");//更新日志

                show();//输出函数
                db.close();//关闭数据库
                name.setText(null);//清空输入框
                price.setText(null);
            }
        });

        updateButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SQLiteDatabase db=dbHelper.getWritableDatabase();
                ContentValues values=new ContentValues();
                values.put("price",price.getText().toString());//读取数据
                db.update("Naicha",values,"name=?",new String[]{name.getText().toString()});//调用更新函数

                show();
                db.close();
                Log.d("new","update");
                name.setText(null);
                price.setText(null);
            }
        });

        searchButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                SQLiteDatabase db=dbHelper.getWritableDatabase();
                String name1=name.getText().toString();
                show.setText(null);
                if(name1.equals("")){//若不输入,查看整个菜单
                    show();

                    db.close();
                }else {
                    show.setText("品名");
                    showPrice.setText("价格");
                    Cursor cursor = db.rawQuery("select * from Naicha where name = ? ", new String[]{name1});//查询结果记录为行的集合
                    while (cursor.moveToNext()) {//遍历所有行
                        String newName = cursor.getString(cursor.getColumnIndex("name"));//分别提取name和price列
                        String newprice = cursor.getString(cursor.getColumnIndex("price"));

                        show.setText(show.getText() + "\n" + newName);//打印
                        showPrice.setText(showPrice.getText()+"\n" + newprice);
                    }

                    cursor.close();
                    db.close();
                    name.setText(null);
                    price.setText(null);
                }
            }
        });

        deleteButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                SQLiteDatabase db=dbHelper.getWritableDatabase();
                db.delete("Naicha","name=?",new String[]{name.getText().toString()});//调用删除函数


                show();
                db.close();
                Log.d("new","DeleteSuccess");
                name.setText(null);
                price.setText(null);
            }
        });

    }

    public void show(){//输出函数
        SQLiteDatabase db=dbHelper.getReadableDatabase();

        show.setText("品名");
        showPrice.setText("价格");
        Cursor cursor=db.rawQuery("select * from Naicha",null);//查询结果封装为行的集合
        while (cursor.moveToNext()){//遍历所有行
            String newName=cursor.getString(cursor.getColumnIndex("name"));//提取name和price列
            String newPirce=cursor.getString(cursor.getColumnIndex("price"));
            show.setText(show.getText()+"\n"+newName);//打印
            showPrice.setText(showPrice.getText()+"\n"+newPirce);
        }
        cursor.close();
    }
}
MyDatabaseHelper
package com.example.myapplicationsqlite;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import android.widget.Toast;

public class MyDatabaseHelper extends SQLiteOpenHelper {


    public static final String CREATE_NAICHA = "Create table Naicha("//创建数据库语句
            + "id integer primary key autoincrement,"
            + "name text,"
            + "price real)";

    private Context mContext;
    public MyDatabaseHelper(Context context,String name,SQLiteDatabase.CursorFactory factory,
                            int version){
        super(context,name,factory,version);
        mContext=context;

    }
    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_NAICHA);//执行创建语句
        Toast.makeText(mContext,"Create Succeeded",Toast.LENGTH_SHORT).show();//创建成功提示


    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.d("new","数据库更新成功!");//更新日志

    }
}
MainActivity

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <Button
        android:id="@+id/create_database"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Create database"
        app:layout_constraintVertical_bias="0"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:ignore="MissingConstraints">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="品名"
            android:textSize="30sp"
            />
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="请输入品名"
            android:textSize="20sp"
            android:id="@+id/name"
            />
    </LinearLayout>
    <LinearLayout

        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="价格"
            android:textSize="30sp"
            />
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="请输入价格"
            android:textSize="20sp"
            android:id="@+id/price"
            />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:textSize="20sp"
            android:text="插入"
            android:id="@+id/btn_insert"
            android:textAllCaps="false"
            />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:textSize="20sp"
            android:text="更新"
            android:id="@+id/btn_update"
            android:textAllCaps="false"
            />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:textSize="20sp"
            android:text="查询"
            android:id="@+id/btn_search"
            android:textAllCaps="false"
            />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:textSize="20sp"
            android:text="删除"
            android:id="@+id/btn_delete"
            android:textAllCaps="false"
            />


    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:id="@+id/tv_show"
            android:textSize="20sp"
            android:gravity="center_horizontal"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:id="@+id/tv_showPrice"
            android:textSize="20sp"
            android:gravity="center_horizontal"
            />
    </LinearLayout>

</LinearLayout>

  • 3
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值