数据存储-SQLite

一、实验目的

1.理解安卓数据存储的方式 

2.熟悉安卓SQLlite数据库存储及访问机制

二、实验过程

1.创建名为Directory的应用程序。

2.导入background.jpgdrawable-hdpi文件夹中。

3.编写界面
 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    
android:layout_width="match_parent"
    
android:layout_height="match_parent"
    
android:orientation="vertical"
    
android:padding="16dp"
    
android:background="@drawable/background">
    <
LinearLayout
        
android:layout_width="match_parent"
        
android:layout_height="wrap_content"
        
android:layout_marginTop="170dp">
        <
TextView
            
android:layout_width="wrap_content"
            
android:layout_height="wrap_content"
            
android:text="姓  名 :"
            
android:textSize="18sp" />

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

        <
TextView
            
android:layout_width="wrap_content"
            
android:layout_height="wrap_content"
            
android:text="电  话 :"
            
android:textSize="18sp" />

        <
EditText
            
android:id="@+id/et_phone"
            
android:layout_width="match_parent"
            
android:layout_height="wrap_content"
            
android:hint="请输入手机号码"
            
android:textSize="16sp" />
    </
LinearLayout>
    <
LinearLayout
        
android:layout_width="match_parent"
        
android:layout_height="wrap_content"
        
android:layout_centerVertical="true">

        <
Button
            
android:id="@+id/btn_add"
            
android:layout_width="0dp"
            
android:layout_height="wrap_content"
            
android:layout_marginRight="2dp"
            
android:layout_weight="1"
            
android:background="#B9B9FF"
            
android:text="添加"
            
android:textSize="18sp" />

        <
Button
            
android:id="@+id/btn_query"
            
android:layout_width="0dp"
            
android:layout_height="wrap_content"
            
android:layout_marginRight="2dp"
            
android:layout_weight="1"
            
android:background="#DCB5FF"
            
android:text="查询"
            
android:textSize="18sp" />

        <
Button
            
android:id="@+id/btn_update"
            
android:layout_width="0dp"
            
android:layout_height="wrap_content"
            
android:layout_marginRight="2dp"
            
android:layout_weight="1"
            
android:background="#E6CAFF"
            
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:background="#ACD6FF"
            
android:text="删除"
            
android:textSize="18sp" />
    </
LinearLayout>

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

</
LinearLayout>

4.编写交互代码

1)在MainActivity中编写逻辑代码,实现联系人信息的添加、查询、修改以及删除功能。对四个按钮设置监听,重写onClick()方法。

34~46行,创建initial()方法,初始化界面控件并设置添加、查询、修改、删除按钮的点击监听事件。

2)创建MyHelper类继承SQLiteOpenHelper类,在该类中重写onCreat()方法和onUpgrade()方法。通过execSQL()方法创建表information

3)重写onClick()方法;

首先获取SQLiteDatabase类的对象db,创建一个ContentValues类的对象values

添加数据,通过SQLiteDatabase类的insert()方法,将姓名、电话信息添加到数据库。

查询数据,通过SQLiteDatabase类的query()方法,将姓名、电话信息查询出来并显示在界面上。其中使用Cursor接口,使用了遍历查询结果的方法。

修改数据,通过SQLiteDatabase类的update()方法,修改姓名、电话信息。

删除数据,通过SQLiteDatabase类的delete()方法,删除姓名、电话信息。

5.实验结果

输入账号,点击添加按钮

点击查询按钮,在下方出现信息

输入新的密码,点击修改按钮

再次点击查询按钮,显示修改后的数据

点击删除按钮

点击查询按钮,弹出“没有数据”的提示

三、实验总结

        本次实验学会了创建数据库,熟悉安卓SQLite数据库存储及访问机制。学会了对数据库的基本操作,并对其中数据信息进行增删改查。对数据库事务进行了简单了解。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值