我是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);
}
}
答案 0 :(得分:10)
您不应该直接使用该字符串,如果您需要将其包装在'
中,因为SQLite会将其视为列名称。
最安全的方法是
public void deleteEntry(String coin) throws SQLException{
String[] whereArgs = new String[] { coin };
ourDatabase.delete(DATABASE_TABLE, KEYNAME + "=?", whereArgs);
}
不安全的方式是
ourDatabase.delete(DATABASE_TABLE, KEYNAME + "='" + coin + "'", null);
如果coin
包含'
,则会失败,所以不要使用它。