Android应用程序在getWritableDatabase()时崩溃;

时间:2017-01-04 14:52:11

标签: java android database sqlite

每当我的应用程序到达SQLiteDatabase db = getWritableDatabase();方法中的addHistory()行时,它崩溃只告诉我: E / SQLiteLog:(1)

可能是什么问题?我尝试使用this.getWriteableDatabase(),但也没有运气:(

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

清理和重建后运行日志

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

1 个答案:

答案 0 :(得分:0)

您在这里有额外的关闭)

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

删除它。

请注意,sqlite根本不关心长度/精度修饰符,因此您也可以完全删除(3,2))

相关问题