2014-08-29 88 views
-3

請幫助。我被卡住了。有人會告訴我爲什麼我在JUnit測試中有AssertionFailedError。我不知道爲什麼SQLiteDatabase數據庫插入方法返回-1。SQLite數據庫插入返回-1

錯誤指向第50行:assertTrue(locationRowId!= -1);

public void testInsertReadDb() { 
    // Test data we're going to insert into the DB to see if it works. 
    String testLocationSetting = "99705"; 
    String testCityName = "North Pole"; 
    double testLatitude = 64.7488; 
    double testLongitude = -147.353; 

    // If there's an error in those massive SQL table creation Strings, 
    // errors will be thrown here when you try to get a writable database. 
    WeatherDbHelper dbHelper = new WeatherDbHelper(mContext); 
    SQLiteDatabase db = dbHelper.getWritableDatabase(); 

    // Create a new map of values, where column names are the keys 
    ContentValues values = new ContentValues(); 
    values.put(LocationEntry.COLUMN_LOCATION_SETTING, testLocationSetting); 
    values.put(LocationEntry.COLUMN_CITY_NAME, testCityName); 
    values.put(LocationEntry.COLUMN_COORD_LAT, testLatitude); 
    values.put(LocationEntry.COLUMN_COORD_LONG, testLongitude); 

    //long locationRowId; 
    long locationRowId = db.insert(LocationEntry.TABLE_NAME, null, values); 

    // Verify we got a row back. 
    assertTrue(locationRowId != -1); 
    Log.d(LOG_TAG, "New row id: " + locationRowId); 

以下是錯誤:

Running tests 
Test running started 
junit.framework.AssertionFailedError 
at com.example.krisemmanuel.sunshine.test.TestDb.testInsertReadDb(TestDb.java:50) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:191) 
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:176) 
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:554) 
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1729) 

Finish 

以下是錯誤的屏幕: http://picpaste.com/debughelpplease-IuEAYaNu.png

和Android Studio項目文件夾: http://krisg.net/java/Sunshine.rar

我不知道爲什麼SQLiteDatabase數據庫的insert()方法返回-1。我試圖測試插入到SQLite的工作原理,但它在測試中失敗。

+1

你問題不清楚。你應該描述細節。如果你的問題不清楚,你不能得到任何建議。 – 2014-08-29 10:47:44

+0

你好B男,我修改了我的問題,使其更清晰。抱歉。我感謝您的幫助。 – Krisg 2014-08-29 11:35:21

回答

1

SQLiteDatabase.insert()失敗時,它只記錄錯誤消息並返回-1。

要獲得例外,請改爲使用insertOrThrow

+0

你先生剛剛上油齒輪:)非常感謝你。我終於找到了bug!只是在SQL表創建中缺少空間。再次感謝你 – Krisg 2014-08-29 12:55:44