2012-03-27 3 views
1

나는 다른 클래스에서 호출해야하는 안드로이드 응용 프로그램을 만들고 있습니다. 나는 내 데이터베이스를 다음과 같이 설정했다.안드로이드에서 SQLite를위한 데이터베이스 호출 코드

public class DataBaseHelper { 

private static final String DATABASE_NAME = "example.db"; 
private static final int DATABASE_VERSION = 1; 
private static final String TABLE_NAME = "table1"; 

private Context context; 
private SQLiteDatabase db; 

private SQLiteStatement insertStmt; 
private static final String INSERT = "insert into "+ TABLE_NAME + "(name) values (?)"; 

    public DataBaseHelper(Context context) { 
     this.context = context; 
     OpenHelper openHelper = new OpenHelper(this.context); 
     this.db = openHelper.getWritableDatabase(); 
     this.insertStmt = this.db.compileStatement(INSERT); 
    } 

    public long insert(String name) { 
     this.insertStmt.bindString(1, name); 
     return this.insertStmt.executeInsert(); 
    } 

    public void deleteAll() { 
     this.db.delete(TABLE_NAME, null, null); 
    } 

    public List<String> selectAll() { 
     List<String> list = new ArrayList<String>(); 
     Cursor cursor = this.db.query(TABLE_NAME, new String[] { "name" }, 
     null, null, null, null, "name desc"); 
     if (cursor.moveToFirst()) { 
     do { 
     list.add(cursor.getString(0)); 
     } while (cursor.moveToNext()); 
    } 
    if (cursor != null && !cursor.isClosed()) { 
     cursor.close(); 
    } 
    return list; 
    } 

    private static class OpenHelper extends SQLiteOpenHelper { 

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

    @Override 
    public void onCreate(SQLiteDatabase db) { 
     db.execSQL("CREATE TABLE " + TABLE_NAME + "(id INTEGER PRIMARY KEY, name TEXT)"); 
    } 

    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
    Log.w("Example", "Upgrading database, this will drop tables and recreate."); 
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME); 
    onCreate(db); 
    } 
    } 
} 

다른 클래스에서 이것을 호출하면 어떻게 처리 할 것인가? 이 클래스를 변경해야합니까?

답변

1

이것은 간단합니다. jst는 DB 클래스의 객체를 만들고 그 방법을 사용합니다.

DataBaseHelper db=new DataBaseHelper(this) ; 
db.insert("HellooAndroid"); 
db.deleteAll(); 

더 참조 할 수 있도록이 링크를 참조하십시오

How to write reusable code for Database in Android

+0

을 호출하면 하나의 클래스에서 변경하고 다른 클래스에서 호출 할 수 있습니까? –

+0

네, 어디서든지 호출 할 수 있지만 각 클래스를 객체화해야합니다. db 클래스의 메소드를 사용할 수 있습니다. – Hasmukh

+0

지난 한 번의 링크를 통해 알 수 있습니다. – Hasmukh

0

내가 그런 말 것 : 응용 프로그램을 확장하는 클래스를 만듭니다. 응용 프로그램 컨텍스트를 저장하는 싱글 톤을 만듭니다. 그런 다음 데이터베이스 클래스를 편집하고 응용 프로그램에있는 컨텍스트로 컨텍스트를 바꿉니다.

public class MyApplication extends Application 
{ 
    private static MyApplication instance = null; 

    public MyApplication() 
    { 
     instance = this; 
    } 

    /** 
    * Instanciates the class and initializes the variables 
    * 
    */ 
    @Override 
    public void onCreate() 
    { 
     super.onCreate(); 
     instance = this; 
    } 

    /** 
    * Creates a singleton of the current class 
    * 
    * @return Returns the app's context 
    */ 
    public static Context getInstance() 
    { 
     if (instance == null) instance = new MyApplication(); 

     return instance; 
    } 
} 

을 다음 앱 컨텍스트 (! 아니 활동 컨텍스트)을 필요로 할 때, 당신은 그런)

을 MyApplication.gesInstance를 (호출 할 수 있습니다 : 여기

내가하고 싶은 말의 예입니다 데이터베이스에 항목을 삽입해야 할 때

DataBaseHelper db_h = new DataBaseHelper() ; 
db_h.insert(AnObject); 
db_h.deleteAll(); 
관련 문제