Android的数据库SQLite进行数据存储与SharePreferences存储

Android 的数据库SQLite进行数据存储与SharePreferences存储

SQLite进行数据存储

package com.example.administrator.myapplicationsqllite;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
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{
        private EditText et_account; //账号输入框
        private EditText et_password; //密码输入框
        private Button btn_login;  //登录按钮
        private Button btn_massage;//显示数据
        private TextView textView;
        private TextView textView2;
        private MyHelper myHelper;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            myHelper = new MyHelper(this);
            initView();
        }

        /**
         * 初始化按钮
         * 只有保存和显示按钮
         */
        private void initView() {
            et_account = (EditText) findViewById(R.id.et_account);
            et_password = (EditText) findViewById(R.id.et_password);
            btn_login =(Button) findViewById(R.id.btn_login);
            btn_massage = (Button) findViewById(R.id.btn_show_massage);
            textView = (TextView) findViewById(R.id.tv_massage);
            textView2 = (TextView) findViewById(R.id.tv_massage2);
            //设置按钮的带年纪监听事件
            btn_login.setOnClickListener(this);
            btn_massage.setOnClickListener(this);
        }

        /**
         * 点击监听方法
         * @param v 试图数据返回
         */
        @Override
        public void onClick(View v) {
            SQLiteDatabase db;
            ContentValues values;
            switch (v.getId()) {
                case R.id.btn_login: //当点击登录按钮时,获取界面上的输入的qq账号和密码
                    String account = et_account.getText().toString();
                    String password = et_password.getText().toString();
                    //校验输入的账号与密码是否为空
                    if (TextUtils.isEmpty(account)) {
                        Toast.makeText(this,"请输入QQ账号",Toast.LENGTH_SHORT).show();
                        return;
                    }
                    if (TextUtils.isEmpty(password)) {
                        Toast.makeText(this,"请输入密码账号",Toast.LENGTH_SHORT).show();
                        return;
                    }
                    Toast.makeText(this,"登录成功",Toast.LENGTH_SHORT).show();

                    db = myHelper.getWritableDatabase(); //获取可读写的SQLitDatebase对象
                    values = new ContentValues(); // 创建ContentValues对象
                    values.put("account", account);
                    values.put("password", password);
                    long id =  db.insert("information", null, values);
                    System.out.println("数据==>" + id);
                    Toast.makeText(this, "信息已添加", Toast.LENGTH_SHORT).show();
                    db.close(); //关闭数据库链接
                    break;
                case R.id.btn_show_massage: //查询数据
                    db = myHelper.getReadableDatabase();
                    Cursor cursor = db.query("information", null, null, null, null, null,null);
                    if (cursor.getCount() == 0) {
                        et_account.setText(" "); //提升信息没有获取到
                        Toast.makeText(this, "数据为空", Toast.LENGTH_SHORT).show();
                    } else {
                        cursor.moveToFirst();
                        String useraccount = cursor.getString(1);
                        String pwd = cursor.getString(2);
                        textView.setText(useraccount);
                        textView2.setText(pwd);
                    }
                    break;
            }
        }
        class MyHelper extends SQLiteOpenHelper {

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

            @Override
            public void onCreate(SQLiteDatabase db) {
               db.execSQL("create table information(id INTEGER  primary key autoincrement,account varchar(20),password varchar(20))");
            }

            @Override
            public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            }
        }
    }

布局文件

<?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:background="#E6E6E6"
    android:orientation="vertical"
    android:padding="10dp">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:background="@android:color/white"
        android:orientation="horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:text="账号:"
            android:textColor="#000"
            android:textSize="20sp" />
        <EditText
            android:id="@+id/et_account"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:background="@null"
            android:padding="10dp" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:background="@android:color/white"
        android:orientation="horizontal">
        <TextView
            android:id="@+id/tv_password"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:text="密码:"
            android:textColor="#000"
            android:textSize="20sp" />
        <EditText
            android:id="@+id/et_password"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:background="@null"
            android:inputType="textPassword"
            android:padding="10dp" />
    </LinearLayout>
    <Button
        android:id="@+id/btn_login"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="25dp"
        android:background="#3C8DC4"
        android:text="登录"
        android:textColor="@android:color/white"
        android:textSize="20sp" />
    <Button
        android:id="@+id/btn_show_massage"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="25dp"
        android:background="#3C8DC4"
        android:text="点击显示账户密码"
        android:textColor="@android:color/white"
        android:textSize="20sp" />
    <TextView
        android:id="@+id/tv_massage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        />
    <TextView
        android:id="@+id/tv_massage2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20dp"/>
</LinearLayout>


SharePreferences存储

布局文件

<?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:background="#E6E6E6"
    android:orientation="vertical"
    android:padding="10dp">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:background="@android:color/white"
        android:orientation="horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:text="账号:"
            android:textColor="#000"
            android:textSize="20sp" />
        <EditText
            android:id="@+id/et_account"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:background="@null"
            android:padding="10dp" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:background="@android:color/white"
        android:orientation="horizontal">
        <TextView
            android:id="@+id/tv_password"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:text="密码:"
            android:textColor="#000"
            android:textSize="20sp" />
        <EditText
            android:id="@+id/et_password"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:background="@null"
            android:inputType="textPassword"
            android:padding="10dp" />
    </LinearLayout>
    <Button
        android:id="@+id/btn_login"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="25dp"
        android:background="#3C8DC4"
        android:text="登录"
        android:textColor="@android:color/white"
        android:textSize="20sp" />
    <Button
        android:id="@+id/btn_show_massage"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="25dp"
        android:background="#3C8DC4"
        android:text="点击显示账户密码"
        android:textColor="@android:color/white"
        android:textSize="20sp" />
    <TextView
        android:id="@+id/tv_massage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
            android:textSize="20dp"
        />
    <TextView
        android:id="@+id/tv_massage2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20dp"/>
</LinearLayout>


SharePreferences函数

package com.example.administrator.myapplication;

import android.content.Context;
import android.content.SharedPreferences;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2022/3/29.
 * 进行SharePreferences的保存
 */

public class SharePreferencesSaves {
    //先进行保存qq账号和密码

    /**
     * 使用SharePreferences保存数据
     * @param context 上下问环境
     * @param account 账号
     * @param password 密码
     * @return 结果为保存成功的提示
     */
    public static boolean saveUserInfo(Context context, String account, String password) {
        SharedPreferences sharedPreferences = context.getSharedPreferences("data", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit(); //获取编辑方法
        editor.putString("account", account);
        editor.putString("password",password);
        editor.commit(); //提交
        return true;
    }

    /**
     * 从data中获取存储的数据
     * @param context 上下文环境对象
     * @return 从SharePreferences中获取数据其中名称为:data.xml
     */
    public static Map<String, String> getUserInfo(Context context) {
        SharedPreferences sharedPreferences = context.getSharedPreferences("data", Context.MODE_PRIVATE);
        String account = sharedPreferences.getString("account", null);
        String password = sharedPreferences.getString("password", null);
        Map<String, String> userMap = new HashMap<>(); //将数据进行保存返回到主函数中
        userMap.put("account",account);
        userMap.put("password",password);
        return userMap;
    }
}

主函数

package com.example.administrator.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Map;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    private EditText et_account; //账号输入框
    private EditText et_password; //密码输入框
    private Button btn_login;  //登录按钮
    private Button btn_massage;//显示数据
    private TextView textView;
    private TextView textView2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        //通过工具类FileSaveQQ中的getUserInfo()方法获取qq账号与密码信息
        Map<String, String> userInfo = SharePreferencesSaves.getUserInfo(this);
    }

    /**
     * 初始化按钮
     */
    private void initView() {
        et_account = (EditText) findViewById(R.id.et_account);
        et_password = (EditText) findViewById(R.id.et_password);
        btn_login =(Button) findViewById(R.id.btn_login);
        btn_massage = (Button) findViewById(R.id.btn_show_massage);
        textView = (TextView) findViewById(R.id.tv_massage);
        textView2 = (TextView) findViewById(R.id.tv_massage2);
        //设置按钮的带年纪监听事件
        btn_login.setOnClickListener(this);
        btn_massage.setOnClickListener(this);
    }

    /**
     * 点击监听方法
     * @param v 试图数据返回
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_login: //当点击登录按钮时,获取界面上的输入的qq账号和密码
                String account = et_account.getText().toString();
                String password = et_password.getText().toString();
                //校验输入的账号与密码是否为空
                if (TextUtils.isEmpty(account)) {
                    Toast.makeText(this,"请输入QQ账号",Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(password)) {
                    Toast.makeText(this,"请输入密码账号",Toast.LENGTH_SHORT).show();
                    return;
                }
                Toast.makeText(this,"登录成功",Toast.LENGTH_SHORT).show();
                //保持用户信息
                boolean isSaveSuccess = SharePreferencesSaves.saveUserInfo(this,account,password);
                if (isSaveSuccess) {
                    Toast.makeText(this,"保持成功",Toast.LENGTH_SHORT).show();

                } else {
                    Toast.makeText(this,"保持失败",Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.btn_show_massage:
                Map<String, String> userInfo = SharePreferencesSaves.getUserInfo(this);
                String useraccoun = userInfo.get("account");
                String passwor = userInfo.get("password");
                et_account.setText(userInfo.get("account")); //将获取的账号显示到界面上
                textView.setText(useraccoun);

                textView2.setText(passwor);
                et_password.setText(userInfo.get("password")); //将获取的密码显示到界面上
                break;
        }
    }
}

在这里插入图片描述

  • 9
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员小徐同学

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值