kb920811 2016-03-17 16:38 采纳率: 33.3%
浏览 2126
已采纳

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条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog