首页 > 解决方案 > 为什么我在 SQLite 中登录页面时页面会自动关闭?

问题描述

我的问题:我想做什么?

1.下面是我在logcat中的错误

日志猫

完整的堆栈跟踪日志猫

2. 为什么我在SQLite浏览器中打开我的SQLite数据库没有进入我的数据?

SQLite 数据库不存在

3. 几乎所有我研究和使用的方法,但都不能使用。

我的连接 SQLite 数据库:Database.java

package com.example.projectvote;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import static androidx.constraintlayout.widget.Constraints.TAG;

public class Database extends SQLiteOpenHelper {
private static final String TAG = "Database";
public static final String DbName = "SignUp.db";
public static final String TbName = "SignUp";
public static final String Col1 = "Num";
public static final String Col2 = "ID";
public static final String Col3 = "Password";

public static final String TbName1 = "candidate";
public static final String Col4 = "Nom";
public static final String Col5 = "Candidates";

public Database(Context context) {
    super(context, DbName, null, 1);
}

@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
    sqLiteDatabase.execSQL("CREATE TABLE SignUp (Num INT PRIMARY KEY AUTOINCREMENT, ID TEXT, Password TEXT)");
    sqLiteDatabase.execSQL("CREATE TABLE candidate (Nom INT PRIMARY KEY AUTOINCREMENT, Candidates TEXT)");
}

@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
    sqLiteDatabase.execSQL(" DROP TABLE IF EXISTS " + TbName);
    sqLiteDatabase.execSQL(" DROP TABLE IF EXISTS " + TbName1);
    onCreate(sqLiteDatabase);
}

public long addUser(String ID, String password)
{
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues cv = new ContentValues();
    cv.put("ID", ID);
    cv.put("password", password);
    long res = db.insert("SignUp", null, cv);
    db.close();
    return res;
}

public boolean checkUser(String ID, String password)
{
    String[] columns = { Col2 };
    SQLiteDatabase db = getReadableDatabase();
    String selection = Col2 + "=?" + "and" + Col3 + "=?";
    String[] selectionArgs = { ID, password };
    Cursor c = db.query(TbName, columns, selection, selectionArgs, null, null, null);
    int count = c.getCount();
    c.close();
    db.close();

    if (count > 0)
        return true;
    else
        return false;
}

public Cursor readData(SQLiteDatabase db) {
    String[] cols = { Col5 };
    Cursor c = db.query(TbName1, cols, null, null, null, null, null);
    return c;
}

public boolean addCandidate(String Candidates)
{
    SQLiteDatabase db = this. getWritableDatabase();
    ContentValues cv = new ContentValues();
    cv.put(Col5, Candidates);
    Log.d(TAG, "addCandidate : Adding " + Candidates + " to " + TbName1);
    long res = db.insert(TbName1, null, cv);
    if(res == -1)
    {
        return false;
    }
    else
    {
        return true;
    }
}

public Cursor getItemID(String Candidates)
{
    SQLiteDatabase db = this.getWritableDatabase();
    String query = "SELECT " + Col4 + " FROM " + TbName1 + " WHERE " + Col5 + " = " + Candidates + "'";
    Cursor data = db.rawQuery(query, null);
    return data;
}

public void updateCandidate(String newCandidate, int ID, String oldCandidate)
{
    SQLiteDatabase db = this.getWritableDatabase();
    String query = "UPDATE " + TbName1 + " SET " + Col5 + "= '" + newCandidate + "' WHERE " + Col5 + "= '" + oldCandidate + "'";
    Log.d(TAG, "updateCandidate: query: " + query);
    Log.d(TAG, "updateCandidate: Setting Candidate to " + newCandidate);
    db.execSQL(query);
}

public void deleteCandidate(int ID, String Candidate)
{
    SQLiteDatabase db = this.getWritableDatabase();
    String query = "DELETE FROM " + TbName1 + " WHERE " + Col5 + "= '" + Candidate + "'";
    Log.d(TAG, "deleteCandidate: query: " + query);
    Log.d(TAG, "deleteCandidate: Deleting: " + Candidate + " from database ");
    db.execSQL(query);
}

public Cursor getListContent1() {
    SQLiteDatabase db = this.getWritableDatabase();
    String query = "SELECT * FROM " + TbName1;
    Cursor data = db.rawQuery(query, null);
    return data;
}
}

我的登录java:Login.java

package com.example.projectvote;

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;

public class Login extends AppCompatActivity
{
EditText ID, password;
Database db;

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

    db = new Database(this);

    ID = findViewById(R.id.etID);
    password = findViewById(R.id.etPassword);
}

public void OnLog(View view)
{
    String Id = ID.getText().toString().trim();
    String Password = password.getText().toString().trim();
    Boolean res = db.checkUser(Id, Password);
    if (res == true)
    {
        startActivity(new Intent(getApplicationContext(), Home.class));
    }
    else if ((ID.equals("Admin") && Password.equals("Admin2019")))
    {
        startActivity(new Intent(getApplicationContext(), Admin.class));
    }
    else
    {
        Toast.makeText(Login.this, "Sorry, Login Error", Toast.LENGTH_SHORT).show();
    }
}

public void OnReg(View view) {
    startActivity(new Intent(getApplicationContext(), SignUp.class));
}
}

标签: javaandroiddatabasesqliteandroid-studio

解决方案


在您的 onCreate() 中更新您的查询,Database.java如下所示

sqLiteDatabase.execSQL("CREATE TABLE SignUp (Num INTEGER PRIMARY KEY AUTOINCREMENT, ID TEXT, Password TEXT)");
sqLiteDatabase.execSQL("CREATE TABLE candidate (Num INTEGER PRIMARY KEY AUTOINCREMENT, Candidates TEXT)");

推荐阅读