gpt4 book ai didi

java - (初学者)SQLite 查询不会使用 MoveToNext 进入 if-loop

转载 作者:行者123 更新时间:2023-12-02 12:56:07 29 4
gpt4 key购买 nike

我整个早上都在 StackOverflow 上尝试自学如何在 Android 应用程序上对我上传的 SQLite 数据库进行查询。我尝试过原始查询和直接查询,但到目前为止都没有奏效。这是代码:

public Product getProduct(int id) {
db = this.getReadableDatabase();


// new String[] { COLUMN_ID, COLUMN_PRODUCT_NAME, COLUMN_SIZE, COLUMN_HP,
// COLUMN_CATEGORY, COLUMN_POWER_TYPE, COLUMN_EXP_PROOF, COLUMN_RPM,
// COLUMN_PSI, COLUMN_GPM, COLUMN_PRICE},

// Cursor cursor = db.query(
// TABLE_NAME,
// null,
// COLUMN_ID + " = ?",
// new String[] { Integer.toString(id)},
// null, null, null, null);

String strquery = "SELECT * FROM " + TABLE_NAME + " WHERE " + COLUMN_ID + "=" + id;
Cursor cursor = db.rawQuery(strquery, null);

Product product = new Product();


if (cursor.moveToFirst()) {
product.setID(cursor.getInt(0));
product.setName(cursor.getString(1));
product.setSize(cursor.getDouble(2));
product.setHp(cursor.getDouble(3));
product.setCategory(cursor.getString(4));
product.setPowerType(cursor.getString(5));
product.setExplosionProof(cursor.getInt(6));
product.setRPM(cursor.getInt(7));
product.setBypassPSI(cursor.getInt(8));
product.setGPM(cursor.getInt(9));
product.setPrice(cursor.getDouble(10));
cursor.close();
}

db.close();
return product;
}

注释掉的字段是我之前尝试过的查询,但都不起作用。

这是我的常量:

private static final String TABLE_NAME = "Products";
private static final String COLUMN_ID = "'_id'";

/image/5YiZr.png^ 这是我正在使用的数据库屏幕截图的链接。

我调试它时的问题是它永远不会进入if循环,并且mCount始终为-1。我已经做了很多研究,但我仍然对此感到沮丧。谢谢

[编辑}

这是我的数据库代码

public class SQLiteHelper extends SQLiteOpenHelper {

private SQLiteHelper sInstance;

private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "ProductList.db";
private SQLiteDatabase db;
private final Context myContext;
private String databasePath = "";


public SQLiteHelper(Context context){
super(context, DATABASE_NAME, null, DATABASE_VERSION);
this.myContext = context;
databasePath = context.getDatabasePath(DATABASE_NAME).getPath();
}


private static final String TABLE_NAME = "Products";
private static final String COLUMN_ID = "'_id'";
private static final String COLUMN_PRODUCT_NAME = "'Name'";
private static final String COLUMN_SIZE = "'Size'";
private static final String COLUMN_HP = "'HP'";
private static final String COLUMN_CATEGORY = "'Category'";
private static final String COLUMN_POWER_TYPE = "'PowerType'";
private static final String COLUMN_EXP_PROOF = "'ExpProof'";
private static final String COLUMN_RPM = "'RPM'";
private static final String COLUMN_PSI = "'PSI'";
private static final String COLUMN_GPM = "'GPM'";
private static final String COLUMN_PRICE = "'Price'";

public void createDataBase() throws IOException {
boolean dbExists = checkDataBase();
if (dbExists) {
} else {
this.getReadableDatabase();
try {
copyDataBase();
} catch (IOException e) {
throw new Error("Error making database");
}
}
}

private boolean checkDataBase() {
SQLiteDatabase check = null;
try {
String path = databasePath + DATABASE_NAME;
check = SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.OPEN_READONLY);
} catch (SQLiteException e) {
// database doesn't exist
}

if (check != null) {
check.close();
}

return check != null;
}

private void copyDataBase() throws IOException{
InputStream myInput = myContext.getAssets().open(DATABASE_NAME);

String outFileName = databasePath + DATABASE_NAME;

OutputStream myOutput = new FileOutputStream(outFileName);

byte[] buffer = new byte[1024];
int length;
while ((length=myInput.read(buffer))>0) {
myOutput.write(buffer, 0, length);
}

// close streams
myOutput.flush();
myOutput.close();
myInput.close();
}



public void openDataBase() throws SQLException {

//Open the database
String myPath = databasePath;
db = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}

@Override
public synchronized void close() {

if(db != null)
db.close();
super.close();

}

@Override
public void onCreate(SQLiteDatabase db) {

}

最佳答案

请检查您的这部分代码。

public void openDataBase() throws SQLException {
//Open the database
String myPath = databasePath;
db = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}

这里您只是使用databasePath,我发现您已将其设置为从构造函数中的context.getDabasePath 获得的值,但查看您拥有的其他方法看起来像是在创建新的数据库路径:

private void copyDataBase() throws IOException{
InputStream myInput = myContext.getAssets().open(DATABASE_NAME);

String outFileName = databasePath + DATABASE_NAME;

OutputStream myOutput = new FileOutputStream(outFileName);

byte[] buffer = new byte[1024];
int length;
while ((length=myInput.read(buffer))>0) {
myOutput.write(buffer, 0, length);
}

// close streams
myOutput.flush();
myOutput.close();
myInput.close();
}

不确定这是否是由于从源代码复制/粘贴造成的,但这看起来像是导致您丢失数据的原因。

关于java - (初学者)SQLite 查询不会使用 MoveToNext 进入 if-loop,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44438456/

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