1 Replies - 1554 Views - Last Post: 19 July 2021 - 06:11 AM

#1 Pogaca   User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 198
  • Joined: 24-August 13

Database there is no table

Posted 17 May 2021 - 06:45 AM

So I have class DBHelper
public class DBHelper extends SQLiteOpenHelper {

    public DBHelper(Context context) {
        super(context, "Event.db", null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE Table Event(eventName TEXT primary key, section TEXT, startDate TEXT, totalEXP TEXT)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("drop Table if exists Event");
    }

    // Insert data into database
    public Boolean insertData(String eventName, String section, String startDate, String totalEXP) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put("eventName", eventName);
        contentValues.put("section", section);
        contentValues.put("startDate", startDate);
        contentValues.put("totalEXP", totalEXP);

        long result = db.insert("Event", null, contentValues);
        if (result == -1) {
            return false;
        } else {
            return true;
        }
    }
}



In there you can see the name of the table is "Event" . When I try to put values in other activity into database, it says no table with that name:
addEvent.setonclickListener(new View.onclickListener() {
            @Override
            public void onclick(View v) {

                // Get values from EditText
                String value = eventName.getText().toString();
                String value2 = eventDurationNumber.getText().toString();
                String flagStr = String.valueOf(flag);

                Boolean checkInsertData = db.insertData(value,flagStr,curDate,value2);

                if(checkInsertData == true) {
                    Toast.makeText(getApplicationContext(),"Event added",Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(getApplicationContext(),"Event is not added",Toast.LENGTH_SHORT).show();
                }

            }
        });

This is button that activates inserDate method. I get this error:

2021-05-17 15:20:15.017 5964-5964/com.example.productivitytracker E/SQLiteLog: (1) no such table: Event
2021-05-17 15:20:15.023 5964-5964/com.example.productivitytracker E/SQLiteDatabase: Error inserting eventName=test section=1 startDate=6 totalEXP=25
    android.database.sqlite.SQLiteException: no such table: Event (code 1): , while compiling: INSERT INTO Event(eventName,section,startDate,totalEXP) VALUES (?,?,?,?)
        at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
        at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
        at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
        at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
        at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
        at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
        at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1472)
        at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1343)
        at com.example.productivitytracker.DBHelper.insertData(DBHelper.java:35)
        at com.example.productivitytracker.EventData$2.onclick(EventData.java:65)
        at android.view.View.performClick(View.java:6259)
        at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1119)
        at android.view.View$PerformClick.run(View.java:24732)
        at android.os.Handler.handleCallback(Handler.java:789)
        at android.os.Handler.dispatchMessage(Handler.java:98)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6694)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:769)



How to fix it?

Is This A Good Question/Topic? 0
  • +

Replies To: Database there is no table

#2 wseng92   User is offline

  • D.I.C Addict
  • member icon

Reputation: 60
  • View blog
  • Posts: 785
  • Joined: 23-September 15

Re: Database there is no table

Posted 19 July 2021 - 06:11 AM

Uninstall and re-install back.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1