2011-05-05 3 views
0

我有一個類,在其中創建數據庫。我想在另一個類中進行數據操作。我有以下代碼:閱讀數據庫Android SQLite

public class DBCreation 
{ 
    public static final String KEY_CODPROJECT = "codProject"; 
    public static final String KEY_NOME = "nome"; 

    private static final String TAG = "DBCreation"; 

    private static final String DATABASE_NAME = "scrum_management.db"; 
    private static final String DATABASE_TABLE = "project"; 

    private static final int DATABASE_VERSION = 1; 

    private static final String DATABASE_CREATE = 
     "create table project (codProject integer primary key autoincrement, " 
     + "name text);"; 


    private final Context context; 

    private DatabaseHelper DBHelper; 
    private SQLiteDatabase db; 

    public DBCreation(Context ctx) 
    { 
     this.context = ctx; 
     DBHelper = new DatabaseHelper(context); 
    } 

    private static class DatabaseHelper extends SQLiteOpenHelper 
    { 
     DatabaseHelper(Context context) 
     { 
      super(context, DATABASE_NAME, null, DATABASE_VERSION); 
     } 


     @Override 
     public void onCreate(SQLiteDatabase db) 
     { 
      db.execSQL(DATABASE_CREATE); 

     } 

     @Override 
     public void onUpgrade(SQLiteDatabase db, int oldVersion, 
     int newVersion) 
     { 
      Log.w(TAG, "Upgrading database from version " + oldVersion 
        + " to " 
        + newVersion + ", which will destroy all old data"); 
      db.execSQL("DROP TABLE IF EXISTS projecto"); 
      onCreate(db); 
      } 


    }  

    public DBCreation open() throws SQLException 
    { 
     db = DBHelper.getWritableDatabase(); 
     return this; 
    } 

    public void close() 
    { 
     DBHelper.close(); 
    } 

} 

然後我有一個類來訪問數據。

public class DBAdapter extends DBCreation{ 

    public DBAdapter(Context ctx) { 
    super(ctx); 
    // TODO Auto-generated constructor stub 
    } 

//---insere um novo local na base de dados--- 
    public long insertLocal(String descricao, String pais, String cidade,String categoria_desc) 
    { 
     ContentValues initialValues = new ContentValues(); 
     initialValues.put(KEY_NOME, nome); 
    } 
} 

這是錯誤的。有人可以幫助我如何在另一個類中執行數據操作嗎?

感謝

回答

2

請參閱我的文章在這裏:

Storing Lists to A Database, and Retrieving Them All Together : Android

有一點要提的是,在您的公用事業類,你可以有一個私人SQLite數據庫對象,這樣你可以參考同樣的數據庫,只要你需要。如果您需要進一步的幫助,請點擊評論。

+0

你好。感謝您的關注,但這不是我尋求的答案,對於我的問題。感謝您的關注 – unnamed 2011-05-05 18:40:10