Forntoh Thomas Forntoh Thomas - 6 months ago 10
Android Question

Problems when INSERT into DB

When I try to submit data into the database, the app crashes.

Here are the parts of the java file concerned with creating and inserting data into the database.

My

Contract.java
class has the following information:

public class Contract {
public static abstract class customReminder{
public static final String TABLE_NAME = "CUSTOM_REMINDER";
public static final String ID = "ID";
public static final String TITLE = "TITLE";
public static final String DESCRIPTION = "DESCRIPTION";
public static final String DATE_TIME = "DATE_TIME";

public static final String[] REMINDER_COLUMNS = {ID, TITLE, DESCRIPTION, DATE_TIME};

}


My
DBHelper.java
class:

//SQLite statement for custom reminder table
public static final String CUSTOM_REMINDER_TABLE = "CREATE TABLE " + Contract.customReminder.TABLE_NAME + "("
+ Contract.customReminder.ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ Contract.customReminder.TITLE + " TEXT,"
+ Contract.customReminder.DESCRIPTION + " TEXT,"
+ Contract.customReminder.DATE_TIME + " TEXT,"
+ ");";

@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CUSTOM_REMINDER_TABLE);
}


My
CustumRemider.java
class:

public boolean createCustomReminder(String title, String description, String dateTime){
ContentValues contentValues = new ContentValues();
contentValues.put(mAllColumns[1], title);
contentValues.put(mAllColumns[2], description);
contentValues.put(mAllColumns[3], dateTime);
long result = mDatabase.insert(Contract.customReminder.TABLE_NAME, null, contentValues);
return result != -1;
}


I downloaded the database to my PC from Android Device Monitor and opened it with SQLiteBrowser. I notice that a table
android_metadata
is created instead of
CUSTOM_REMINDER
.

CREATE TABLE android_metadata (locale TEXT)


I neither know where the query comes from nor why it does this.

Answer

You need to change delete comma (",") after last parameter in create table query

public static final String CUSTOM_REMINDER_TABLE = "CREATE TABLE " + Contract.customReminder.TABLE_NAME + "("
    + Contract.customReminder.ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
    + Contract.customReminder.TITLE + " TEXT,"
    + Contract.customReminder.DESCRIPTION + " TEXT,"
    + Contract.customReminder.DATE_TIME + " TEXT"
    + ");";

instead of this

public static final String CUSTOM_REMINDER_TABLE = "CREATE TABLE " + Contract.customReminder.TABLE_NAME + "("
    + Contract.customReminder.ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
    + Contract.customReminder.TITLE + " TEXT,"
    + Contract.customReminder.DESCRIPTION + " TEXT,"
    + Contract.customReminder.DATE_TIME + " TEXT,"
    + ");";