0

Whenever my application gets to the SQLiteDatabase db = getWritableDatabase(); line in the addHistory() method it crashes telling me only this: E/SQLiteLog: (1)

What might be the problem? I tried using this.getWriteableDatabase() but no luck with that too :(

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class HistoryDB extends SQLiteOpenHelper {
    private static final String DATABASE_NAME = "history.db";

    private static final String TABLE_HISTORY = "history";
    private static final String FIELD_TITLE = "title";
    private static final String FIELD_TEMPERATURE = "temperature";
    private static final String FIELD_SUMMARY = "summary";
    private static final String FIELD_WIND = "wind";
    private static final String FIELD_LOCATION_ID = "locationID";

    private static final int DATABASE_VERSION = 1;

   HistoryDB(Context context){
        super(context,DATABASE_NAME,null,DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db){
        db.execSQL("CREATE TABLE " + TABLE_HISTORY +
                "(_idHistory INT PRIMARY KEY, " +
                FIELD_TITLE + " TEXT, " +
                FIELD_TEMPERATURE + " FLOAT(3,2)), " +
                FIELD_SUMMARY + " TEXT, " +
                FIELD_WIND + " TEXT, " +
                FIELD_LOCATION_ID + " INT(3));");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
        //handle database upgrade as needed
    }

    public long addHistory(String title, float temp, String sum, String wind, int locationId){
        SQLiteDatabase db = getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(FIELD_TITLE, title);
        values.put(FIELD_TEMPERATURE, temp);
        values.put(FIELD_SUMMARY, sum);
        values.put(FIELD_WIND, wind);
        values.put(FIELD_LOCATION_ID, locationId);
        return db.insert(TABLE_HISTORY, null, values);
    }
}


01-04 16:17:57.643 11289-11289/? I/art: Late-enabling -Xcheck:jni
01-04 16:17:57.991 11289-11289/com.example.h454951.imtrying W/System: ClassLoader referenced unknown path: /data/app/com.example.h454951.imtrying-1/lib/arm64
01-04 16:17:57.995 11289-11289/com.example.h454951.imtrying I/InstantRun: Instant Run Runtime started. Android package is com.example.h454951.imtrying, real application class is null.
01-04 16:17:58.167 11289-11289/com.example.h454951.imtrying W/System: ClassLoader referenced unknown path: /data/app/com.example.h454951.imtrying-1/lib/arm64
01-04 16:17:58.285 11289-11289/com.example.h454951.imtrying I/HwCust: Constructor found for class android.app.HwCustHwWallpaperManagerImpl
01-04 16:17:58.343 11289-11289/com.example.h454951.imtrying W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
01-04 16:17:58.546 11289-11289/com.example.h454951.imtrying I/HwSecImmHelper: mSecurityInputMethodService is null
01-04 16:17:58.624 11289-11337/com.example.h454951.imtrying I/OpenGLRenderer: Initialized EGL, version 1.4
01-04 16:17:58.743 11289-11289/com.example.h454951.imtrying W/IInputConnectionWrapper: getExtractedText on inactive InputConnection
01-04 16:17:58.743 11289-11289/com.example.h454951.imtrying W/IInputConnectionWrapper: getTextBeforeCursor on inactive InputConnection
01-04 16:17:58.744 11289-11289/com.example.h454951.imtrying W/IInputConnectionWrapper: getSelectedText on inactive InputConnection
01-04 16:17:58.744 11289-11289/com.example.h454951.imtrying W/IInputConnectionWrapper: getTextAfterCursor on inactive InputConnection
01-04 16:18:04.044 11289-11289/com.example.h454951.imtrying E/SQLiteLog: (1) 
01-04 16:18:04.133 11289-11289/com.example.h454951.imtrying I/Process: Sending signal. PID: 11289 SIG: 9

Run log after clean&rebuild

01-04 16:41:50.130 19496-19496/? I/art: Late-enabling -Xcheck:jni
01-04 16:41:50.264 19496-19496/com.example.h454951.imtrying W/System: ClassLoader referenced unknown path: /data/app/com.example.h454951.imtrying-1/lib/arm64
01-04 16:41:50.269 19496-19496/com.example.h454951.imtrying I/InstantRun: Instant Run Runtime started. Android package is com.example.h454951.imtrying, real application class is null.
01-04 16:41:50.416 19496-19496/com.example.h454951.imtrying W/System: ClassLoader referenced unknown path: /data/app/com.example.h454951.imtrying-1/lib/arm64
01-04 16:41:50.562 19496-19496/com.example.h454951.imtrying I/HwCust: Constructor found for class android.app.HwCustHwWallpaperManagerImpl
01-04 16:41:50.632 19496-19496/com.example.h454951.imtrying W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
01-04 16:41:50.867 19496-19496/com.example.h454951.imtrying I/HwSecImmHelper: mSecurityInputMethodService is null
01-04 16:41:50.967 19496-19533/com.example.h454951.imtrying I/OpenGLRenderer: Initialized EGL, version 1.4
01-04 16:41:52.738 19496-19502/com.example.h454951.imtrying W/art: Suspending all threads took: 13.619ms
01-04 16:41:52.917 19496-19496/com.example.h454951.imtrying E/SQLiteLog: (1) 
01-04 16:41:52.947 19496-19496/com.example.h454951.imtrying I/Process: Sending signal. PID: 19496 SIG: 9
Sidelley
  • 11
  • 1

1 Answers1

0

You have extra close paren ) here:

FIELD_TEMPERATURE + " FLOAT(3,2)), " +

Remove it.

Note that sqlite does not care about length/precision modifiers at all so you could as well remove the (3,2)) altogether.

laalto
  • 150,114
  • 66
  • 286
  • 303
  • The problem is still present, but thanks for pointing out this typo and that sqlite doesn't care about the modifiers! – Sidelley Jan 04 '17 at 15:11
  • If there's a crash, there's a stacktrace you should post as text to your question. http://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this – laalto Jan 04 '17 at 15:15