Мои проблемы: что я должен делать?
1. Ниже моя ошибка в logcat
2. Почему мои данные не попадают в базу данных 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));
}
}