Android SQLite数据库存储,怎样创建一个的数据库?

今天看《第一行代码--Android》,有一个地方没看懂,创建一个名为bookstore.db的数据库,
这个数据库在哪里创建,也就是创建数据库的代码在哪里写?

SQLiteOpenHelper类的构造方法中传入db名称自然就会创建数据库了,然后在SQLiteOpenHelper.onCreate()方法里创建表。

 // A string that defines the SQL statement for creating a table
private static final String SQL_CREATE_MAIN = "CREATE TABLE " +
    "main " +                       // Table's name
    "(" +                           // The columns in the table
    " _ID INTEGER PRIMARY KEY, " +
    " WORD TEXT"
    " FREQUENCY INTEGER " +
    " LOCALE TEXT )";
...
/**
 * Helper class that actually creates and manages the provider's underlying data repository.
 */
protected static final class MainDatabaseHelper extends SQLiteOpenHelper {

    /*
     * Instantiates an open helper for the provider's SQLite data repository
     * Do not do database creation and upgrade here.
     */
    MainDatabaseHelper(Context context) {
        super(context, DBNAME, null, 1);
    }

    /*
     * Creates the data repository. This is called when the provider attempts to open the
     * repository and SQLite reports that it doesn't exist.
     */
    public void onCreate(SQLiteDatabase db) {

        // Creates the main table
        db.execSQL(SQL_CREATE_MAIN);
    }
}

更详细的可以查看google developer官方说明:http://developer.android.com/guide/topics/providers/content-provider-creating.html

下载Navicat for SQLite这个软件,用它创建
http://jingyan.baidu.com/article/636f38bb2b299dd6b8461000.html
把创建好的复制到程序目录中。

创建bookstore.db这个数据库。。新建一个类来创建的。该类得继承SQLiteOpenHelper。简单如下:

class MyDBOpenHelper extends SQLiteOpenHelper{

/**
 * 参数:
 *1- context:上下文
 *2- "bookstore.db":你要创建的数据库名称
 *3-指的是游标工厂:为null
 *4-表明当前的版本为1,下次你需要更改数据库的时候,这里的的版本需要更改(值大于1即可)。
 * @param context
 */
public MyDBOpenHelper(Context context) {
    super(context, "bookstore.db", null, 1);
}

/**
 * onCreate方法
 * ————第一次创建数据库的时候用的。
 */
@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("create table ...");//sql:写入创建表的语句

}
/**
 * onUpgrade方法
 * ————数据库版本更新的时候或者更新表结构的时候调用。
 */
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("sql语句");//重新创建表的语句
}
---------------------------------------------------------------------------------------------------------
大概就是这样,你可以查找相关资料自己理解一下。这里只是我个人简单的理解。

Pt950-- 给的答案中,这一句就是。

MainDatabaseHelper(Context context) {
super(context, DBNAME, null, 1);
}

SQLiteOpenHelper类的构造方法中传入db名称自然就会创建数据库了,然后在SQLiteOpenHelper.onCreate()方法里创建表。

// A string that defines the SQL statement for creating a table
private static final String SQL_CREATE_MAIN = "CREATE TABLE " +
"main " + // Table's name
"(" + // The columns in the table
" _ID INTEGER PRIMARY KEY, " +
" WORD TEXT"
" FREQUENCY INTEGER " +
" LOCALE TEXT )";
...
/**

  • Helper class that actually creates and manages the provider's underlying data repository.
    */
    protected static final class MainDatabaseHelper extends SQLiteOpenHelper {

    /*

    • Instantiates an open helper for the provider's SQLite data repository
    • Do not do database creation and upgrade here. */ MainDatabaseHelper(Context context) { super(context, DBNAME, null, 1); }

    /*

    • Creates the data repository. This is called when the provider attempts to open the
    • repository and SQLite reports that it doesn't exist.
      */
      public void onCreate(SQLiteDatabase db) {

      // Creates the main table
      db.execSQL(SQL_CREATE_MAIN);
      }
      }