我整个上午都在使用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'";
https://i.stack.imgur.com/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) {
}
答案 0 :(得分:1)
请查看此部分代码。
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();
}
不确定这是否是因为源代码中的复制/粘贴,但这看起来就像是导致您丢失数据的原因。