2011-05-05 204 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