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
Related
As a new Android programmer, I followed various online examples to create my own class that extends SQLiteOpenHelper...
public class MySQLiteHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION=1;
private static final String DATABASE_NAME="MyDB";
// Main Database Operations
public MySQLiteHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
#Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE_A);
db.execSQL(CREATE_TABLE_B);
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
...
public long addRecord(){
SQLiteDatabase db = this.getWritableDatabase();
....
etc
This works throughout my various activities with no issues. When I want to use it, I call...
MySQLiteHelper db = new MySQLiteHelper(this);
Then I call the various routines like...
db.addRecord();
Now I have created a secondary class that I need to use throughout the application. In that class, there are some routines that need to process data from the database. Problem is, I can't declare the MySQLiteHelper because of the this which errors out.
Doing some online search I think I understand that I need to get the Context from the application (or the calling activity??), but not sure how to go about doing that with this code.
As I mentioned, I am new to Android... so any code examples would be greatly appreciated.
EDIT
I need to clarify this secondary class as mentioned above. From the aforementioned online examples, the second class is used to "hold" the database record information. It looks something like this...
public class Acct {
private int _id;
private String _name;
private String _phone;
public Acct() {
}
public int get_id(){
return this._id;
}
public void set_id(int id) {
this._id=id;
}
public void set_name(String name){
this._name=name;
}
public String get_name(){
return this._name;
}
public void set_phone(String phone){
this._name=phone;
}
public String get_phone(){
return this._phone;
}
...
This is typically used with something like this in an activity...
MySQLiteHelper db = new MySQLiteHelper(this);
Acct acct = new Acct();
acct=db.getAccount(searchId);
myEditText.setText(acct.get_name());
...
Where my problem arises, I want to create a routine and code it IN the Acct class so it can be referenced such as...
acct.UpdateData();
This UpdateData routine is where we need to access the db and thus the MySQLiteHelper. It needs to be able, for each account, to go into the database, access some information from another table, do some processing, and store a summary back into this table for easier reference. As mentioned, there is no Context in the Acct class, so this is where I am getting confused.
And to make matters worse, because the Acct class is a 'holding' place for data from the DB, the online examples also use Acct IN the 'MySQLiteHelper' itself during the getAccount routine....
public Acct getAccount(int id){
SQLiteDatabase db = this.getReadableDatabase();
String SQL_STRING="SELECT * FROM "+ACCT_TABLE+" WHERE "+ACCT_FLD_ID+" = "+String.valueOf(id);
Cursor cursor =
db.rawQuery(SQL_STRING, null);
Acct acct = new Acct();
if (cursor!=null) {
cursor.moveToFirst();
acct.set_id(cursor.getInt((cursor.getColumnIndex(ACCT_FLD_ID))));
acct.set_name(cursor.getString(cursor.getColumnIndex(ACCT_FLD_NAME)));
acct.set_phone(cursor.getString(cursor.getColumnIndex(ACCT_FLD_PHONE)));
cursor.close();
} else {
acct = null;
}
db.close();
return acct;
}
I hope all this additional helped clarify what I am trying to do for the couple comments and answers posted so far. If you need more information, please ask. I'd like to get this to work, just still not sure how.
Your problem is you need a Context to call the constructor of MySQLiteHelper. You've been successful doing so in an Activity (which is a Context), but now you have some other class (which I will call "Foo") that isn't a Context and doesn't have one.
A quick solution is to make Foo take a Context in its constructor, and instantiate your MySQLiteHelper like so:
public class Foo {
private MyOpenHelper openHelper;
public Foo(Context context) {
openHelper = new MyOpenHelper(context.getApplicationContext());
}
}
If Foo is a singleton, you can do the same thing in whatever method obtains the instance (i.e. force the caller to provide a Context). Every application component either is a Context (Activity, Service) or has a Context (BroadcastReceiver gets one in onReceive(), ContentProvider has getContext()).
The use of getApplicationContext() here is worth noting: The Application object for your app is always a singleton--only one instance of it will exist for as long as your app is running (this is guaranteed by the OS). Activities can be destroyed, and creating a MySQLiteHelper with one can cause a memory leak. The application Context always exists and so it cannot be leaked.
Instead of using this, Try to use MainActivity.this or getApplicationContext().
Hope this help!
I have the following code below.
I am creating a database in my application that uses SQLiteOpenHelper.
I have couple of concerns and would appreciate some consults.
Direct answers for these were not found on stack overflow as they might be subjective.
1 - I will be using this database from several activities. However I am not planning on making this a singleton to avoid leaks, but rather I will be getting the getWritableDatabase() and getReadableDatabase() inside each method. I plan on doing a db.close() inside each activity's onDestroy() .Is this advisable ? given my app has couple of activites and is not a huge app.
2 - I am not following and DAO model, nor I am using a different class for every table.
The way I see it, I don't need to. Do I ?
3 - (A question rather than consult)
In the code below, I am not creating a database of the form
private SQLiteDatabase database;
So all the references to the database (from my activities) are being done via the methods in the same subclassed SQLiteOpenHelper, therefore I am referencing the physically created database directly via getWritableDatabase and getReadableDatabase.
Do I need to create an instance of SQLiteDatabase and use it ? Even inside the subclass of SQLiteOpenHelper ?
Below is the code.
public class DbHelper extends SQLiteOpenHelper
{
private static final String DATABASE_NAME = "myDbName";
private static final String DATABASE_TABLE = "myTable";
private static final int DATABASE_VERSION = 1;
private Context ctx;
public DbHelper(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
this.ctx = context;
}
#Override
public void onCreate(SQLiteDatabase db)
{
db.execSQL("CREATE TABLE myTable(_id INTEGER PRIMARY KEY, title TEXT);");
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
//nothing here now. maybe later.
}
public void insertTitle(String title)
{
ContentValues titleCV = new ContentValues();
titleCV .put("title", title);
getWritableDatabase().insert(DATABASE_TABLE, null, titleCV );
}
public void getTitles()
{
Cursor result = getReadableDatabase().rawQuery("SELECT _id, title FROM myTable", null);
while (result.moveToNext())
{
int id = result.getInt(0);
String titleGotten= result.getString(1);
}
result.close();
}
Q1
If you have a scenario within your app that have two parallel threads accessing the database, use a single instance of the SQLiteOpenHelper (singleton or member in the Application or whatever). If not you don't need to.
about calling db.close(), if it is in the onDestroy(), then it's fine.
Q2
a DAO is an abstraction layer to ease maintaining and scaling your project. If you are not going to scale or maintain your code (upcoming releases or something), then I suppose you don't need one.
Q3
You don't need to create an instance of SQLiteDatabse. when you call getReadableDatabase() or getWritableDatabase(), SQLiteOpenHelper creates and maintains an instance. The same instance is used the next time you call getReadable\WritableDatabase().
let me know if you still have questions.
step 1: make a staic instace of SqliteOpenHelper
step 2: you never close conexion to database, sqlite manage itself the sequencial access to write or read :)
private static ControladorBBDD instancia;
my class: public class ControladorBBDD extends SQLiteOpenHelper {
default :
private ControladorBBDD(Context ctx_p) throws Exception {
super(ctx_p, DB_NAME, null, DATABASE_VERSION);
try {
ControladorBBDD.ctx = ctx_p;
DB_PATH = ctx.getDatabasePath(DB_NAME).getAbsolutePath();
String myPath = DB_PATH;// + DB_NAME;
this.createDataBase();
db = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READWRITE);
} catch (SQLiteException ex) {
Conexiones.escribirLog(Log.getStackTraceString(ex),
ctx.getString(R.string.versionReal));
db.close();
}
}
and my way to implement a conexion to database:
public static synchronized ControladorBBDD getBBDD(Context ctx_p)
throws Exception {
if (instancia == null) {
instancia = new ControladorBBDD(ctx_p);
}
return instancia;
}
and to call it from activities:
dblectura = ControladorBBDD.getBBDD(getApplicationContext());
where private ControladorBBDD dblectura;
i hope that it helps, important thing is that you use applicationContext, no Activity context ;))
well if i were u i would create a class and the dbhelper as a subclass then i would use a open and a close function for main class and also the insert
whenever i want to use database i do it like this
mainclass mc=new mainclass(this);
mc.open();
mc.insert();
mc.close();
First let you know I am new in Android.
Is it good practice to use ContentProvider to handle database table operations only for one application?
Trying to create multiple classes to handle database table operations. Created a database helper as follow:
public class WSDatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "wsemp";
private static final int DATABASE_VERSION = 5;
public WSDatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
#Override
public void onCreate(SQLiteDatabase database) {
ItemTable.onCreate(database);
CustomerTable.onCreate(database);
}
#Override
public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {
ItemTable.onUpgrade(database, oldVersion, newVersion);
CustomerTable.onUpgrade(database, oldVersion, newVersion);
}
}
Created a class to handle database table operation:
public class CustomerBean {
private WSDatabaseHelper database;
#Override
public boolean onCreate() {
database = new WSDatabaseHelper(getContext());
return false;
}
public boolean insertObject(valObj) {
SQLiteDatabase db = database.getWritableDatabase();
db.insert(CustomerTable.TABLE_CUST_ACCOUNT_INDEX, null, values);
}
}
But now I am not sure how I can call this insertObject function from my activity or session file. I tried by CustomerBean.isnertObject(obj) but it's asking to change the method to static.
Is it good practice to use ContentProvider to handle database table operations only for one application?
If your data is exclusive only for your application and other application cannot use it I don't see any reason to use ContentProviders. ContentProvider is used as an interface for sharing your application's data to other application. If your data can be shared or other application is dependent on it then you have to use ContentProvider.
Also you can create set of permissions to your content providers to restrict some operations in the provider.
Exception:
CREATE TABLE android_metadata failed
Failed to setLocale() when constructing, closing the database
android.database.sqlite.SQLiteException: database is locked
My app works fine and has no db issues, except when onUpgrade() is called.
When onUpgrade is automatically called, it tries to use the CarManager class below to do data manipulation required for the upgrade. This fails because the db is locked.
Because this seems like it should be a normal thing to do, it seems that I must not be structuring the following code correctly (two classes follow, a helper and a table manager):
public class DbHelper extends SQLiteOpenHelper {
private Context context;
//Required constructor
public DbAdapter(Context context)
{
super(context, "my_db_name", null, NEWER_DB_VERSION);
this.context = context;
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
overrideDB = db;
CarManager.migrateDataForOnUpgrade(context);
}
}
public class CarManager {
DbHelper dbHelper;
public CarManager(Context context)
{
dbHelper = new DbHelper(context);
}
public void addCar(String make, String model)
{
ContentValues contentValues = new ContentValues();
contentValues.put("make", make);
contentValues.put("model", model);
SQLiteDatabase db = dbHelper.getWritableDatabase();
db.insert("car", null, contentValues);
db.close();
}
public static void migrateDataForOnUpgrade()
{
//Code here that migrates data when onUpgrade() is called
//Db lock happens here
}
}
Any ideas?
Do people set up table manager (ex: dao) differently than this?
edit: I talked to the google team # android developer hours, and they said onUpgrade3 was never meant to do anything like structural changes (alters). So yes, it seems like there are some hacks that must be used in many instances right now.
I use the following model by extending the Application class. I maintain a single static instance of my db helper which all other app components use...
public class MyApp extends Application {
protected static MyAppHelper appHelper = null;
protected static MyDbHelper dbHelper = null;
#Override
protected void onCreate() {
super.onCreate();
...
appHelper = new MyAppHelper(this);
dbHelper = MyAppHelper.createDbHelper();
dbHelper.getReadableDatabase(); // Trigger creation or upgrading of the database
...
}
}
From then on any class which needs to use the db helper simply does the following...
if (MyApp.dbHelper == null)
MyApp.appHelper.createDbHelper(...);
// Code here to use MyApp.dbHelper
I want to read (only) records from data/data/com.android.providers.telephony/databases/mmssms.db (I know no official API, etc. but since it lacks an official API I have to do it). I thought I can do it like written How to access an existing sqlite database in Android? in the last answer, just by
SQLiteDatabase db = SQLiteDatabase.openDatabase("data/data/com.android.providers.telephony/databases/mmssms.db ", null, 0);
but the system is not able to open that DB file. Next I thought I might have to create a full blown DB Helper class
package com.test.dbaccess;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DbAdapterMms
{
private final Context ctx;
private DatabaseHelper dbHelper;
public SQLiteDatabase db;
private static class DatabaseHelper extends SQLiteOpenHelper
{
private final Context ctx;
DatabaseHelper(Context context)
{
super(context, "data/data/com.android.providers.telephony/databases/mmssms.db", null, 1);
this.ctx = context;
}
#Override
public void onCreate(SQLiteDatabase db)
{
//do nothing, this should exist
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
//do nothing, reading only
}
}
public DbAdapterMms(Context context)
{
this.ctx = context;
}
public void open()
{
dbHelper = new DatabaseHelper(ctx);
db = dbHelper.getReadableDatabase();
}
public void close()
{
dbHelper.close();
}
}
It still doesn't work, this time I think it might be an issue in the call super(); since there the DBVersion has to be given. The version I don't know, I just want to read the existing db. Can't be rocket sience...
Any help?
Thanks, A.
I think you cannot read the database of another app. As in Android every app has it's own storage location on the file system which cannot be accessed from other processes. The only exception is if it is defined in the manifest file that other applications are allowed to access the data. As you're trying to access the data of an application that you don't own, you cannot edit its manifest file to grant yourself access to the data.
The only way to read the database would be if you got a rooted phone. Then you can pull the database on you PC and push it back into the store of you app. But I think that's not what you're looking for.
You can use something like this to open the smsmms database
Cursor cSys = getContentResolver().query("content://mms-sms",null, null, null,null);
Using this cursor you can manipulate the database.
getting list of mms