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);
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.
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
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 have created class cl_DBHandlerwhich extends SQLiteOpenHelper. and i have created class my_srvcwhich extends service. i have intialise object m_DBHandlerof class cl_DBHandlerin service but while initialization object i have to pass parameters to cl_DBHandler(context, name, factory, version), i am not getting exactly what to pass in this.
i have pass like this cl_DBHandler(getappcontext(),"databasename.sqlite",null,1)
2nd thing: i am calling one function from class with help of m_DBHandler object but i am getting value null of object m_DBHandler at every call though i have intialise that object in service.can anyone tell me or guide me to solve this problem.
Thanks in Advance--
Use the following constructor for your cl_DBHandler class
public static final String DATABASE_NAME = "MyDBName.db";
public static final int DATABASE_VERSION = 1;
public cl_DBHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
In your service or activity:
cl_DBHandler m_DBHandler = new cl_DBHandler(this);
Then open your database:
SQLiteDatabase db;
try {
db = m_DBHandler.getWritableDatabase();
} catch (SQLiteException ex) {
db = m_DBHandler.getReadableDatabase();
}
// do anything you like for example:
Cursor cursor = db.rawQuery(strQuery, null);
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