2012-08-29 55 views
0

我是android的noob。我在使用SQLite表時遇到非法參數異常。 logcat表示這是由於我的光標沒有關閉,所以我關閉了所有的遊標。現在我仍然得到IllegalArgumentException沒有詳細消息,我的logcat不顯示任何錯誤消息只是調試和警告消息。沒有任何錯誤信息指向正確的方向我不知道如何開始解決這個問題。任何幫助是極大的讚賞。如何解決IllegalArgumentException?

這是我與光標問題預先固定的代碼:

public static final String KEY_ROWID = "_id"; 
public static final String KEY_NAME = "cointype_name"; 
public static final String KEY_QUANTITY = "cointype_quantity"; 
public static final String KEY_OUNCES = "cointype_ounces"; 
public static final String KEY_VALUE = "cointype_value"; 

private static final String DATABASE_NAME = "PortfolioDatabase"; 
private static final String DATABASE_TABLE = "cointypeTable"; 
private static final int DATABASE_VERSION = 1; 

private DbHelper ourHelper; 
private final Context ourContext; 
private SQLiteDatabase ourDatabase; 

private static class DbHelper extends SQLiteOpenHelper{ 

    public DbHelper(Context context) { 
     super(context, DATABASE_NAME, null, DATABASE_VERSION); 
     // TODO Auto-generated constructor stub 
    } 

    @Override 
    public void onCreate(SQLiteDatabase db) { 
     // TODO Auto-generated method stub 
     db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" + 
       KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + 
       KEY_NAME + " TEXT NOT NULL, " + 
       KEY_QUANTITY + " TEXT NOT NULL, " + 
       KEY_OUNCES + " TEXT NOT NULL, " + 
       KEY_VALUE + " TEXT NOT NULL);" 
     ); 
    } 

    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     // TODO Auto-generated method stub 
     db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE); 
     onCreate(db); 
    }  
} 

public PortfolioDatabase(Context c){ 
    ourContext = c; 
} 

public PortfolioDatabase open() throws SQLException{ 
    ourHelper = new DbHelper(ourContext); 
    ourDatabase = ourHelper.getWritableDatabase();  
    return this; 
} 
public void close(){ 
    ourHelper.close(); 
} 

public long createEntry(String coin, String quantity, String ounces, String value) { 
    // TODO Auto-generated method stub 
    ContentValues cv = new ContentValues(); 
    cv.put(KEY_NAME, coin); 
    cv.put(KEY_QUANTITY, quantity); 
    cv.put(KEY_OUNCES, ounces); 
    cv.put(KEY_VALUE, value); 
    return ourDatabase.insert(DATABASE_TABLE, null, cv); 
} 

public String getData() { 
    // TODO Auto-generated method stub 
    String[] columns = new String[]{ KEY_ROWID, KEY_NAME, KEY_QUANTITY, KEY_OUNCES, KEY_VALUE };   
    Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null); 
    String result = ""; 

    int iRow = c.getColumnIndex(KEY_ROWID); 
    int iName = c.getColumnIndex(KEY_NAME); 
    int iQuantity = c.getColumnIndex(KEY_QUANTITY); 
    int iOunces = c.getColumnIndex(KEY_OUNCES); 
    int iValue = c.getColumnIndex(KEY_VALUE); 

    for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){ 
     result = result + /*c.getString(iRow) + " " +*/ c.getString(iName) + " " + c.getString(iQuantity) + " " + c.getString(iOunces) + " " + c.getString(iValue) + "\n"; 

    } 
    c.close(); 
    return result; 

} 

public String getCoin() { 
    // TODO Auto-generated method stub 
    String[] columns = new String[]{ KEY_ROWID, KEY_NAME, KEY_QUANTITY, KEY_OUNCES, KEY_VALUE }; 
    Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null); 
    String result = ""; 

    int iRow = c.getColumnIndex(KEY_ROWID); 
    int iName = c.getColumnIndex(KEY_NAME); 
    int iQuantity = c.getColumnIndex(KEY_QUANTITY); 
    int iOunces = c.getColumnIndex(KEY_OUNCES); 
    int iValue = c.getColumnIndex(KEY_VALUE); 

    for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){ 
     result = result + /*c.getString(iRow) + " " +*/ c.getString(iName).substring(0, Math.min(18, c.getString(iName).length())) + "\n"; 
    } 
    c.close(); 
    return result; 
} 

    public String getQty() { 
     // TODO Auto-generated method stub 
     String[] columns = new String[]{ KEY_ROWID, KEY_NAME, KEY_QUANTITY, KEY_OUNCES, KEY_VALUE }; 
     Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null); 
     String result = ""; 

     int iRow = c.getColumnIndex(KEY_ROWID); 
     int iName = c.getColumnIndex(KEY_NAME); 
     int iQuantity = c.getColumnIndex(KEY_QUANTITY); 
     int iOunces = c.getColumnIndex(KEY_OUNCES); 
     int iValue = c.getColumnIndex(KEY_VALUE); 

     for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){ 
      result = result + /*c.getString(iRow) + " " +*/ c.getString(iQuantity) + "\n"; 
     }   
    c.close(); 
    return result; 
} 

    public String getOunces() { 
     // TODO Auto-generated method stub 
     String[] columns = new String[]{ KEY_ROWID, KEY_NAME, KEY_QUANTITY, KEY_OUNCES, KEY_VALUE }; 
     Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null); 
     String result = ""; 

     int iRow = c.getColumnIndex(KEY_ROWID); 
     int iName = c.getColumnIndex(KEY_NAME); 
     int iQuantity = c.getColumnIndex(KEY_QUANTITY); 
     int iOunces = c.getColumnIndex(KEY_OUNCES); 
     int iValue = c.getColumnIndex(KEY_VALUE); 

     for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){ 
      result = result + /*c.getString(iRow) + " " +*/ c.getString(iOunces) + "\n"; 
     } 
     c.close(); 
     return result; 
    } 

    public String getValue() { 
     // TODO Auto-generated method stub 
     String[] columns = new String[]{ KEY_ROWID, KEY_NAME, KEY_QUANTITY, KEY_OUNCES, KEY_VALUE }; 
     Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null); 
     String result = ""; 

     int iRow = c.getColumnIndex(KEY_ROWID); 
     int iName = c.getColumnIndex(KEY_NAME); 
     int iQuantity = c.getColumnIndex(KEY_QUANTITY); 
     int iOunces = c.getColumnIndex(KEY_OUNCES); 
     int iValue = c.getColumnIndex(KEY_VALUE); 

     for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){ 
      result = result + /*c.getString(iRow) + " " +*/ c.getString(iValue) + "\n"; 
     }   
     c.close(); 
    return result; 
} 



public String getSingleValue(String aCoin) throws SQLException{ 
    // TODO Auto-generated method stub 
    String[] columns = new String[]{ KEY_ROWID, KEY_NAME, KEY_QUANTITY, KEY_OUNCES, KEY_VALUE};  
    Cursor c = ourDatabase.query(DATABASE_TABLE, columns, KEY_NAME + "=" + aCoin, null, null, null, null); 
    String result = ""; 

    int iRow = c.getColumnIndex(KEY_ROWID); 
    int iName = c.getColumnIndex(KEY_NAME); 
    int iQuantity = c.getColumnIndex(KEY_QUANTITY); 
    int iOunces = c.getColumnIndex(KEY_OUNCES); 
    int iValue = c.getColumnIndex(KEY_VALUE); 

    if (c != null){ 
     c.moveToFirst(); 
     result= c.getString(0); 
     return result; 
    } 
    c.close(); 
    return null; 
} 

/*public String getHotness(long l) throws SQLException{ 
    // TODO Auto-generated method stub 
    String[] columns = new String[]{ KEY_ROWID, KEY_NAME, KEY_QUANTITY, KEY_VALUE }; 
    Cursor c = ourDatabase.query(DATABASE_TABLE, columns, KEY_ROWID + "=" + l, null, null, null, null); 
    if (c != null){ 
     c.moveToFirst(); 
     String hotness = c.getString(2); 
     return hotness; 
    } 
    c.close(); 
    return null; 
}*/ 



public void updateEntry(String mCoin, String mQuantity, String mOunces, String mValue) throws SQLException{ 
    // TODO Auto-generated method stub 
    String[] whereArgs = new String[] { mCoin }; 
    ContentValues cvUpdate = new ContentValues(); 
    cvUpdate.put(KEY_NAME, mCoin); 
    cvUpdate.put(KEY_QUANTITY, mQuantity); 
    cvUpdate.put(KEY_OUNCES, mOunces); 
    cvUpdate.put(KEY_VALUE, mValue); 
    ourDatabase.update(DATABASE_TABLE, cvUpdate, KEY_NAME + "=?", whereArgs); 
} 

public void changeCurrency(String mCoin, String mValue) throws SQLException{ 
    // TODO Auto-generated method stub 
    String[] whereArgs = new String[] { mCoin }; 
    ContentValues cvUpdate = new ContentValues(); 
    cvUpdate.put(KEY_NAME, mCoin);  
    cvUpdate.put(KEY_VALUE, mValue); 
    ourDatabase.update(DATABASE_TABLE, cvUpdate, KEY_NAME + "=?", whereArgs); 
} 



public void deleteEntry(String coin) throws SQLException{ 
    String[] whereArgs = new String[] { coin }; 
    ourDatabase.delete(DATABASE_TABLE, KEY_NAME + "=?", whereArgs); 
} 

public void removeAll() 
{ 
    /*// db.delete(String tableName, String whereClause, String[] whereArgs); 
    // If whereClause is null, it will delete all rows. 
    SQLiteDatabase db = helper.getWritableDatabase(); // helper is object extends SQLiteOpenHelper 
    db.delete(DatabaseHelper.TAB_USERS, null, null); 
    db.delete(DatabaseHelper.TAB_USERS_GROUP, null, null);*/ 
    ourDatabase.delete(DATABASE_TABLE, null, null); 

} 

} 

的代碼的原因的誤差。

public void add(){ 
    boolean didItWork = true; 
    try { 
     //String name = sqlName.getText().toString(); 
     //String hotness = sqlHotness.getText().toString(); 
     calculateaddentry(); 
     String coin = addcoinselection.getSelectedItem().toString(); 
     String quantity = sqlAddCoin.getText().toString(); 
     String ounces = entryounces; 
     String value = denomination + newentryvalue; 


     //PortfolioDatabase entry = new PortfolioDatabase(Portfolio.this); 
     info.open(); 
     info.createEntry(coin, quantity, ounces, value); 
     info.close(); 

     info.open(); 
     String data = info.getData();  
     String ccoin = info.getCoin();  
     String cqty = info.getQty();   
     String counces = info.getOunces();   
     String cvalue = info.getValue(); 
     info.close(); 

     currentcoin.setText(ccoin); 
     currentqty.setText(cqty); 
     currentounces.setText(counces); 
     currentvalue.setText(cvalue); 

    } catch (Exception e) { 
     didItWork = false; 
     String error = e.toString(); 
     Dialog d = new Dialog(this); 
     d.setTitle("Dang it!"); 
     TextView tv = new TextView(this); 
     tv.setText(error); 
     d.setContentView(tv); 
     d.show(); 
    } finally { 
     if (didItWork) { 
      /*Dialog d = new Dialog(this); 
      d.setTitle("Heck Yea!"); 
      TextView tv = new TextView(this); 
      tv.setText("Success"); 
      d.setContentView(tv); 
      d.show();*/ 
     } 
    }} 
+0

你準確得到錯誤的地方?像特定的行號 – MAC

+0

最初我在這一行得到錯誤,指示遊標未關閉:Cursor c = ourDatabase.query(DATABASE_TABLE,columns,null,null,null,null,null); –

+0

雖然現在沒有logcat錯誤。只是IllegalArgumentException當我運行此代碼:info.open(); \t \t \t info.createEntry(coin,quantity,ozs,value); \t \t \t info.close(); \t \t \t \t \t \t info.open(); \t \t \t String data = info.getData(); \t \t \t info.close(); \t \t \t info.open(); \t \t String ccoin = info.getCoin(); \t \t info.close(); \t \t info.open(); \t \t String cqty = info.getQty(); \t \t info.close(); \t \t info.open(); \t \t String counces = info.getOunces(); \t \t info.close(); \t \t info.open(); \t \t String cvalue = info.getValue(); \t \t info.close(); –

回答

1

嘗試DATABASE_VERSION值更改爲2和增量當您更改表結構,添加列或添加表中數據庫類。

DATABASE_VERSION中的更改觸發對onUpgrade()的調用,在您的情況下會刪除表並創建一個新表。只要DATABASE_VERSION是相同的,onUpgrade()onCreate()將不會被調用,因此該結構不會受到影響,因此不會更新數據庫結構。這可能是您收到IllegalArgumentException的原因,因爲它可能無法在設備數據庫中找到列,因爲列不在那裏。這是說明文檔時的含義:

注意:此類假設升級版本號單調遞增。

http://developer.android.com/reference/android/database/sqlite/SQLiteOpenHelper.html

因此,如果你犯了一個簡單的數據庫,運行它,然後在onCreate()方法添加一列不改變數據庫版本,數據庫類方法onCreate()onUpgrade()不會被調用,因爲就它而言,數據庫是一樣的。

你必須記住一個Android本地數據庫是persistent。它會在應用程序運行之後停留在您的設備上。因此,數據庫將信息從應用程序的一次運行保存到另一次。數據庫不會每次都重置您運行該應用程序。這不僅包括它持有的數據,而且還包括的數據庫結構,如表和列。

當您首先運行代碼時,它會檢查數據庫是否已經創建。這是在實際的設備上。如果存在,它將檢查設備上的數據庫版本是否與幫助程序類中的DATABASE_VERSION匹配。如果是這樣,它什麼都不會做。如果數據庫類中的DATABASE_VERSION大於設備上的數據庫版本,則會調用onUpgrade()方法,並按照定義調整數據庫。

+0

沒有這個運氣。感謝您的建議,儘管 –

+0

你確定你改變了版本號嗎?您是否嘗試過在設置 - > apps-> yourapp->清除數據中手動刪除設備中的數據庫? –

+0

是的。我試着清除數據並卸載/重新安裝。 –

0

請確保不留下待定事務並打開遊標。

對於INSERT,UPDATE,DELETE:

db.beginTransaction(); 
try{ 

    //INSERT/UPDATE/DELETE statements here 

    db.setTransactionSuccessful(); 
}finally { 
    db.endTransaction(); 
} 

遊標:

Cursor c = db.query(// query code 

try{ 
    while(c.moveToNext()){ 

    // read data 

    } 
} finally{ 
    c.close(); 
} 
+0

不幸。謝謝。我懷疑,也許這個例外是因爲我從對話框中獲得了一個值 –

0

嘗試將數據庫名稱和表名重命名爲全部小寫。

private static final String DATABASE_NAME = "portfolio_database"; 
private static final String DATABASE_TABLE = "cointype_table"; 

以前有數據庫嗎?

+0

重命名不起作用。數據庫工作正常,直到我添加此列。這太令人沮喪了。 –

+0

你添加了哪一列?您是否在添加列之前運行該應用程序? –