移动应用开发 实验报告5 Sqlite数据库

一、实验要求 

        通过线性布局和相对布局来搭建Activity界面,在MainActivity中编写逻辑代码,运行程序,输入两条联系人信息分别点击“添加”“查询”“修改”“删除”按钮。实现联系人信息的添加、查询、修改以及删除功能。操作成功后,通过弹出的消息内容显示对应操作。下图界面仅供参考,可根据功能另外设计界面。

二、实验结果展示

1.没有数据,和添加数据

 2.查询,和修改

 

 3.修改后查询,和删除

 

 4.删除后查询

 

 三、代码展示

1.xml部分

<?xml version="1.0" encoding="utf-8"?>
<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:background="@drawable/bg"
    android:padding="16dp"
    android:orientation="vertical">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="222dp"
        android:background="@drawable/boiledmeat" />

    <LinearLayout
        android:layout_marginTop="30dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/white">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="姓 名:"
            android:textSize="28sp"/>

        <EditText
            android:id="@+id/et_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="请输入姓名"
            android:textSize="26sp"/>
    </LinearLayout>

    <LinearLayout
        android:layout_marginBottom="10dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/white">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="电 话:"
            android:textSize="28sp"/>

        <EditText
            android:id="@+id/et_phone"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="请输入手机号码"
            android:textSize="26sp"
            tools:ignore="VisualLintTextFieldSize" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <Button
            android:id="@+id/btn_add"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_marginRight="2dp"
            android:background="@color/white"
            android:text="添加"
            android:textSize="18sp"
            />
        <Button
            android:id="@+id/btn_query"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_marginRight="2dp"
            android:background="@color/white"
            android:text="查询"
            android:textSize="18sp"/>
        <Button
            android:id="@+id/btn_update"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_marginRight="2dp"
            android:background="@color/white"
            android:text="修改"
            android:textSize="18sp"/>
        <Button
            android:id="@+id/btn_delete"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_marginRight="2dp"
            android:background="@color/white"
            android:text="删除"
            android:textSize="18sp"/>
    </LinearLayout>

    <TextView
        android:id="@+id/tv_show"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="25sp"
        android:textSize="30sp"

        android:background="@color/white"/>
</LinearLayout>

2.java部分

package com.example.a5;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    MyHelper myHelper;
    private EditText mEtName, mEtPhone;
    private TextView mTvShow;
    private Button mBtnAdd,mBtnQuery,mBtnDelete,mBtnUpdata;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myHelper = new MyHelper(this);
        init();
    }

    public void init() {
        mEtName = findViewById(R.id.et_name);
        mEtPhone = findViewById(R.id.et_phone);
        mTvShow = findViewById(R.id.tv_show);
        mBtnAdd = findViewById(R.id.btn_add);
        mBtnDelete = findViewById(R.id.btn_delete);
        mBtnQuery = findViewById(R.id.btn_query);
        mBtnUpdata = findViewById(R.id.btn_update);
        mBtnAdd.setOnClickListener(this);
        mBtnUpdata.setOnClickListener(this);
        mBtnQuery.setOnClickListener(this);
        mBtnDelete.setOnClickListener(this);
    }
    @Override
    public void onClick(View view) {
        String name, phone;
        SQLiteDatabase db;
        ContentValues values;
        switch (view.getId()) {
            case R.id.btn_add:
                name = mEtName.getText().toString();
                phone = mEtPhone.getText().toString();
                db = myHelper.getWritableDatabase();
                //创建ContentValues对象
                values = new ContentValues();
                values.put("name", name);
                values.put("phone", phone);
                db.insert("information", null, values);
                Toast.makeText(this, "信息已添加", Toast.LENGTH_SHORT).show();
                db.close();
                break;
            case R.id.btn_query:
                db = myHelper.getReadableDatabase();
                //行数集合
                Cursor cursor = db.query("information", null, null, null, null, null, null);

                if (cursor.getCount() == 0) {
                    mTvShow.setText("");
                    Toast.makeText(this, "没有数据", Toast.LENGTH_SHORT).show();
                }
                else {
                    cursor.moveToFirst();
                    mTvShow.setText("Name : " + cursor.getString(0) + " ; Tel : " + cursor.getString(1));
                }

                while (cursor.moveToNext()) {
                    mTvShow.append("\n" + "Name : " + cursor.getString(0) + " ; Tel : " + cursor.getString(1));
                }
                cursor.close();
                db.close();
                break;
            case R.id.btn_update:
                db = myHelper.getWritableDatabase();
                values = new ContentValues();
                //只能修改电话,不能修改姓名
                values.put("phone", phone = mEtPhone.getText().toString());
                db.update("information", values, "name=?", new String[]{mEtName.getText().toString()});
                Toast.makeText(this, "信息已修改", Toast.LENGTH_SHORT).show();
                db.close();
                break;
            case R.id.btn_delete:
                db = myHelper.getWritableDatabase();
                //这种删除是  删除所有数据
                db.delete("information", null, null);
                mTvShow.setText("");
                Toast.makeText(this, "信息已删除", Toast.LENGTH_SHORT).show();
                db.close();
                break;
        }

    }

    class MyHelper extends SQLiteOpenHelper {

        public MyHelper( Context context) {
            super(context,"111.db",null,1);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            //创建information表
            db.execSQL("CREATE TABLE information(name VARCHAR(20) PRIMARY KEY,phone VARCHAR(20))");
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int i, int i1) {

        }
    }
}

四、实验总结

数据查询时要用到cursor,注意这个下标是从0开始

  • 6
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
江 西 理 工 大 学 江 西 理 工 大 学 Android平台开发基础 实验报告 实验名称 实验四AndroidSQLite数据库应用 日期 2016-05-14 专业班级 计算机133班 地点 信息学院M601 实验人 学号 12 同组人 单独完成 实验目的 (1) 熟悉Android的文件操作 (2) 掌握Android SQLite数据库 (3) 熟悉XML 和JSon 文件读取 实验要求 熟练使用Android的项目创建; 掌握Android的SQLite数据库设计; 掌握Android的Activity 和Fragement用法; 熟悉XML 和JSon 文件读取 实验内容 要求使用SQLite数据库实现用户注册和登录,读取数据库信息,退出时生成XML文件或JSON文件。 四、实验过程和结果 content_main.xml: Android-实验报告-Sqlite-数据库操作全文共4页,当前为第1页。<TabHost android:id="@android:id/tabhost" tools:context="com.example.ydc.filemanager.MainActivity"> <LinearLayout android:background="@drawable/login_bg" android:orientation="vertical"> <TabWidget android:id="@android:id/tabs"/> <FrameLayout android:id="@android:id/tabcontent"> </FrameLayout> </LinearLayout> </TabHost> Android-实验报告-Sqlite-数据库操作全文共4页,当前为第1页。 MainActivity.java: public class MainActivity extends TabActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.content_main); /*start code*/ TabHost tabHost = getTabHost(); /*添加第一个选项卡*/ TabHost.TabSpec tab1 = tabHost.newTabSpec("tab1"); tab1.setIndicator("login"); tab1.setContent(new Intent(this,LoginActivity.class)); tabHost.addTab(tab1); /*添加第二个选项卡*/ ··· } RegisterActivity.java: public class RegisterActivity extends Activity { SQLiteDatabase db; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.content_register); /*start*/ db = SQLiteDatabase.openOrCreateDatabase(this.getFilesDir().toString()+"/user.db3", null); /**/ Button register = (Button)findViewById(R.id.register); register.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String username = ((EditText)findViewById(R.id.username)).getText().toString(); String password = ((EditText)findViewById(R.id.password)).getText().toString(); try{ String sql = "create table if not exists user_info (_id integer " + " primary key autoincrement," + " username varchar(255)," + "

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值