[JAVA] Beachten Sie, dass ich SQLiteOpenHelper unter Android leicht berührt habe

Konstrukteur

java In Java rufe super im Konstruktor auf super(context, DATABASE_NAME, null, DATABASE_VERSION); kotlin Da sich der Name von super von Java unterscheidet, müssen Sie vorsichtig sein, wie Sie ihn schreiben. Beschreiben Sie wie folgt class DatabaseHelper(context : Context) : SQLiteOpenHelper(context, DATABASE_NAME, null, DATABASE_VERSION)

Hinweis

Wenn der Kontext beim Aufrufen des Konstruktors null ist, tritt eine NullPointerException auf.

Gerätetest

Achten Sie bei der Durchführung von Unit-Tests mit Mockito auf den Kontext. RuntimeEnvironment.application Zum DatabaseHelper-Konstruktor: Sie können innerhalb des Komponententests auf die Datenbank zugreifen, diese schreiben und lesen.

sample


    @Mock
    private DatabaseHelper mDatabaseHelper;
    private Context mContext;

    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);
        mContext = RuntimeEnvironment.application;
        mDatabaseHelper = spy(new DatabaseHelper(mContext));
    }

Beispielcode

sample


    static class DatabaseHelper extends SQLiteOpenHelper {
        private static final String DATABASE_NAME = "Test.db";
        private static final int DATABASE_VERSION = 1;
        private static final String TABLE_NAME = "tablename";
        private static final String COLUMN_KEY = "name";

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

        private void createTable(SQLiteDatabase db) {
            db.execSQL("CREATE TABLE " + TABLE_NAME + " ("
                    + COLUMN_KEY + " TEXT NOT NULL);");
        }

        /**
         * This call needs to be made while the mCacheLock is held. The way to
         * ensure this is to get the lock any time a method is called ont the DatabaseHelper
         * @param db The database.
         */
        @Override
        public void onCreate(SQLiteDatabase db) {
            createTable(db);
        }

        @Override
        public void onOpen(SQLiteDatabase db) {
        }

        @Override
        public void onConfigure(SQLiteDatabase db) {
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int currentVersion) {
        }

        @Override
        public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        }
    }

MainActivity.kt


/*
 * Copyright (C) 2018 Sacred Sanctuary Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package jp.sacredsanctuary.databasesample

import android.content.ContentValues
import android.content.Context
import android.database.DatabaseUtils
import android.database.sqlite.SQLiteDatabase
import android.database.sqlite.SQLiteException
import android.database.sqlite.SQLiteOpenHelper
import android.support.v7.app.AppCompatActivity
import android.os.Bundle

class MainActivity : AppCompatActivity() {
    companion object {
        const val DATABASE_NAME = "Test.db"
        const val DATABASE_VERSION = 1
        const val TABLE_NAME = "tablename"
        const val COLUMN_KEY = "name"
        const val QUERY = "SELECT * FROM " + COLUMN_KEY + " limit 1"
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }

    override fun onResume() {
        super.onResume()
        try {
            val db = DatabaseHelper(this).writableDatabase
            if (null != db) {
                try {
                    val values = ContentValues()
                    values.put(COLUMN_KEY, "TEST")
                    db.delete(TABLE_NAME, null, null)
                    db.insert(TABLE_NAME, null, values)
                } catch (e: SQLiteException) {
                    //Tritt auf, wenn nicht in die Datenbank geschrieben werden kann
                } finally {
                    db.close()
                }
            }
        } catch (e: SQLiteException) {
            //Tritt auf, wenn die Datenbank nicht geöffnet werden kann
        } catch (e: NullPointerException) {
            //Tritt im SQLiteOpenHelper-Konstruktor auf, wenn Context null ist
        }
    }

    override fun onPause() {
        super.onPause()
        try {
            val db = DatabaseHelper(this).readableDatabase
            try {
                val name = DatabaseUtils.stringForQuery(db, QUERY, null)
            } catch (e: SQLiteException) {
                //Tritt auf, wenn die Abfrageanweisung ungültig ist
            } finally {
                db.close()
            }
        } catch (e: SQLiteException) {
            //Tritt auf, wenn die Datenbank nicht geöffnet werden kann
        } catch (e: NullPointerException) {
            //Tritt im SQLiteOpenHelper-Konstruktor auf, wenn Context null ist
        }
    }

    class DatabaseHelper(context : Context) : SQLiteOpenHelper(context, DATABASE_NAME, null, DATABASE_VERSION) {
        fun createTable(db: SQLiteDatabase?) {
            db?.execSQL("CREATE TABLE " + TABLE_NAME + " ("
                    + COLUMN_KEY + " TEXT NOT NULL);")
        }

        /**
         * This call needs to be made while the mCacheLock is held. The way to
         * ensure this is to get the lock any time a method is called ont the DatabaseHelper
         * @param db The database.
         */
        override fun onCreate(db: SQLiteDatabase?) {
            createTable(db)
        }

        override fun onOpen(db: SQLiteDatabase) {
        }

        override fun onConfigure(db: SQLiteDatabase) {
        }

        override fun onUpgrade(db: SQLiteDatabase?, oldVersion: Int, newVersion: Int) {
        }
    }
}

Recommended Posts

Beachten Sie, dass ich SQLiteOpenHelper unter Android leicht berührt habe
Ich habe Scala berührt
Beachten Sie, dass ich beim Erstellen der Rails-Umgebung gestolpert bin
Ich habe zuerst Java touched berührt
Ich habe zuerst Java touched berührt
Ich habe zuerst Java touched berührt
Ich habe zuerst Java berührt
Ich habe Scala ~ [Klasse] ~ berührt
Ich habe Scala ~ [Objekt] ~ berührt
Ich habe Scala ~ [Trate] ~ berührt
Ich berührte den Gerätesteuerer, den ich in der Blackbox fühlte