0

I am new in android development so the code is not completed yet....

When I created my DB and insert some items inside with using contentValues in the onCreate() method but after the app is running, I tried to read my DB but it was empty. That is the first time to launch my app so it will use the onCreate() method but unfortunately not inserting.

That is my code here...

package com.example.axel.spotopia;

    import android.content.ContentValues;
    import android.content.Context;
    import android.database.sqlite.SQLiteDatabase;
    import android.database.sqlite.SQLiteOpenHelper;

    import java.security.PublicKey;



    public class SpotopiaDBHelper extends SQLiteOpenHelper {
        private static final String DB_NAME = "spotopia";
        private static final int DB_VESION = 1;

        public SpotopiaDBHelper(Context context) {
            super(context, DB_NAME, null, DB_VESION);
        }

        @Override
        public void onCreate(SQLiteDatabase db)
        {
            updateMyDB(db, 0, DB_VESION);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
        {
            updateMyDB(db, oldVersion, newVersion);
        }

        private void insertSpot(SQLiteDatabase db, String name, String address, String note, String rank) {
            ContentValues content = new ContentValues();
            content.put("NAME", name);
            content.put("ADDRESS", address);
            content.put("NOTE", note);
            content.put("RANK", rank);
            db.insert(DB_NAME, null, content);
        }

        private void updateMyDB(SQLiteDatabase db, int oldVersion, int newVersion)
        {
            if (oldVersion < 1) {
                db.execSQL("CREATE TABLE SPOT  ("
                        + "_id INTEGER PRIMARY KEY AUTOINCREMENT, "
                        + "NAME TEXT, "
                        + "ADDRESS TEXT, "
                        + "NOTE TEXT, "
                        + "RANK REAL);");
                insertSpot(db, "jaz", null, null, "8");
                insertSpot(db, "jaz1", null, null, "9");
                insertSpot(db, "jaz2", null, null, "10");
            }

        }
    }

1 Answer 1

1

Here:

db.insert(DB_NAME, null, content);

First parameter should be Table name... and not, Database name. So, could you please test with code below?

private void insertSpot(SQLiteDatabase db, String name, String address, String note, String rank) {
    ContentValues content = new ContentValues();
    content.put("NAME", name);
    content.put("ADDRESS", address);
    content.put("NOTE", note);
    content.put("RANK", rank);
    db.insert("SPOT", null, content);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.