Reputation: 81
So I was writing code for login and register in android studio let me paste it here:
package com.example.myapplication.database;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.util.Log;
import android.widget.*;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class database extends SQLiteOpenHelper {
private final static String dbName = "user.db";
private final static String tableName = "customer";
private int Id;
private static final String ID_COL = "ID";
private static final String NAME_COL = "NAME";
private static final String EMAIL_COL = "EMAIL";
private static final String PASS_COL = "PASS";
private static final String MOBILE_COL = "MOBILE";
private final static int dbVersion = 3;
Context context;
public database(Context context){
super(context,dbName,null,dbVersion);
this.context = context;
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase = this.getWritableDatabase();
String query = "CREATE TABLE IF NOT EXISTS " + tableName + "(" + ID_COL + " VARCHAR(50) PRIMARY KEY," + NAME_COL + " VARCHAR(50)," + EMAIL_COL + " VARCHAR(50)," + PASS_COL + " VARCHAR(50), "+MOBILE_COL+" VARCHAR(50));";
sqLiteDatabase.execSQL(query);
}
public boolean checkAlreadyExists(){
SQLiteDatabase db = this.getReadableDatabase();
String check = "SELECT * FROM "+tableName+" WHERE ID ="+Id+";";
Cursor cursor = db.rawQuery(check,null);
if(cursor.getCount() > 0){
cursor.close();
return true;
}
else
{
cursor.close();
return false;
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
db.execSQL("DROP TABLE IF EXISTS "+tableName);
onCreate(db);
}
public void addRecord(String id,String name,String email,String pass){
SQLiteDatabase sql = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(ID_COL,id);
values.put(NAME_COL,name);
values.put(EMAIL_COL,email);
values.put(PASS_COL,pass);
if(!checkAlreadyExists()) {
long res = sql.insert(tableName,null,values);
if(res==-1) {
Toast.makeText(context, "Registration Failed", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(context,"You Have Been Registered..//",Toast.LENGTH_LONG).show();
}
}
else
{
Toast.makeText(context,"User Already Exists.Please Login.",Toast.LENGTH_LONG).show();
}
}
public boolean login(String loginId,String loginPass){
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(tableName,null,ID_COL+" = ? AND "+PASS_COL+" =? ",new String[]{loginId,loginPass},null,null,null);
try
{
if(cursor.moveToFirst())
return true;
}catch(Exception e){
String message = e.getMessage();
Log.v("Database.java","login:"+e.getMessage());
}
finally {
cursor.close();
}
return false;
}
}
This is the buttonPressed code:
package com.example.myapplication;
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.myapplication.database.database;
public class RegisterActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
initializeUI();
}
private String id;
private int Id;
private String name;
private String pass;
private String email;
private String confirm;
private String mobile;
private EditText NAME;
private EditText PASSWORD;
private EditText CONFIRMPASSWORD;
private EditText EMAIL;
private EditText REGISTER;
private EditText ID;
//Below method initializes the UI.
public void initializeUI(){
ID = (EditText)findViewById(R.id.registerID);
NAME = (EditText) findViewById(R.id.registerName);
PASSWORD = (EditText) findViewById(R.id.registerPassword);
EMAIL = (EditText) findViewById(R.id.registerEmail);
CONFIRMPASSWORD = (EditText) findViewById(R.id.registerConfirmPassword);
}
//Register process gets called.
public void buttonPressed(View view){
database db = new database(RegisterActivity.this);
id = ID.getText().toString();
name = NAME.getText().toString();
email = EMAIL.getText().toString();
pass = PASSWORD.getText().toString();
confirm = CONFIRMPASSWORD.getText().toString();
//Seeing if the password matches.
if (pass.equals(confirm)) {
db.addRecord(id, name, email, pass);
ID.setText("");
NAME.setText("");
EMAIL.setText("");
PASSWORD.setText("");
CONFIRMPASSWORD.setText("");
Intent in = new Intent(RegisterActivity.this,LoginActivity.class);
startActivity(in);
} else {
Toast.makeText(RegisterActivity.this, "Password are Incorrect..//", Toast.LENGTH_LONG).show();
}
}
}
And I am getting the following exception:
E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.myapplication, PID: 3980 java.lang.IllegalStateException: Could not execute method for android:onClick at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:446) at android.view.View.performClick(View.java:5610) at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1131) at android.view.View$PerformClick.run(View.java:22265) at android.os.Handler.handleCallback(Handler.java:751) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6077) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756) Caused by: java.lang.reflect.InvocationTargetException at java.lang.reflect.Method.invoke(Native Method) at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:441) at android.view.View.performClick(View.java:5610) at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1131) at android.view.View$PerformClick.run(View.java:22265) at android.os.Handler.handleCallback(Handler.java:751) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6077) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756) Caused by: java.lang.IllegalStateException: getDatabase called recursively at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:203) at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163) at com.example.myapplication.database.database.onCreate(database.java:28) at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:251) at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163) at com.example.myapplication.database.database.addRecord(database.java:52) at com.example.myapplication.RegisterActivity.buttonPressed(RegisterActivity.java:51) at java.lang.reflect.Method.invoke(Native Method) at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:441) at android.view.View.performClick(View.java:5610) at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1131) at android.view.View$PerformClick.run(View.java:22265) at android.os.Handler.handleCallback(Handler.java:751) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6077) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
Can Somebody Please Help me for this?
Upvotes: 0
Views: 75
Reputation: 200080
You should always look for the Caused by
section of the exception:
Caused by: java.lang.IllegalStateException: getDatabase called recursively
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:203)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
at com.example.myapplication.database.database.onCreate(database.java:28) at
You're calling getWritableDatabase()
in your SQLiteOpenHelper
's onCreate()
method. Don't do that - use the sqLiteDatabase
that is passed to your onCreate()
method instead:
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
String query = "CREATE TABLE IF NOT EXISTS " + tableName + "(" + ID_COL + " VARCHAR(50) PRIMARY KEY," + NAME_COL + " VARCHAR(50)," + EMAIL_COL + " VARCHAR(50)," + PASS_COL + " VARCHAR(50), "+MOBILE_COL+" VARCHAR(50));";
sqLiteDatabase.execSQL(query);
}
Upvotes: 0