2012-03-07 61 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类中添加这些东西。我想我无法相信任何在网上发布内容的人。

相关问题