-1

I'm just trying to create a database with a tutorial and it keeps crashing on me and was hoping someone could give me a some direction on what's up.

MainActivity.java

package com.example.dwalker.btv2;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        DatabaseHelper myDB = new DatabaseHelper(this);
    }
}

DatabaseHelper.java

package com.example.dwalker.btv2;

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

/**
 * Created by DWalker on 5/24/2017.
 */

public class DatabaseHelper extends SQLiteOpenHelper {
    public static final int DATABASE_VERSION = 1;
    public static final String DATABASE_NAME = "events.db";
    public static final String TABLE_NAME = "event_table";
    public static final String COL_1 = "ID";
    public static final String COL_2 = "EVENT";
    public static final String COL_3 = "EVENTDATE";
    public static final String COL_4 = "EVENTTIME";

    public DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        SQLiteDatabase db = this.getWritableDatabase();
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE event_table (ID INTEGER PRIMARY KEY AUTOINCREMENT, EVENT TEXT, EVENTDATE TEXT, EVENTTIME TEXT");

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
        onCreate(db);

    }
}

When I run the app on the emulator its just telling me in stopped unexpectedly. All i'm trying to do is create the db file so I can verify its running correctly.

Thanks

DW

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Dallas Walker
  • 27
  • 1
  • 3
  • 2
    Use LogCat to examine the Java stack trace associated with your crash: https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this. Most likely, your problem is in having `SQLiteDatabase db = this.getWritableDatabase();` in the constructor of your `SQLiteOpenHelper` subclass. – CommonsWare May 24 '17 at 16:05
  • first off change the ID field to _ID it will be easier to get the built in stuff to load the data. – danny117 May 24 '17 at 18:03

1 Answers1

0

Your query is failing, you are missing a bracket at the end (I also added a semicolon ; but I am unsure if this is necessary):

db.execSQL("CREATE TABLE event_table (ID INTEGER PRIMARY KEY AUTOINCREMENT, EVENT TEXT, EVENTDATE TEXT, EVENTTIME TEXT);");

This causes the onCreate function to fail, which in turn causes the applciation in general to crash.

Pieter De Clercq
  • 1,951
  • 1
  • 17
  • 29