2011-05-21 117 views

回答

13

在DBAdapter.java類文件中,你可以這樣做。


@Override 
    public void onCreate(SQLiteDatabase db) { 
     db.execSQL(CREATE_TABLE_1); 
     db.execSQL(CREATE_TABLE_2); 
     db.execSQL(CREATE_TABLE_3); 
     db.execSQL("insert into " + DATABASE_TABLE3 + "(" + KEY_ROWID + "," 
       + KEY_DEF + ") values(1,'')"); 
    } 

喜歡這類型。

+0

難道只能用於應用程序或每次第一次運行的運行? – 2013-07-01 07:47:07

+0

@Setayeshi,來自developer.android.com「首次創建數據庫時調用,這是創建表和初始表的總體應該發生的地方。」 – 2015-07-01 14:43:46

2

像這樣的一次或第一次加載的要求很常見。

1)設置一個名爲FIRSTLOAD的標誌變量並將其設置爲True。請務必將此變量保存到設備中的獨立存儲中,以便每次啓動應用程序時都能讀回。 2)創建一個檢查FIRSTLOAD值的方法,只有在FIRSTLOAD爲真時才執行。把你的代碼'添加到SQLite數據庫的初始值'在這裏。然後將FIRSTLOAD變量設置爲false。這樣它只會執行一次代碼!

Boolean FIRSTLOAD= new Boolean("true"); 

void SetInitialValues() 
{ 
    if(!FIRSTLOAD) 
     return; 

    // insert your code to run only when application is started first time here 


    FIRSTLOAD = False; 
} 
2

你應該使用SQLiteOpenHelper這個

private static class OpenHelper extends SQLiteOpenHelper { 

    private static final String DATABASE_NAME = "rss.sqlite"; 
    private static final int DATABASE_VERSION = 1; 

    OpenHelper(Context context) { 
     super(context, DATABASE_NAME, null, DATABASE_VERSION); 
    } 
    @Override 
    public void onCreate(SQLiteDatabase db) { 
     db.execSQL("CREATE TABLE RSS (TITLE TEXT, LINK TEXT, DESCR TEXT, PUBDATE DATE, GUID TEXT, READ TEXT, TYPE TEXT)"); 
     db.execSQL("CREATE TABLE PAGE (LINK TEXT, CONTENT TEXT)"); 
    } 

    @Override 
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) { 

    } 
} 

並在代碼中使用它

OpenHelper openHelper = new OpenHelper(this.context); 
    this.db = openHelper.getWritableDatabase(); 
0

你應該每次在應用程序中首次打開數據庫時,請檢查數據庫中的對象數量。 不要忘記,用戶可以釋放所有數據。

//Open DataBase 
    MyDb.Open(); 

    //Get All List 
    MyCursor = MyDb.getAllNames(); //You should have a function to get all the table data 

    //First Time we open Database, add default Values 
    if (MyCursor.getCount() < 1) 
    { 
     AddDefaultValuesToDataBase(); 
    } 

從這裏您可以繼續獲取值。

希望它有幫助, BR, 阿德里安。

1

如果您使用org.droidparts庫OR-Mapper訪問數據庫,可以這樣做。

在你執行

public class CategoryEntityMgr extends EntityManager<Category> { 
    public CategoryEntityMgr(Context ctx) { 
     super(Category.class, ctx); 
    } 

    // NEW --> this one must be used in onCreateTables or you will receive a database locked exception 
    private CategoryEntityMgr(Context ctx, SQLiteDatabase db) { 
     super(Category.class, ctx, db); 
    } 
} 

在您的實現AbstractDbOpenHelper的:

protected void onCreateTables(SQLiteDatabase db) { 
    // create your tables as usual 
    createTables(db, Booking.class, Category.class); 

    // Use a new instance of your entity manager 
    // but use the 2nd constructor to provide the db connection 
    CategoryEntityMgr mgr = new CategoryEntityMgr(ctx, db); 

    // add the new table row/entity you an to have 
    Category c = new Category(); 
    c.name = "the value for this column"; 
    mgr.createOrUpdate(c); 
} 
+0

如果這是一個通用的答案,而不是綁定到特定的庫實現,那會更好。 – Levon 2017-05-22 20:15:34

相關問題