gpt4 book ai didi

java - 为什么我收到死代码警告?

转载 作者:搜寻专家 更新时间:2023-11-01 04:04:19 24 4
gpt4 key购买 nike

在将数据输入 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();
}
}

最佳答案

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

if (checkQuery == null) {

checkQuery 永远不能为 null,因为您在 if-check 上方 2 行对其进行了初始化...因此永远不会调用 if-clause 中的代码。

关于java - 为什么我收到死代码警告?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22528831/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com