![](/img/trans.png)
[英]Android SQLite convert time in onCreate method before passing it into object
[英]Android SQLite: Calling the onCreate Method and passing in an Object
我正在為我正在使用的Android應用程序創建數據庫。 我正在嘗試學習如何按照正確的標准進行編碼,並且在DbHelper類的onCreate方法中了解到如何創建數據庫。 我還讀到,應該在onCreate方法中用數據填充數據庫。 這個對嗎? 如果是這樣,如何將對象傳遞給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);
但這是行不通的。 有什么想法嗎? 我必須忽略一些簡單明了的東西。
不,沒有必要在onCreate()
填充數據。 onCreate()僅用於創建數據庫和表。
我的想法是要做這樣的事情。 DbHelper.onCreate(Food myFoodObj);
這是錯誤的,您不需要調用onCreate()。 當您僅引用使用數據庫類的類的實例時, onCreate()
會被調用。
DbHelper dbhelper = new DbHelper(this);
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;
}
}
在您要實例化數據庫的類中
DBAdapter dbAdapter =新的DBAdapter(this); dbAdapter.open(); //如果有任何查詢,請在此處寫dbadapter.close();
你可以按照本教程
SQLiteOpenHelper中的onCreate方法用於創建數據庫...,我認為用onCreate方法填充數據庫沒有用。如user1203673所說。您可以將這部分代碼添加到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);”。
這就是我最終要做的。 我創建了一個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類中添加這些東西。 我想我不敢相信任何在網上發布內容的人。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.