kb920811
2016-03-17 16:38
采纳率: 33.3%
浏览 2.1k

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

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

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

5条回答 默认 最新

  • Pt950-- 2016-03-17 16:54
    已采纳

    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

    评论
    解决 无用
    打赏 举报
  • 查看更多回答(4条)

相关推荐 更多相似问题