我是 Android 的菜鸟,我试图弄清楚如何使用字符串删除 sqlite 中的表行,而不是行号的 long 。我向表中添加数据没有问题,但是当我尝试删除相同的值时,我得到一个 SQL 异常,上面写着SQLiteException: no such column 'my_entry': while compiling: DELETE FROM dbTABLE WHERE db_NAME = my_entry
非常感谢任何洞察力。这是我的代码:
添加行条目的代码:
public long createEntry(String coin, String quantity, String value) {
ContentValues cv = new ContentValues();
cv.put(KEY_NAME, coin);
cv.put(KEY_QUANTITY, quantity);
cv.put(KEY_VALUE, value);
return ourDatabase.insert(DATABASE_TABLE, null, cv);
}
删除行条目的代码:
public void deleteEntry(String coin) throws SQLException{
ourDatabase.delete(DATABASE_TABLE, KEY_NAME + "=" + coin, null);
}
整个数据库的代码:
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_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);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +
KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
KEY_NAME + " TEXT NOT NULL, " +
KEY_QUANTITY + " TEXT NOT NULL, " +
KEY_VALUE + " TEXT NOT NULL);"
);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
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 value) {
ContentValues cv = new ContentValues();
cv.put(KEY_NAME, coin);
cv.put(KEY_QUANTITY, quantity);
cv.put(KEY_VALUE, value);
return ourDatabase.insert(DATABASE_TABLE, null, cv);
}
public String getData() {
String[] columns = new String[]{ KEY_ROWID, KEY_NAME, KEY_QUANTITY, 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 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(iValue) + "\n";
}
return result;
}
public String getName(long l) throws SQLException{
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 name = c.getString(1);
return name;
}
return null;
}
public String getHotness(long l) throws SQLException{
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;
}
return null;
}
public void updateEntry(String mCoin, String mQuantity, String mValue) throws SQLException {
ContentValues cvUpdate = new ContentValues();
cvUpdate.put(KEY_NAME, mCoin);
cvUpdate.put(KEY_QUANTITY, mQuantity);
cvUpdate.put(KEY_VALUE, mValue);
ourDatabase.update(DATABASE_TABLE, cvUpdate, KEY_NAME + "=" + mCoin, null);
}
public void deleteEntry(String coin) throws SQLException {
ourDatabase.delete(DATABASE_TABLE, KEY_NAME + "=" + coin, null);
}
}