what is the meaning of the factory argument is this statement
public SQLiteDatabase openOrCreateDatabase(String name, int mode, CursorFactory factory)
and when is this argument used?
public SQLiteDatabase openOrCreateDatabase(String name, int mode, CursorFactory factory)
SQLiteDatabase.CursorFactory
Used to allow returning sub-classes of Cursor when calling query.
Link
Related
Hi I'm using broadcast receiver in service to get message and update it in db. I successfully receive messages but my problem is when ever it tries to insert in db when the activity is destroyed (but the update is happening when the app is running),it shows the following errors
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.sqlite.SQLiteDatabase android.content.Context.openOrCreateDatabase(java.lang.String, int, android.database.sqlite.SQLiteDatabase$CursorFactory, android.database.DatabaseErrorHandler)' on a null object reference
05-13 04:01:46.896 4171-4171/com.example E/AndroidRuntime: at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:274)
05-13 04:01:46.896 4171-4171/com.example E/AndroidRuntime: at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:223)
05-13 04:01:46.896 4171-4171/com.example E/AndroidRuntime: at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
05-13 04:01:46.896 4171-4171/com.example E/AndroidRuntime: at com.scanlibrary.DbHandler.addImage(DbHandler.java:132)
I show some of my codes
Broadcastreceiver:
public static class ReceiveSms extends BroadcastReceiver {
public ReceiveSms(){
super();
}
private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
#Override
public void onReceive(Context context, Intent intent) {
// receive message
MainActivity mainActivity = new MainActivity();
mainActivity.upDateMessage(message);
}
}
In MainActivity.class:
public void upDateMessage(String message){
DbHandler db = new DbHandler(this,null,null,1);
db.addImage(message);
}
In my DbHandler.class:
//constructor
public DbHandler(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, DATABASE_NAME, factory, DATABASE_VERSION);
}
public void addImage(CroppedImageClass cic){
ContentValues values = new ContentValues();
// do some editing
try{
SQLiteDatabase db = this.getWritableDatabase();
db.insert(TABLE_IMAGE, null, values);
db.close();
}catch (NullPointerException npe){
}
}
I searched Google but I didn't found a solution to solve my problem. Any help will be appreciated and thanks in advance
You should never instantiate an activity
MainActivity mainActivity = new MainActivity();
This object is not part of any life cycle, let alone in any way set up as a context.
If you want an activity or service to handle your database operation, you need to create an intent for that and call context.startActivity(intent) or context.startService(intent). The message can be added as an extra to the intent.
Also if you do something like
public DbHandler(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, DATABASE_NAME, factory, DATABASE_VERSION);
}
remove the parameter you're not using. The constructor
public DbHandler(Context context, SQLiteDatabase.CursorFactory factory) {
super(context, DATABASE_NAME, factory, DATABASE_VERSION);
}
would suffice.
On the documentation it say's
execSQL(String sql) Throws: SQLException - if the SQL string is invalid
but it do not in android studio, any one having this problem ?
public class test extends SQLiteOpenHelper {
public test(Context context, String name, CursorFactory factory, int version) {
super(context, name, factory, version);
}
#Override
public void onCreate(SQLiteDatabase db) {
try {
db.execSQL("some sql code");
}
catch(SQLException e){
//do something
}
}
//some other overrides
}
This will give me error an error because execSQL() is not throwing an exception.when checking its declaration I get
public void execSQL(String sql) {
executeSql(sql, null);
}
So my question is in the documentation and even on eclipse I am sure it returns a SQLException but here on Android Studio its not.
execSQL() can throw android.database.SQLException. If you inadvertently imported java.sql.SQLException as first suggested by Android Studio, you'll get an error
Exception 'java.sql.SQLException' is never thrown in the corresponding try block
To fix it, import android.database.SQLException instead.
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.
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);
Hii everybody ,
I am noob at android and need some help...
I am developing an app which requires me to write to an SQLiteDatabase in one activity and access it from another activity . I am facing a problem implementing this. Any suggestions/ideas as to how we can share the database across multiple activities ...?
I'd recommend you to use the SQLiteOpenHelper class.
Simply use the same database name consistently across your activities, it should not cause any problem.
SQLiteOpenHelper helper = new SQLiteOpenHelper(
context, R.string.db_name, null, 1);
SQLiteDatabase db = helper.getWritableDatabase();
The issue of accessing the same database two different activities can be handled in a few different ways.
The simplest, which should work for your case, is to create a new class that extends SQLITEOpenHelper and instantiate that class from both activities.
Android has no problem with multiple Activities or processes accessing the SQlite database simultaneously.
Simply you can make a common Class for DataBase and use it by creating object.
public class DbOperation extends SQLiteOpenHelper{
public static final String name="mydb.db";
public static final String MainTab="MainTab";
public static final String ID="_ID";
public static final String LevelName="LevelName";
int version =2;
public DbOperation(Context context, String name, CursorFactory factory,
int version) {
super(context, name,null, version);
}
#Override
public void onCreate(SQLiteDatabase db) {
String str="CREATE TABLE "+MainTab+"("+ID+" integer primary key autoincrement,"+LevelName+" text not null unique key)";
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
Use this data base in any activity in below way
DbOperation ob=new DbOperation ();
SQLiteDatabase db=new SQLiteaDatabase();
db=ob.getWritableDataBase();
and now you can use operation like query,delete,update
Cursor cur=db.query(Table_name,null,null,null,null); etc