How to delete Sqlite android Dd? - android

I am using method context.deleteDatabase(DATABASE_NAME);
Please tell me clearly when should this method be applied in code line !!
DatabaseHelper(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
context.deleteDatabase(DATABASE_NAME);
}

You have your Context and you know the name of the database, then you have to use:
context.deleteDatabase(DATABASE_NAME);
When this line gets run, the database should be deleted.

Related

upgrading database in android studio not working

I am trying to updgrade the sqlite version number from 1 to 2 but onupgrade method is not getting called.
do i have to delete application in the device and then install the application to test it ?
Only method which get called is DatabaseHelper and onCreate
No other method get called.
In DatabaseHelper.java file
private static final int DBVersion = 1; // I had change this value to 2.but it is not working.
public DatabaseHelper(Context context, CursorFactory cf) {
super(context, DBName, cf, DBVersion);
}
#Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(TABLE_CREATE_Table);
}
#Override
public void onOpen(SQLiteDatabase db) {
super.onOpen(db);
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int currentVersion) {
//This is not called.If it called i will add the changed here
}
another class for dataprovider.java
#Override
public boolean onCreate() {
dbHelper = new DatabaseHelper(getContext(), null);
return true;
}
using :-
#Override
public boolean onCreate() {
dbHelper = new DatabaseHelper(getContext(), null);
SqliteDatabase db = dbHelper.getWritableDatabase(); //<<<<<<<<<<<
return true;
}
attempts to open the database and thus onCreate/onUpgrade would be called.
- onCreate only if the database does not exist.
- onUpgrade only if the database exists AND the specified version number is greater than the database version stored in the database.
That is when instantiating the Database Helper (subclass of SQLiteOpenHelper) no attempt is made to open the database.
The attempt to open the database is only made when the SQLiteDatabase's getWritableDatabase (or getReadableDatabase) are called. Both attempt to open the database. Noting that getWritableDatabase or getReadableDatabase may well be called implicitly.
Note the above does not include directly using the SQliteDatabase's OPEN methods.
Alternative Fix
I personally tend to force the open when constructing the database helper by using :-
public DatabaseHelper(Context context, CursorFactory cf) {
super(context, DBName, cf, DBVersion);
this.getWritableDatabase();
}
I tend to save the returned SQLiteDatabase into a class variable and then use that rather than using this.getWritableDatabase() in the underlying methods.

Android different databases for different users

I want to see if there is a way to assign different database names to different users that login to an app. I'm trying to write a small app that assigns a DB to a user based on their username/email.
I tried setting DATABASE_NAME that is passed into
public SQLiteHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
to the users' email addresses but it doesn't work. It loads the same data for all users.
On logout, I close the database and clean up and on startup, everytime I start a new database connection with the name already properly assigned...
Can someone help me understand what I'm missing?
You can just pass the name of the database to your SQLiteOpenHelper class constructor (along with required Context instance) instead of hardcoding it as did DATABASE_NAME:
public class DBOpenHelper extends SQLiteOpenHelper {
public DBOpenHelper(Context context, String dbName, int dbVersion) {
super(context, dbName, null, dbVersion);
}
...
}

what is the use of SQLiteDatabase.CursorFactory in Android?

I have created a database by extending SQLiteOpenHelper class. And its created also. This is code I am pasting
public Imagehelper(Context context) {
super(context, DATABASE_NAME, null, SCHEMA_VERSION);
cntxt = context;
filename = Environment.getExternalStorageDirectory();
DATABASE_FILE_PATH_EXTERNAL = filename.getAbsolutePath()+File.separator+DATABASE_NAME;
Log.i("Log", ":"+DATABASE_FILE_PATH_EXTERNAL);
}
Here everything is working fine.
But if you focus on the parameters pass in super super(context, DATABASE_NAME, null, SCHEMA_VERSION); . I am not able to understand the null parameter. I know here we have to pass the SQLiteDatabase.CursorFactory object.
But how?? And what is the use of that??
The reason of passing null is you want the standard SQLiteCursor behaviour. If you want to implement a specialized Cursor you can get it by by extending the Cursor class( this is for doing additional operations on the query results). And in these cases, you can use the CursorFactory class to return an instance of your Cursor implementation. Here is the document for that
SQLiteDatabase.CursorFactory DOC
Used to allow returning sub-classes of Cursor when calling query.

SQLite DatabaseHelper class

when we create SQLite database from an android application can use a DatabaseHelper class which extend SQLiteOpenHelper.
My constructor is as follows
public DatabaseHelper(Context context)
{
super(context, dbName, null, DATABASE_VERSION);
}
Then the onCreate() method
public void onCreate(SQLiteDatabase db)
{
String qry = "CREATE TABLE DEPT(dept_id INTEGER PRIMARY KEY, deptName TEXT)";
db.execSQL(qry);
InsertDepts(db);
}
But when i create an instance of the DatabaseHelper class, my database havent created yet.
I believe when I create an Object from this class, the constructor executes and since no database created yet it will create database and execute onCreate() method. So definitely an database must be created.
Or untill we execute the following code doesn't it execute the onCreate() method, even if we create an object from the class.
databaseHelper.getWritableDatabase()
Can anyone please explain the execution order of the databasehelper class.
You create an instance of your DatabaseHelper-class, which causes the constructor to be executed. The constructor will execute the mother-classes constructor (SQLiteOpenHelper), which will then check if the Database-file already exists.
If it exists, nothing is done and your object is created.
If it doesn't exist, the Database-file is created and the onCreate-method is called.
You say, your Database doesn't exist. Are you getting any errors in the LogCat? Also, have you tried accessing the Database using the adb?
Order should be like this (as reads SQLiteOpenHelper javadoc)
Constructor
During 1st call of openReadableDatabase() or openWritableDatabase() will be called onCreate
Then will called onOpen
Added
Just checked sources of SQLiteOpenHelper constructor and it reads:
public SQLiteOpenHelper(Context context, String name, CursorFactory factory, int version) {
if (version < 1) throw new IllegalArgumentException("Version must be >= 1, was " + version);
mContext = context;
mName = name;
mFactory = factory;
mNewVersion = version;
}
In contrary openReadableDatabase/OpenWritableDatabase sources consists calls to openOrCreateDatabase() - so constructor doesn't call onCreate
please refer to this
http://androidforbeginners.blogspot.com/2010/01/creating-multiple-sqlite-database.html

The method openOrCreateDatabase(String, int, null) is undefined

I'm trying to open database as follows :
SQLiteDatabase myDatabase;
myDatabase = openOrCreateDatabase("sudoku.db", Context.MODE_PRIVATE, null);
This code works fine when I implement it in the Service class, but when I try to implement this in the onPostExecute eventhandler of the GeneraterThread class,implementing AsyncTask, I get the following error :
The method openOrCreateDatabase(String, int, null) is undefined for the type GeneraterThread
It looks like your just set wrong arguments for function.
There are these definitions in SDK:
public static SQLiteDatabase openOrCreateDatabase (String path, SQLiteDatabase.CursorFactory factory, DatabaseErrorHandler errorHandler)
public static SQLiteDatabase openOrCreateDatabase (String path, SQLiteDatabase.CursorFactory factory)
public static SQLiteDatabase openOrCreateDatabase (File file, SQLiteDatabase.CursorFactory factory)
But your calling that function is wrong.
Maybe you wanted to call openDatabase (String path, SQLiteDatabase.CursorFactory factory, int flags)?
In this case you just set arguments in wrong order - you're doing
openOrCreateDatabase("sudoku.db", Context.MODE_PRIVATE, null); //WRONG
instead:
openDatabase("sudoku.db", null, Context.MODE_PRIVATE); //RIGHT
It looks like you're trying to invoke openOrCreateDatabase method on GeneraterThread instance which doesn't have the method (and Service class has the method). You probably may pass in a reference to a Context object and invoke the method on it. Or use static method of SQLiteDatabase.openOrCreateDatabase().
Use it with your parent class
something like
myService.this.openOrCreateDatabase
its better use this,
db = SQLiteDatabase.openOrCreateDatabase(DATABASE_PATH_AND_NAME, null);
db.close();
db = SQLiteDatabase.openDatabase(DATABASE_PATH_AND_NAME, null, Context.MODE_APPEND);

Categories

Resources