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 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办