2

I want to make my code clean & simple, so I want to create DB but I want to call it from another class through the main activity... I tried to do this but it didn't works... So, how should it works ??

Note: I'm kind a newbie in the android development. So, sorry for this type of question...

This is the main activity:

package com.DataStorage.Excercise;

import android.app.Activity;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;

public class DataStorageActivity extends Activity {

    private Context context;
    public SQLiteDatabase db_1=null;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

       AccessingDB acc= new AccessingDB(context);
       acc.onCreate(db_1);


    }
}

the other class that have the DB code.. :

package com.DataStorage.Excercise;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class AccessingDB extends SQLiteOpenHelper {

    public AccessingDB(Context context) {
        super(context, "Test_1", null, 1);
        // TODO Auto-generated constructor stub
    }


    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub

        db.execSQL(CreateTable());
        db.execSQL(Insert_1());

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub

    }

    /*
    public String CreateDB_1(){

        String s1= "create database Test_1;";
            return s1;
    }
    */

    private String CreateTable(){

        String s2="create table Customer(id_1 INTEGER,fn TEXT,ln TEXt);";
        return s2;
    }

    private String Insert_1(){

        String s3="insert into Customer values(1,'aaa','bbb');";
        return s3;

    }

}

Thanks alot...

Q8Y
  • 3,801
  • 12
  • 39
  • 38
  • Check this link [How to initialize sqlitedatabase once from a helper class in Android](http://stackoverflow.com/questions/7444327/how-to-initialize-sqlitedatabase-once-from-a-helper-class-in-android/7444373#7444373). Its about the initializing database class from main Activity. Thanks. – user370305 Sep 17 '11 at 09:56
  • it is a bad idea to do , because database to open requires context, see this link http://stackoverflow.com/questions/7444327/how-to-initialize-sqlitedatabase-once-from-a-helper-class-in-android/7444355#7444355 – Mohammed Azharuddin Shaikh Sep 17 '11 at 10:13
  • ok, then how can I make the activity class to extend also the sqlite ?? it can have only one extend... – Q8Y Sep 17 '11 at 11:16
  • have a look at this http://stackoverflow.com/questions/7453153/how-can-i-open-a-sqlite-database-in-android/7453188#7453188 – Mohammed Azharuddin Shaikh Sep 17 '11 at 11:18

1 Answers1

0

If you call the database in your MainActivity, can't you just call your MainActivity in your other activity's and then call the database using the Main? Like this:

In your MainActivity:

Database db;

In your other activity's:

MainActivity main;

main.db.somequery; //call the method you want

Or did I misunderstand and you need something else?

Remi
  • 1,289
  • 1
  • 18
  • 52