如何通过android中的不同活动使用数据库?

时间:2013-09-11 21:44:36

标签: android database sqlite

我在Android项目中创建了一个SQLite数据库,用于存储有关使用SqliteOpenHelper的家庭信息。我有两个Activities.One是一个列表活动,包含所有家庭的列表,以及一个由listActivity启动以添加新家庭的列表活动。我的问题与db的使用有关。哪个更好?在这两个活动中都有db的实例吗?或者只在一个活动中有一个静态的db实例,并在需要访问它的每个其他活动中使用它?通过不同的活动使用数据库的更好方法是什么?

public class HomeDatabaseHandler extends SQLiteOpenHelper{

    //Database Version
    private static final int DATABASE_VERSION = 1;

    //Database name static value
    private static final String DATABASE_NAME = "homeManager";

    //Table name
    private static final String HOMES_TABLE = "homes";
    //more code here for adding creating etc...
}


public class MainActivity extends ListActivity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //Here is my db for populating listView
        HomeDatabaseHandler db = new HomeDatabaseHandler(this);

}


//in Activity to add home
 @Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
         case R.id.action_add_done:
             //more code here to get values from views
             //create new db to add values
             HomeDatabaseHandler db = new HomeDatabaseHandler(this);

             //method to add home
             db.addHome

    }
}    

这就是它现在的样子,创建Handler的实例并添加。那你觉得怎么样?我觉得这不是最好的方式......

2 个答案:

答案 0 :(得分:0)

嗯,我会说,一旦你的广告,你可以正确地做到这一点,然后你不必再担心了。
这是一个简单明了的例子,你可以在半天内完成并运行。

Android Content Providers using MySql

答案 1 :(得分:0)

方法覆盖是在不同活动中使用SQLite的最佳方式。

Adapter.java

import java.util.ArrayList;
import java.util.List;

import com.bambeeq.conferencecall.domain.ProfileDo;
import com.bambeeq.conferencecall.domain.StepDO;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class ConferenceAdapter {
    SQLiteDatabase database_ob;
    ConferenceOpenHelper openHelper_ob;
    Context context;

    public ConferenceAdapter(Context c) {// constructor
        context = c;
    }


    public ConferenceAdapter opnToRead() {// method for open and read the database to perform the operations
        openHelper_ob = new ConferenceOpenHelper(context, openHelper_ob.DATABASE_NAME, null, openHelper_ob.VERSION);
        database_ob = openHelper_ob.getReadableDatabase();
        return this;
    }

    public ConferenceAdapter opnToWrite() {// method for open and write the database to perform the operations
        openHelper_ob = new ConferenceOpenHelper(context, openHelper_ob.DATABASE_NAME, null, openHelper_ob.VERSION);
        database_ob = openHelper_ob.getWritableDatabase();
        return this;
    }

    public void Close() {// method for closing the database
        database_ob.close();
    }

    public long insertDetails(String value, String delay, int profileId) {// insert method for steps
        ContentValues contentValues = new ContentValues();
        contentValues.put(openHelper_ob.VALUE, value);
        contentValues.put(openHelper_ob.DELAY, delay);
        contentValues.put(openHelper_ob.PROFILE_ID, profileId);
        opnToWrite();
        long val = database_ob.insert(openHelper_ob.STEP_TABLE_NAME, null, contentValues);
        Close();
        return val;
    }


    public long insertProfile(String profile, String status,String optionstatus) { // method to insert the profile name
        ContentValues contentValues = new ContentValues();
        contentValues.put(openHelper_ob.PROFILE, profile);
        contentValues.put(openHelper_ob.STATUS, status);
        contentValues.put(openHelper_ob.OPTION_STATUS, optionstatus);
        opnToWrite();
        long val = database_ob.insert(openHelper_ob.PROFILE_TABLE_NAME, null, contentValues);
        Close();
        return val;
    }

    public Cursor queryForSteps(int profileId) {// method to display the edit profile name and its related steps.
        String[] cols = { openHelper_ob.KEY_ID, openHelper_ob.VALUE, openHelper_ob.DELAY };
        opnToWrite();
        Cursor c = database_ob.query(openHelper_ob.STEP_TABLE_NAME, cols, openHelper_ob.PROFILE_ID + "=" + profileId, null, null, null, null);
        return c;
    }

    public Cursor queryAll(int stepId) {// method to display the edit profile
        String[] cols = { openHelper_ob.KEY_ID, openHelper_ob.VALUE, openHelper_ob.DELAY };
        opnToWrite();
        Cursor c = database_ob.query(openHelper_ob.STEP_TABLE_NAME, cols, openHelper_ob.KEY_ID + "=" + stepId, null, null, null, null);
        return c;
    }
public long updateldetail(int rowId, String value, String delay) {// method to update the value and delay
        ContentValues contentValues = new ContentValues();
        contentValues.put(openHelper_ob.KEY_ID, rowId);
        contentValues.put(openHelper_ob.VALUE, value);
        contentValues.put(openHelper_ob.DELAY, delay);
        opnToWrite();
        long val = database_ob.update(openHelper_ob.STEP_TABLE_NAME, contentValues, openHelper_ob.KEY_ID + "=" + rowId, null);
        Close();
        return val;
    }
public int deleteHomeRecord(int profileId) {// method to delete the profileId
        opnToWrite();
        int vals = database_ob.delete(openHelper_ob.STEP_TABLE_NAME, openHelper_ob.PROFILE_ID + "=" + profileId, null);
        int val = database_ob.delete(openHelper_ob.PROFILE_TABLE_NAME, openHelper_ob.P_ID + "=" + profileId, null);
        Close();
        return val;
    }

OpenHelper.java

public class ConferenceOpenHelper extends SQLiteOpenHelper {
    public static final String DATABASE_NAME = "confDB";
    public static final String PROFILE_TABLE_NAME = "profile";
    public static final String STEP_TABLE_NAME = "step";
    public static final int VERSION = 3;    
    public static final String KEY_ID = "_id";
    public static final String P_ID = "_id";
    public static final String TEMP_VARIABLE = "0";
    public static final String  INSERT_TEMPLATE= "0";
    public static final String TEMPLATE = "template";
    public static final String VALUE = "value";
    public static final String DELAY = "delay";
    public static final String PROFILE_ID = "profile_id";
    public static final String PROFILE = "profile";
    public static final String STATUS = "status";
    public static final String OPTION_STATUS = "optionstatus";

//query for creating tables for profile and steps
    private static final String CREATE_PROFILE_TABLE = "create table profile(_id integer not null primary key autoincrement,profile text null,status text not null,optionstatus text not null)";
    private static final String CREATE_STEP_TABLE = "create table step (_id integer not null primary key autoincrement,value text null,delay integer null,profile_id integer default 0, FOREIGN KEY (profile_id) REFERENCES profile(id))";

    //constructor
    public ConferenceOpenHelper(Context context, String name,
            CursorFactory factory, int version) {
        super(context, name, factory, version);
        // TODO Auto-generated constructor stub
    }

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

        db.execSQL(CREATE_PROFILE_TABLE);
        db.execSQL(CREATE_STEP_TABLE);
    }

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

        onCreate(db);
    }

}

插入方法:

@覆盖

public void onClick(DialogInterface dialog, int whichButton) {

                    String profile = profileEditText.getText().toString();


                    long vals = adapter.insertProfile(profile, HOME,CUSTOM_DIALER);

你可以过度使用这样的方法进行更新,删除。