如何将初始数据添加到SQLite数据库?

问题描述:

我需要将初始值添加到SQLite数据库,一旦应用程序第一次启动。我应该怎么做?如何将初始数据添加到SQLite数据库?

在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

像这样的一次或第一次加载的要求很常见。

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; 
} 

您可以创建自己的SQLite数据库,把资产目录和访问它,如在这个环节的答案: adding your own SQLite database to an android application

你应该使用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(); 

你应该每次在应用程序中首次打开数据库时,请检查数据库中的对象数量。 不要忘记,用户可以释放所有数据。

//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, 阿德里安。

如果您使用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