222wan

导航

个人作业------打卡app下

zhuce界面用于用户注册

 

`package com.example.conect_sqlite;

import androidx.appcompat.app.AppCompatActivity;

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

import com.example.conect_sqlite.bean.student;
import com.example.conect_sqlite.bean.uid;

public class zhuce extends AppCompatActivity {
private EditText etname,etuid ,etpassword;
private MySQLiteOpenHelper mMySQLiteOpenHelper;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_zhuce);
    etname=findViewById(R.id.et_name);
    etuid=findViewById(R.id.et_uid);
    etpassword=findViewById(R.id.et_password);
    mMySQLiteOpenHelper =new MySQLiteOpenHelper(this);
}

public void insertDate2(View view) {
    String name= etname.getText().toString().trim();
    String uid= etuid.getText().toString().trim();
    String password= etpassword.getText().toString().trim();
   student student1=new student();
    student1.setName(name);
    student1.setUid(uid);
    student1.setPassword(password);
    long rowId=mMySQLiteOpenHelper.insertData2(student1);
    if(rowId!=-1){
        Toast.makeText(this,"注册成功!",Toast.LENGTH_SHORT).show();
      Intent  intent=new Intent(zhuce.this,MainActivity.class);
        startActivity(intent);

    }else {
        Toast.makeText(this,"注册失败!请重新注册!",Toast.LENGTH_SHORT).show();

    }

}

}MySQLiteOpenHelper界面数据库的连接,以及对数据库中的数据操作package com.example.conect_sqlite;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.example.conect_sqlite.bean.student;
import com.example.conect_sqlite.bean.uid;

import java.util.ArrayList;
import java.util.List;

public class MySQLiteOpenHelper extends SQLiteOpenHelper {//数据库名字
private static final String db_name="mySQLite.db";
private static final String table_name_student="student";
private static final String create_table_sql1="create table " +table_name_student+ "(id integer primary key autoincrement ,uid text,name text, password text,thing text, date text, daytime text)";

// private static final String table_name_uid="student_uid";
// private static final String create_table_sql2="create table " +table_name_uid+ "(id integer primary key autoincrement ,uid text,password text,name text)";
public MySQLiteOpenHelper(Context context) {
super(context,"db_name",null,2);//这里必须大于0否则就报错

}


@Override
public void onCreate(SQLiteDatabase db) {
    //创建数据库,创建时调用只调用一次
    db.execSQL(create_table_sql1);
   // db.execSQL(create_table_sql2);
}

@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
    //数据库更新的时候调用

}


public long insertData1(student student1 ){//打卡事件
    SQLiteDatabase db=getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put("password",student1.getPassword());
    values.put("name",student1.getName());
    values.put("thing",student1.getThing());
    values.put("date",student1.getDate());
    values.put("daytime",student1.getDaytime());

    return   db.insert(table_name_student,null,values);

}
public int deletefromdb(String name,String uid){
    SQLiteDatabase db=getWritableDatabase();

    return   db.delete(table_name_student,"name like ? or uid like ? ",new String[] {name,uid});
    //       表名                            名字或者uid像谁               传进去两个值放在数组里面

}

public long insertData2(student student1) {//注册信息
    SQLiteDatabase db=getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put("name",student1.getName());
    values.put("uid",student1.getUid());
    values.put("password",student1.getPassword());


    return   db.insert(table_name_student,null,values);

}

@SuppressLint("Range")
public int searchuid(String uid, String pass) {
    SQLiteDatabase db=getWritableDatabase();
    int row=0;
    Cursor cursor= db.query(table_name_student,new String[]{"uid","password"},"uid=?",new String[]{uid},null,null,"password");
    //       表名
    //       名字或者uid像谁               传进去两个值放在数组里面

    while(cursor.moveToNext()){
        String  password=cursor.getString(cursor.getColumnIndex("password"));
        cursor.close();
        if(pass.equals(password)){
            row=1;
        }else {
            row=0;
        }
  /* Cursor cursor=db.query(table_name_student,new String[]{uid},"uid=?",null,null,null,null);
    while(cursor.moveToNext()){
       String  pass=cursor.getString(cursor.getColumnIndex("password"));
        cursor.close();
        if(password.equals("pass")){
            row=1;
        }else {
            row=0;
        }*/

    }
    return row;


}

public List<student> searchfromdb(String name) {

    SQLiteDatabase db=getWritableDatabase();
    List<student> studentList =new ArrayList<>();
    int row=0;
    Cursor cursor= db.query(table_name_student,new String[]{"uid","name","thing","date","daytime"},"name like ?",new String[]{name},null,null,null);
    if(cursor != null) {
        while (cursor.moveToNext()){
            String uid= cursor.getString(cursor.getColumnIndex("uid"));
           String name1= cursor.getString(cursor.getColumnIndex("name"));
            String thing= cursor.getString(cursor.getColumnIndex("thing"));
            String date= cursor.getString(cursor.getColumnIndex("date"));
            String daytime= cursor.getString(cursor.getColumnIndex("daytime"));

            student student1=new student();
            student1.setUid(uid);
            student1.setName(name1);
            student1.setThing(thing);
            student1.setDate(date);
            student1.setDaytime(daytime);

            studentList.add(student1);

        }
        cursor.close();
    }
    return studentList;
}

public long updata(student student1) {
    SQLiteDatabase db=getWritableDatabase();
    ContentValues values = new ContentValues();
   // values.put("password",student1.getPassword());
    values.put("name",student1.getName());
    values.put("thing",student1.getThing());
    values.put("date",student1.getDate());
    values.put("daytime",student1.getDaytime());
   return  db.update(table_name_student,values,"password=?",new String[]{student1.getPassword()});

}

}
bean包package com.example.conect_sqlite.bean;

public class student {
public String uid;
public String password;

public String getUid() {
    return uid;
}

public void setUid(String uid) {
    this.uid = uid;
}

public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}

public String name;//名字
public String thing;//事件
public String date;//提醒日期
public String daytime;//当前时间

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getThing() {
    return thing;
}

public void setThing(String thing) {
    this.thing = thing;
}

public String getDate() {
    return date;
}

public void setDate(String date) {
    this.date = date;
}

public String getDaytime() {
    return daytime;
}

public void setDaytime(String daytime) {
    this.daytime = daytime;
}

@Override
public String toString() {
    return "student{" +
            "name='" + name + '\'' +
            ", thing='" + thing + '\'' +
            ", date='" + date + '\'' +
            ", daytime='" + daytime + '\'' +
            '}';
}

}
`
之实现了最基础的功能小细节还没有完善,比较难受的是sqlite 数据库它不是个动态的也就是说只能建一次表导致我这里后来仔细想想建两个表会更方便但是没办法试了很多种办法都没有成功,另外要注意的是:在我们完成项目大部分代码(这里默认数据库类已经写好)时运行在模拟器上但是这是遇到某一个功能时直接闪退不用想80%是数据库没连接成功,一般都有这几个情况

sql语句的问题
数据库版本的问题
其他问题
sql语句的问提一般在空格和双引号或者里面的特有名词拼写上大家注意就行
数据库版本是啥:

就是上面里面的version后面的数字代表版本,从0开始大家可以往后试到4如果都不行那就是别的问题了,
其他的问题大家要根据自己的代码进行推敲以及不断地去测试,我们要对自己的代码大体有个了解某部分大概是怎样的,实现怎样的功能,因为我们敲得代码行数也不算少每个都记住不太现实所以注释就相当重要了,大家最好是在函数后面加上作用表述比较可能出错的地方做标记,这样对我们后期排除错误有很大的帮助。

posted on 2023-03-19 22:26  角落的蘑菇  阅读(12)  评论(0编辑  收藏  举报