阶段二冲刺五

昨天的主要制作了一个完善的底部导航功能

今天打算学一下简单的sqlite

这是一个Android自带的一个小型数据库,可以进行本地存储

建立数据库代码如下

public class DBcreate extends SQLiteOpenHelper {
    public static SQLiteDatabase sqLiteDatabase;

    public DBcreate(@Nullable Context context) {
        super(context, "gameapp", null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        sqLiteDatabase.execSQL(create_user);
    }

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

    }
    private static final String create_user = "create table user (" +
            "id varchar(20) not null primary key," +
            "username varchar(30)," +
            "password varchar(20)," +
            "icon varchar(255)," +
            "age varchar(10)," +
            "sex varchar(10)," +
            "address varchar(50))";
}
package com.example.gameapp.ui.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.gameapp.R;
import com.example.gameapp.bean.User;
import com.example.gameapp.dao.Userdao;

public class LoginActivity extends AppCompatActivity {
    private Button btn_login;
    private TextView text_login;
    private EditText eidt_login_photo,edit_login_password;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        initView();
        ininEvent();
    }

    private void ininEvent() {
        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //验证登录
                String userphone = eidt_login_photo.getText().toString();
                String userpassword = edit_login_password.getText().toString();
                if(userphone.isEmpty() || userpassword.isEmpty()){
                    Toast_1("账号和密码不能为空");
                    return;
                }else {
                    Userdao userdao = new Userdao();
                    User user = userdao.select(userphone);
                    if(user != null){
                        if(user.getPassword().equals(userpassword)){
                            toOrderActivity();
                        }else {
                            Toast_1("密码错误");
                            return;
                        }
                    }else
                    {
                        Toast_1("账号不存在");
                        return;
                    }

                }
            }
        });

        text_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                toRegistActivity();
            }
        });
    }

    private void toOrderActivity() {
        Intent intent = new Intent(this,OrderActivity.class);
        startActivity(intent);
        finish();
    }

    private void toRegistActivity() {
        Intent intent = new Intent(LoginActivity.this,RegistActivity.class);
        startActivity(intent);

    }

    private void initView() {
        btn_login = findViewById(R.id.btn_login);
        text_login = findViewById(R.id.text_login);
        eidt_login_photo = findViewById(R.id.eidt_login_photo);
        edit_login_password= findViewById(R.id.eidt_login_password);

    }

    public void Toast_1(String content){
        Toast toast = Toast.makeText(this,content,Toast.LENGTH_SHORT);
        toast.show();
    }

}

在初次使用这个数据库的时候,遇到了很多的错误

这个数据库只能被初始化一次,因为第一次接触到这个数据库

对初始化语句并不理解,所以便遇到了一直闪退的情况

posted @ 2022-06-14 13:46  软工小蜗牛  阅读(60)  评论(0编辑  收藏  举报