简体   繁体   English

为什么我会收到死密码警告?

[英]Why am I getting dead code warning?

Before entering the data into the sqlite database, I would want to check whether the item id exist. 在将数据输入sqlite数据库之前,我想检查项ID是否存在。 However, I am getting dead code from these lines. 但是,我从这些行获得了死代码。 What is the reason for it? 它是什么原因?

Dead code: 死代码:

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
    } 

Database handler class: 数据库处理类:

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();
}
}
String checkQuery = "SELECT iid FROM item WHERE iid = KEY_IID";
SQLiteDatabase db = this.getWritableDatabase();

if (checkQuery == null) {

checkQuery can never be null, since you initialize it 2 lines above the if-check... therefore the code within the if-clause will never be called. checkQuery永远不能为null,因为你在if-check之上初始化了2行...因此if子句中的代码永远不会被调用。

String checkQuery = "SELECT iid FROM item WHERE iid = KEY_IID";
SQLiteDatabase db = this.getWritableDatabase();

if (checkQuery == null) {

There is no way that checkQuery will be null here. checkQuery在这里无法为null。 You just initialized it to a literal and it cannot be changed since then. 您刚刚将其初始化为文字,从那时起就无法更改。 Hence the dead code warning. 因此死码警告。

You're setting checkQuery explicitly to be a non-null value, therefore checkQuery is certain to not be null at your if statement. 您将checkQuery显式设置为非null值,因此checkQuery肯定不会在if语句中为null。 As the if will always be false, all the code inside it will be considered dead because it cannot ever execute. 因为if总是假的,所以里面的所有代码都会被认为是死的,因为它无法执行。

因为你在if(checkQuery == null)行之上初始化了checkQuery 2行...

You are checking whether the String checkQuery is null, but you are assigning a value to it two lines above. 您正在检查String checkQuery是否为null,但您要为其分配两行以上的值。 So the ode in the if block will never be executed. 所以if块中的ode永远不会被执行。

if (checkQuery == null) {
String checkQuery = "SELECT iid FROM item WHERE iid = KEY_IID";
    SQLiteDatabase db = this.getWritableDatabase();

    if (checkQuery == null) {
    ContentValues values = new ContentValues();

Yes,it is a dead code, you are assigning somehting in checkQuery and then asking to check if is null, how can it be null when you have already assigned value to it in the line just two lines above it. 是的,这是一个死代码,你在checkQuery中分配somehting然后要求检查是否为null,当你已经在它上面两行的行中为它赋值时,它怎么能为null。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM