从Android Studio中的两个不同活动访问SQLite数据库

时间:2016-04-27 06:13:19

标签: android sqlite

我有三个班级。 saveinSQL(我的数据库类),Home(我要访问数据库的类),Create(我创建数据库的类)。我有一个问题。我想访问Home类中Create类创建的数据库。任何人都可以帮我这个吗?

public class saveinSQL extends SQLiteOpenHelper {
    private static final int DATABASE_VERSION = 2;
    private static final String PETITIONS_TABLE_NAME = "Petitions";
    private static final String DATABASE_NAME = "myDB.db";
    public static final String PETITIONS_COLUMN_ID = "id";
    public static final String PETITIONS_COLUMN_HEADING = "heading";
    public static final String PETITIONS_COLUMN_DESCRIPTION = "description";
    public static final String PETITIONS_COLUMN_PEOPLE = "people";

public saveinSQL(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL(
            "create table Petitions" +
                    "(id integer primary key, heading text, description text, people text)"
    );
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS Petitions");
    onCreate(db);
}

public boolean insertPetition  (String heading, String description,String people)
{
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put("heading", heading);
    contentValues.put("description", description);
    contentValues.put("people", people);
    db.insert("Petitions", null, contentValues);
    return true;
}

public Cursor getData(int id){
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor res =  db.rawQuery( "select * from Petitions where id="+id+"", null );
    return res;
}




public  String getHeader(int id){
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor res = db.rawQuery("select * from Petitions where id=" + id + "", null);
    res.moveToFirst();
    for(int i = 1; i < id; i++){
        res.moveToNext();
    }
    String header = res.getString(res.getColumnIndex(saveinSQL.PETITIONS_COLUMN_HEADING));
    return header;
}

public String getDescription(int id){
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor res = db.rawQuery("select * from Petitions where id=" + id + "", null);
    res.moveToFirst();
    for(int i = 1; i < id; i++){
        res.moveToNext();
    }
    String descrip = res.getString(res.getColumnIndex(saveinSQL.PETITIONS_COLUMN_DESCRIPTION));
    return descrip;
}

public String getPeople(int id){
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor res = db.rawQuery("select * from Petitions where id=" + id + "", null);
    res.moveToFirst();
    for(int i = 1; i < id; i++){
        res.moveToNext();
    }
    String people = res.getString(res.getColumnIndex(saveinSQL.PETITIONS_COLUMN_PEOPLE));
    return people;
}




public int numberOfRows(){
    SQLiteDatabase db = this.getReadableDatabase();
    int numRows = (int) DatabaseUtils.queryNumEntries(db, PETITIONS_TABLE_NAME);
    return numRows;
}

public boolean updatePetitiont (Integer id, String heading, String description, String people)
{
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put("heading", heading);
    contentValues.put("description", description);
    contentValues.put("people", people);
    db.update("Petitions", contentValues, "id = ? ", new String[] { Integer.toString(id) } );
    return true;
}

public Integer deletePetition (Integer id)
{
    SQLiteDatabase db = this.getWritableDatabase();
    return db.delete("Petitions",
            "id = ? ",
            new String[] { Integer.toString(id) });
}

public ArrayList<String> getAllPetitions()
{
    ArrayList<String> array_list = new ArrayList<String>();

    //hp = new HashMap();
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor res =  db.rawQuery( "select * from Petitions", null );
    res.moveToFirst();

    while(res.isAfterLast() == false){
        array_list.add(res.getString(res.getColumnIndex(PETITIONS_COLUMN_HEADING)));
        res.moveToNext();
    }
    return array_list;
    }
}

1 个答案:

答案 0 :(得分:0)

在Home类中,您必须实例化saveinSQL的对象,并调用与您要执行的活动相对应的方法。例如:

saveinSQL mySaveinSQL = new saveinSQL(<context>);
mySaveinSQL.getHeader(id);