在将数据输入sqlite数据库之前,我想检查项目ID是否存在。但是,我从这些行获得了死代码。它是什么原因?
死码:
if (checkQuery == null) {
ContentValues values = new ContentValues();
values.put(KEY_IID, item.getIID()); // mysql item id
values.put(KEY_NAME, item.getName()); // item name
values.put(KEY_PRICE, item.getPrice()); // item price
values.put(KEY_CREATED_AT, item.getDate()); // Created At
values.put(KEY_TYPE, item.getType()); // type
// Inserting Row
db.insert(TABLE_ITEM, null, values);
db.close(); // Closing database connection
}
数据库处理程序类:
public class DatabaseHandler extends SQLiteOpenHelper {
// All Static variables
// Database Version
private static final int DATABASE_VERSION = 2;
// Database Name
private static final String DATABASE_NAME = "itemManager";
// table name
public static final String TABLE_ITEM = "item";
// item Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_IID = "iid";
private static final String KEY_NAME = "name";
private static final String KEY_PRICE = "price";
private static final String KEY_CREATED_AT = "created_at";
private static final String KEY_TYPE = "type";
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_ITEM_TABLE = "CREATE TABLE " + TABLE_ITEM + "("
+ KEY_ID + " INTEGER PRIMARY KEY autoincrement,"
+ KEY_IID + " INTEGER"
+ KEY_NAME + " TEXT,"
+ KEY_PRICE + " TEXT,"
+ KEY_CREATED_AT + " TEXT,"
+ KEY_TYPE + " TEXT" + ")";
db.execSQL(CREATE_ITEM_TABLE);
}
// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_ITEM);
// Create tables again
onCreate(db);
}
/**
* Storing item details in database
* */
public void addItem(Items item) {
String checkQuery = "SELECT iid FROM item WHERE iid = KEY_IID";
SQLiteDatabase db = this.getWritableDatabase();
if (checkQuery == null) {
ContentValues values = new ContentValues();
values.put(KEY_IID, item.getIID()); // mysql item id
values.put(KEY_NAME, item.getName()); // item name
values.put(KEY_PRICE, item.getPrice()); // item price
values.put(KEY_CREATED_AT, item.getDate()); // Created At
values.put(KEY_TYPE, item.getType()); // type
// Inserting Row
db.insert(TABLE_ITEM, null, values);
db.close(); // Closing database connection
} else {
Log.d("notice", "item already in watchlist");
db.close();
}
}
/**
* Getting item data from database
* */
public List<Items> getAllItems(){
List<Items> itemList = new ArrayList<Items>();
String selectQuery = "SELECT * FROM " + TABLE_ITEM;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// Move to first row
if (cursor.moveToFirst()) {
do {
Items item = new Items();
item.setID(Integer.parseInt(cursor.getString(0)));
item.setName(cursor.getString(1));
item.setPrice(cursor.getString(2));
item.setDate(cursor.getString(3));
item.setType(cursor.getString(3));
// Adding item to list
itemList.add(item);
} while (cursor.moveToNext());
}
// return item list
return itemList;
}
/**
* return true if rows are there in table
* */
public int getRowCount() {
String countQuery = "SELECT * FROM " + TABLE_ITEM;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
int rowCount = cursor.getCount();
db.close();
cursor.close();
// return row count
return rowCount;
}
// Updating item
public int updateItem(Items item) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, item.getName());
values.put(KEY_PRICE, item.getPrice());
values.put(KEY_CREATED_AT, item.getDate());
values.put(KEY_TYPE, item.getType());
// updating row
return db.update(TABLE_ITEM, values, KEY_ID + " = ?",
new String[] { String.valueOf(item.getID()) });
}
// Deleting item
public void deleteItem(Items item) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_ITEM, KEY_ID + " = ?",
new String[] { String.valueOf(item.getID()) });
db.close();
}
}
答案 0 :(得分:6)
String checkQuery = "SELECT iid FROM item WHERE iid = KEY_IID";
SQLiteDatabase db = this.getWritableDatabase();
if (checkQuery == null) {
checkQuery永远不能为null,因为你在if-check之上初始化了2行...因此if子句中的代码永远不会被调用。
答案 1 :(得分:4)
String checkQuery = "SELECT iid FROM item WHERE iid = KEY_IID";
SQLiteDatabase db = this.getWritableDatabase();
if (checkQuery == null) {
这里的checkQuery无法为null。您刚刚将其初始化为文字,从那时起就无法更改。因此,死码警告。
答案 2 :(得分:3)
您将checkQuery显式设置为非null值,因此checkQuery肯定不会在if语句中为null。因为if总是假的,所以里面的所有代码都会被认为是死的,因为它无法执行。
答案 3 :(得分:1)
因为您在checkQuery
行之上初始化了if(checkQuery == null)
2行......
答案 4 :(得分:1)
您正在检查字符串checkQuery
是否为空,但是您要为其分配两行以上的值。因此,if
块中的ode将永远不会被执行。
if (checkQuery == null) {
答案 5 :(得分:0)
String checkQuery = "SELECT iid FROM item WHERE iid = KEY_IID";
SQLiteDatabase db = this.getWritableDatabase();
if (checkQuery == null) {
ContentValues values = new ContentValues();
是的,这是一个死代码,你在checkQuery中分配somehting然后要求检查是否为null,当你已经在它上面两行的行中为它赋值时,它怎么能为null。