代码之家  ›  专栏  ›  技术社区  ›  Karim Mtl

android创建sqlite数据库

  •  1
  • Karim Mtl  · 技术社区  · 6 年前

    我无法使用简单的文件来创建sqlite数据库。 如果数据库已创建或升级,但没有显示,我将在日志中记录一些希望在日志中显示的消息。

    代码如下:

    package com.example.karim.myandoidapp;
    
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.util.Log;
    import android.content.Context;
    import android.database.sqlite.SQLiteDatabase;
    import android.database.sqlite.SQLiteOpenHelper;
    
    public class HomeActivity extends AppCompatActivity {
    
        String msg = "My android app log : ";
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            Log.e(msg, "onCreate() event loading activity_home view");
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_home);
            Log.e(msg, "onCreate() event loading activity_home view");
            Context context = myandoidapp.getAppContext();
            MySQLiteHelper MySQLiteHelper = new MySQLiteHelper(context);
        }
    
        private class MySQLiteHelper extends SQLiteOpenHelper {
    
            // Database Version
            private static final int DATABASE_VERSION = 1;
            // Database Name
            private static final String DATABASE_NAME = "mydbnamesss";
    
            public MySQLiteHelper(Context context) {
                super(context, DATABASE_NAME, null, DATABASE_VERSION);
            }
    
            @Override
            public void onCreate(SQLiteDatabase db) {
                Log.e("DEBUG: ", "db created at " + db.getPath());
            }
    
            @Override
            public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
                Log.e("DEBUG: ", "db upgraded ");
            }
    
        }
    
    }
    

    以下是我日志中的输出:

    06-14 15:02:29.122 6644-6644/com.example.karim.myandoidapp E/My android app log :: Opening HomeActivity
    
        --------- beginning of system
    06-14 15:02:29.141 6644-6644/com.example.karim.myandoidapp E/My android app log :: onCreate() event loading activity_home view
    06-14 15:02:29.203 6644-6673/com.example.karim.myandoidapp D/EGL_emulation: eglMakeCurrent: 0xa22050c0: ver 3 0 (tinfo 0xa2203360)
    06-14 15:02:29.209 6644-6673/com.example.karim.myandoidapp D/EGL_emulation: eglMakeCurrent: 0xa22050c0: ver 3 0 (tinfo 0xa2203360)
    06-14 15:02:29.216 6644-6673/com.example.karim.myandoidapp D/EGL_emulation: eglMakeCurrent: 0xa22050c0: ver 3 0 (tinfo 0xa2203360)
    06-14 15:02:29.218 6644-6673/com.example.karim.myandoidapp D/OpenGLRenderer: endAllActiveAnimators on 0x8eeae180 (RippleDrawable) with handle 0xa2203ac0
    

    我在这里补充一下,如果上下文相关,我将如何获取上下文:

    包com.example.karim.myandoidapp;

    导入android.content.context; 导入android.app.application;

    public class myandoidapp extends Application {
    
        private static Context context;
    
        public void onCreate() {
            super.onCreate();
            myandoidapp.context = getApplicationContext();
        }
    
        public static Context getAppContext() {
            return myandoidapp.context;
        }
    
    }
    

    谢谢

    2 回复  |  直到 6 年前
        1
  •  2
  •   MikeT    6 年前

    onCreate

        public MySQLiteHelper(Context context) {
                super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }
    

        public MySQLiteHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
            this.getWritableDatabase(); //<<<< this.getReadableDatabase could also be used.  
        }
    

        public MySQLiteHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
            SQLiteDatabase mydb = this.getWritableDatabase();
            mydb.close();
        }
    

    然后在实例化数据库助手时,使用 MySQLiteHelper MySQLiteHelper = new MySQLiteHelper(context);

    注意, 方法在数据库的生存期内只调用一次,因此您将只在第一次打开数据库时获取日志消息。

    onUpgrade

        2
  •  1
  •   mtrewartha    6 年前

    您的代码定义了 SQLiteOpenHelper 然后实例化它,但这不足以创建/打开一个sqlite数据库。如果我是你,我会在谷歌的新图书馆里读到 Room 存取资料表