My SqLite Helper Class onCreateMethod Doesn`t call at all

i Can't Create My DataBase With SqliteOpenHelper.My OnCreate Method Don't Run at all

i have already tried getReadable and getWriteable DataBase Methods but nothing Changes

public class SQLiteHandler extends SQLiteOpenHelper {

    private static SQLiteHandler sInstance;
    private static final String DATABASE_NAME = "telestaDB";
    private static final int DATABASE_VERSION = 1;
    private static final String TAG = "SqliteHelper";

    public SQLiteHandler(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        getReadableDatabase();
        getWritableDatabase();
        Log.i(TAG, "Constractor create!!");
    }

    public static SQLiteHandler getInstance(Context context) {

        if (sInstance == null) {
            Log.i(TAG, "getInstance: new instance created!!");
            sInstance = new SQLiteHandler(context.getApplicationContext());
        }
        return sInstance;
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        sqLiteDatabase.execSQL(ExceptionsModel.CREATE_TABLE);
        sqLiteDatabase.execSQL(DownloadModel.CREATE_TABLE);
    }

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

    }
    /*
    *
    *     @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(ExceptionsModel.CREATE_TABLE);
        Log.d(TAG, "onCreate: databaseCreated!!");
        db.execSQL(DownloadModel.CREATE_TABLE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL(ExceptionsModel.CREATE_TABLE);
        Log.d(TAG, "onUpgrade: databaseCreated!!");
        db.execSQL(DownloadModel.CREATE_TABLE);
    }

}

nothing happens when i tried to create an object of helper class and try to insert data into that. i use these lines to create db

SQLiteHandler sqliteHelper = SQLiteHandler.getInstance(context);
                    sqliteHelper.getWritableDatabase();

i already seen some other questions about this problem but no one helped!

Upvotes: 0

Views: 117

Answers (4)

Abhinav Gupta
Abhinav Gupta

Reputation: 16

in oncreate

method you did not create query

change->

https://www.javatpoint.com/android-sqlite-tutorial

Upvotes: 0

Andrew
Andrew

Reputation: 10182

I also note your Singleton Pattern is not quite right but this should not cause a problem unless you are using it wrong (Your constructor should be private) and getReadableDatabase(); and getWritableDatabase(); do nothing;

An example of a correct Singleton Pattern for SQLite is at Using Singleton design pattern for SQLiteDatabase

Upvotes: 1

ok so i found my code issue its from singleton pattern i don't know why but its prevent running my code truly just delete it and its work fine i send my codes here may help some one else

my SQLiteHelper Class Here

public class SQLiteHandler extends SQLiteOpenHelper {

    private static final String DATABASE_NAME = "telestaDB";
    private static final int DATABASE_VERSION = 1;
    private static final String TAG = "SqliteHelper";

    private SQLiteHandler(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        getWritableDatabase();
        getReadableDatabase();
        Log.i(TAG, "Constractor create!!");
    }

    public void addException(ExceptionsModel model) {
        SQLiteDatabase db = getWritableDatabase();
        db.beginTransaction();
        try {
            ContentValues values = new ContentValues();
//            values.put(ExceptionsModel.COLUMN_ID, model.getId());
            values.put(ExceptionsModel.USER_NAME, model.getUserName());
            db.insert(ExceptionsModel.TABLE_NAME, null, values);
            db.setTransactionSuccessful();
            db.endTransaction();
        } catch (Exception e) {
            Log.d(TAG, "Error while trying to add user to database" + e.toString());
        }
    }

    public void deleteException(String model) {
        SQLiteDatabase db = getWritableDatabase();
        db.beginTransaction();
        try {
            db.delete(ExceptionsModel.TABLE_NAME, ExceptionsModel.USER_NAME + "=?", new String[]{model});
        } catch (Exception ignored) {
            Log.d(TAG, "Error while trying to delete user from database");
        } finally {
            db.endTransaction();
        }
    }

    public List<ExceptionsModel> getAllExceptions() {

        List<ExceptionsModel> exceptions = new ArrayList<>();
        SQLiteDatabase db = getReadableDatabase();
        Cursor cursor = db.rawQuery(ExceptionsModel.SELECT, null);
        try {
            while (cursor.moveToNext()) {
                ExceptionsModel model = new ExceptionsModel();
                /* model.setId(cursor.getInt(cursor.getColumnIndex(ExceptionsModel.COLUMN_ID)));*/
                model.setUserName(cursor.getString(cursor.getColumnIndex(ExceptionsModel.USER_NAME)));
                exceptions.add(model);
            }
        } catch (Exception e) {
            Log.d(TAG, "Error while trying to get exceptions from database" + e.toString());
        } finally {
            if (cursor != null && !cursor.isClosed()) {
                cursor.close();
            }
        }
        return exceptions;
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        Log.i(TAG, "getInstance: new instance created!!");
        sqLiteDatabase.execSQL(ExceptionsModel.CREATE_TABLE);
    }

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

    }
}

My Model Class Here

public class ExceptionsModel {

    public static final String TABLE_NAME = "user_exceptions";
    public static final String COLUMN_ID = "id";
    public static final String USER_NAME = "username";
    public static final String CREATE_TABLE = "CREATE TABLE IF NOT EXISTS " + TABLE_NAME + "("
            + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT UNIQUE,"
            + USER_NAME + " TEXT"
            + ")";
    public static final String SELECT = String.format("SELECT * FROM %s", TABLE_NAME);

    private int id;
    private String userName;

    public ExceptionsModel() {
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public ExceptionsModel(int id, String userName) {
        this.id = id;
        this.userName = userName;
    }
}

Upvotes: 0

WiLc0
WiLc0

Reputation: 11

this is the code i have done which works fine for me adjust it as per your need.hope it works for u too!

public class DatabaseHelper extends SQLiteOpenHelper{
public static final String DATABASE_NAME ="register.db";
public static final String TABLE_NAME ="registeruser";
public static final String COL_1 ="ID";
public static final String COL_2 ="username";
public static final String COL_3 ="password";


public static final String TABLE2_NAME="Student";
public static final String COL_ONE="ID";
public static final String COL_TWO="RegNum";


public DatabaseHelper(Context context) {
    super(context, DATABASE_NAME, null, 1);
    //SQLiteDatabase db=this.getWritableDatabase();
}

@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
    sqLiteDatabase.execSQL("CREATE TABLE registeruser (ID INTEGER PRIMARY  KEY AUTOINCREMENT, username TEXT, password TEXT)");
    sqLiteDatabase.execSQL("CREATE TABLE Student(ID INTEGER PRIMARY KEY AUTOINCREMENT, RegNum TEXT)");


}

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

    sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + TABLE2_NAME);
    onCreate(sqLiteDatabase);
}

public long addUser(String user, String password){
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put("username",user);
    contentValues.put("password",password);
    long res = db.insert("registeruser",null,contentValues);
    db.close();
    return  res;
}

public boolean checkUser(String username, String password){
    String[] columns = { COL_1 };
    SQLiteDatabase db = getReadableDatabase();
    String selection = COL_2 + "=?" + " and " + COL_3 + "=?";
    String[] selectionArgs = { username, password };
    Cursor cursor = db.query(TABLE_NAME,columns,selection,selectionArgs,null,null,null);
    int count = cursor.getCount();
    cursor.close();
    db.close();

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



public boolean markAttendance(String regnum ){
    SQLiteDatabase db=this.getWritableDatabase();
    ContentValues contentValues=new ContentValues();
    contentValues.put(COL_TWO,regnum);
    long result=db.insert(TABLE2_NAME ,null , contentValues);

    if (result==-1)

        return false;

    else

        return true;

}
public Cursor getAllAttendance(){

    SQLiteDatabase db=this.getWritableDatabase();

    Cursor res= db.rawQuery("select * From " + TABLE2_NAME,null );
    return res;

}

}

Upvotes: 1

Related Questions