0

我是安卓的菜鸟。并且在使用 SQLite 表时出现非法参数异常。logcat 表明这是由于我的游标没有关闭,所以我关闭了所有游标。现在我仍然收到没有详细消息的 IllegalArgumentException,而且我的 logcat 没有显示任何错误消息,只是调试和警告消息。没有任何错误消息可以为我指明正确的方向,我什至不知道如何开始解决此问题。任何帮助是极大的赞赏。

这是我事先用光标问题修复的代码:

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();*/
        }
    }}
4

3 回答 3

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 本地数据库是持久的。它会在应用程序运行后保留在您的设备上。因此,数据库保存从应用程序的一次运行到另一次运行的信息。每次运行应用程序时,数据库都不会重置。这不仅包括它保存的数据, 包括数据库结构,例如表和列。

当您首先运行代码时,它会检查是否已经创建了数据库。这是在实际设备上。如果存在,它将检查设备上的数据库版本是否与DATABASE_VERSION助手类中的匹配。如果是这样,它什么也做不了。如果 DATABASE_VERSION数据库类中的 大于设备上的数据库版本,则onUpgrade()调用该方法并将根据定义调整数据库。

于 2012-08-29T14:43:16.440 回答
0

确保您没有留下待处理的事务并打开游标。

对于插入、更新、删除:

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();
}
于 2012-08-29T13:40:22.337 回答
0

尝试将数据库名称和表名称重命名为全部小写。

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

数据库以前工作过吗?

于 2012-08-29T15:30:44.747 回答