2012-03-07 60 views
0

我正在爲我正在開發的Android應用創建數據庫。我試圖學習如何遵循正確的標準進行編碼,並且我在DbHelper類的onCreate方法中讀取了數據庫被創建的地方。我還讀到它在onCreate方法中應該用數據填充數據庫。它是否正確?如果是這樣,我如何傳遞一個對象到onCreate方法,以便我可以遍歷它並填充數據庫?Android SQLite:調用onCreate方法並傳入一個對象

public class DbHelper extends SQLiteOpenHelper 
{ 
private static String DATABASE_NAME = "FodoSubstitutes.db"; 
private static String FOOD_TABLE = "Food"; 

//Creates the database with db name and calls onCreate(). 
public DbHelper(Context context) 
{ 
    super(context, DATABASE_NAME, null, 1); 
} 

@Override 
public void onCreate(SQLiteDatabase db) 
{ 
    //System.out.println("in onCreate"); 
    //assocID food healthierFood category description count submittedBy 
    String sql = "CREATE TABLE IF NOT EXISTS " + FOOD_TABLE + 
       "(Food_ID integer primary key, " + 
       "Food_Name string not null, " + 
       "Food_Aliases string, " + 
       "Hints string, " + 
       "Category string, " + 
       "Subcategory string, " + 
       "Description string, " + 
       "Submission_ID int, " + 
       "Comment_ID int, " + 
       "Count int); "; 
    db.execSQL(sql); 

} 
} 

我的想法是做這樣的事情。
DbHelper.onCreate(Food myFoodObj);

但這不起作用。有什麼想法嗎?這是一種簡單明顯的東西,我可以忽略。

+0

將其分成兩部分。第一部分:創建數據庫。第二部分:創建一個方法,用你的對象更新數據庫,比如說創建一個名爲'addFood'的方法,在那裏你執行一個合適的'db.execSQL','db。查詢「等等。您可能還需要一些幫助器方法來檢查數據庫是否存在等等。另外,請記住:http://stackoverflow.com/a/7164505/429047 – 2012-03-07 04:34:09

回答

1

不,它是沒有必要裏面onCreate()數據來填充你的數據庫。 onCreate()僅用於創建數據庫和表。

我的想法是做這樣的事情。 DbHelper.onCreate(食品 myFoodObj);

這是錯誤的,你不需要調用onCreate()。當您僅引用您在其中使用Database類的類的實例時,將會調用onCreate()

DbHelper dbhelper = new DbHelper(this); 
0
public class DBAdapter { 

String sql = "CREATE TABLE IF NOT EXISTS " + FOOD_TABLE + 
       "(Food_ID integer primary key, " + 
       "Food_Name string not null, " + 
       "Food_Aliases string, " + 
       "Hints string, " + 
       "Category string, " + 
       "Subcategory string, " + 
       "Description string, " + 
       "Submission_ID int, " + 
       "Comment_ID int, " + 
       "Count int); "; 
private DatabaseHelper DBHelper; 
    private SQLiteDatabase db; 

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

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

     } 

     public void onCreate(SQLiteDatabase db) { 
      db.execSQL(sql); 

     } 

     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("DELETE FROM" + DATABASE_USERSTATUS); 
      onCreate(db); 
     } 
    } 

    // ---opens the database--- 
    public SecureSMSDBAdapterAES open() throws SQLException { 
     db = DBHelper.getWritableDatabase(); 

     return this; 
    } 

    // ---closes the database--- 
    public void close() { 
     DBHelper.close(); 
    } 

    // ---insert values into the database--- 

    public long insertDataByQuery(String q) { 

     db.execSQL(q); 

     return 0; 
    } 
} 

而在其中u要實例數據庫

將對DBAdapter將對DBAdapter =新將對DBAdapter(這)類; dbAdapter.open(); //如果有任何查詢寫在這裏 dbadapter.close();

和U可以按照本

http://www.vogella.de/articles/AndroidSQLite/article.html

0

在SQLiteOpenHelper的onCreate方法是創建一個數據庫...,我認爲這是沒有用的填充你的數據庫中onCreate方法.. 作爲user1203673 said..you能的這部分代碼添加到將對DBAdapter類填充數據庫..

public void createcustom(String category,String subcategory){ 
ContentValues values = new ContentValues(2); 
values.put(Category string,category); 
values.put(Subcategory string,subcategory); 
long insertId = customdb.insert(sqliteplaylist.LIBRARY, null,values); 

} 

,並從你的活動稱這是 「DBAdapter.createcustom(foodname,foodcategory);」

0

這是我最終做的。我創建了一個DataLyaer類,它與DbHelper一起工作,看起來像這樣。

public class DataLayer 
{ 

private DbHelper _dbHelper; 

public DataLayer(Context c) 
{ 
    _dbHelper = new DbHelper(c); 
} 


public void populateMyDB(Table myTables) 
{ 
    SQLiteDatabase db = _dbHelper.getWritableDatabase(); 
    try 
    { 
     //do stuff. 

    } 
    finally 
    { 
     if (db != null) 
      db.close(); 
    } 

} 
} 

然後我打電話給我的DataLayer。

DataLayer d = new DataLayer(context); 
d.populateMyDB(myFood); 

感謝您的幫助。問題是我無法弄清楚如何在onCreate()方法或者我的DbHelper類中添加這些東西。我想我無法相信任何在網上發佈內容的人。

相關問題