[Android Studio] Description that can be continuously input in SQLite Database [Java]


I'm new to Android. I can now register the value of EditText in the database while looking at the text or web page, but when I try to enter it continuously, it fails. ** You can only register data once each time you open the app! ** ** I thought this was fatal as an app and investigated what to do. I think there are two factors. ➀ try-catch-finally statement not written ➁ Wrong location to open database I decided to write this article as a reference for those who are suffering from the same situation.

➀ About try-catch-finally sentence not written

As you can see from the code after this, my code doesn't have the necessary try-catch, it's very unstable because it didn't have a catch statement in the first place. I understand the importance of try-catch, but it's difficult. .. I thought I still needed to study. I think it is better to use "** try-with-resources **" described later rather than thinking about complicated try-catch.

➁ About the wrong place to open the database

Falling code

Click here for the full code (jump to another article I wrote) If you register only once, it will not fall. If you try to register twice, you will get the exception "java.lang.illegalStateException attempt to re-open an already-closed object". That's because the falling code opens the database with the onCreate method. The onCreate method is the first method to be executed when the app is started. I open the database here, do some processing and then release the database. Even if I try to operate it again, the app crashes because it was closed by onCreate.


public void insertData(SQLiteDatabase db, String maxBP, String minBP, String pulse){
        ContentValues values = new ContentValues();
        try {
            values.put("_maxBP", maxBP);
            values.put("_minBP", minBP);
            values.put("_pulse", pulse);
            db.insert("_BPtable", null, values);
        } finally {

try-with-resources I think the advantage of try-with-resources is that you don't have to write complicated try-catch statements. ~~ I don't understand try-catch, which is a big help ~~ However, I think that there are many programs that only try-catch-finally are implemented, so I will do my best to study. .. Click here for try-with-resources syntax


try (Declaration of variables that need to be cleaned up by close) {
Original processing
} catch (Exception class variable name) {
What to do if an exception occurs

Here is a rewrite of the previous code using try-with-resources In addition, I reviewed the code to open the database with methods. ➁ It is a solution.


    public void insertData(String maxBP, String minBP, String pulse){
        ContentValues values = new ContentValues();
        DatabaseHelper helper = new DatabaseHelper(MainActivity.this);
        try (SQLiteDatabase data = helper.getWritableDatabase()){
            values.put("_maxBP", maxBP);
            values.put("_minBP", minBP);
            values.put("_pulse", pulse);
            data.insert("_BPtable", null, values);
        } catch (Exception e){

This will automatically release the database after the try is finished. We have created a program that will not drop continuous registration!


WINGS Project by Shinzo Saito / Supervised by Yoshihiro Yamada "Textbook for Android Application Development" Written by Kiyotaka Nakayama / Written by Daigo Kunimoto / Supervised by Flarelink Co., Ltd. "Introduction to Java for a refreshing 3rd edition"

Recommended Posts

[Android Studio] Description that can be continuously input in SQLite Database [Java]
Write a class that can be ordered in Java
Firebase-Realtime Database on Android that can be used with copy
Java (super beginner edition) that can be understood in 180 seconds
Reference memo / In-memory LDAP server that can be embedded in Java
Summary of ORM "uroboroSQL" that can be used in enterprise Java
Java to C and C to Java in Android Studio
Java file input / output processing that can be used through historical background
[Java 8] Until converting standard input that can be used in coding tests into a list or array
Technology excerpt that can be used for creating EC sites in Java training
[Android / Java] Operate a local database in Room
Basic functional interface that can be understood in 3 minutes
Convenient shortcut keys that can be used in Eclipse
Notes in Android studio
Syntax and exception occurrence conditions that can be used when comparing with null in Java
[Java 8] Sorting method in alphabetical order and string length order that can be used in coding tests
List of devices that can be previewed in Swift UI
Create a jar file that can be executed in Gradle
Refer to C ++ in the Android Studio module (Java / kotlin)
Problems that can easily be mistaken for Java and JavaScript
Introduction to Rakefile that can be done in about 10 minutes
You are currently using Java 6. Solution in Android Studio Gradle
How to make a key pair of ecdsa in a format that can be read by Java
Read standard input in Java
Java in Visual Studio Code
Static analysis tool that can be used on GitHub [Java version]
The problem that XML attributes are sorted arbitrarily in Android Studio
Note that system properties including JAXBContext cannot be used in Java11
Source to display character array with numberPicker in Android studio (Java)
SwiftUI View that can be used in combination with other frameworks
It seems that data class-like functions will be added in Java14
[Android / Java] Understand the description type in listener settings (button installation)
Introduction to Java that can be understood even with Krillin (Part 1)
Determining if a custom keyboard is enabled in Android Studio (Java)
An active hash that can be treated as data even if it is not in the database