[英]Android SQLite array is empty
我有一个问题:
如果LocationCode
等于名为SKM
的字符串,则来自ShopName
值将插入到名为ShopName
的数组中。
但是,数组为空,为什么?
DatabaseAccess.java
public class DatabaseAccess {
private SQLiteOpenHelper openHelper;
private SQLiteDatabase database;
private static DatabaseAccess instance;
/**
* Private constructor to aboid object creation from outside classes.
*
* @param context
*/
public DatabaseAccess(Context context) {
this.openHelper = new DatabaseOpenHelper(context);
}
/**
* Return a singleton instance of DatabaseAccess.
*
* @param context the Context
* @return the instance of DabaseAccess
*/
public static DatabaseAccess getInstance(DatabaseGrabber context) {
if (instance == null) {
instance = new DatabaseAccess(context);
}
return instance;
}
/**
* Open the database connection.
*/
public void open() {
this.database = openHelper.getWritableDatabase();
}
/**
* Close the database connection.
*/
public void close() {
if (database != null) {
this.database.close();
}
}
/**
* Read all quotes from the database.
*
* @return a List of quotes
*/
public List<String> getQuotes() {
String WhiteFarm = "SKM";
List<String> list = new ArrayList<>();
Cursor cursor = database.rawQuery("SELECT * FROM WhereToEat WHERE 'LocationCode' = " + "'" + WhiteFarm + "'", null);
cursor.moveToFirst();
if (cursor.moveToFirst()) {
do {
// do what you need with the cursor here
list.add(cursor.getString(0));
} while (cursor.moveToNext());
}
cursor.close();
return list;
}
}
DatabaseGrabber.java
public class DatabaseGrabber extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.selection_main);
final TextView Shop_name = (TextView)findViewById(R.id.ShopName);
DatabaseAccess databaseAccess = DatabaseAccess.getInstance(this);
databaseAccess.open();
final List<String> ShopName = databaseAccess.getQuotes();
databaseAccess.close();
Button StartDraw = (Button)findViewById(R.id.draw_start);
StartDraw.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (ShopName.size()>0) {
Collections.shuffle(ShopName);
String random = ShopName.get(0);
Shop_name.setText(random);
}
else{
Shop_name.setText("Nothing here!");
}
}
});
}
}
DatabaseOpenHelper.java
public class DatabaseOpenHelper extends SQLiteAssetHelper {
private static final String DATABASE_NAME = "WhereToEat";
private static final int DATABASE_VERSION = 1;
public DatabaseOpenHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
}
这是SQL文件
BEGIN TRANSACTION;
CREATE TABLE "WhereToEat" (
`ShopName` TEXT,
`Location` TEXT,
`LocationCode` TEXT,
`Photo` BLOB
);
INSERT INTO `WhereToEat` VALUES ('KFC','Shek Kip Mei','WhiteFarm',NULL);
INSERT INTO `WhereToEat` VALUES ('McDonalds','Lai Chi Kok','SKM',NULL);
INSERT INTO `WhereToEat` VALUES ('SomethingBig','Nam Cheong','SKM',NULL);
INSERT INTO `WhereToEat` VALUES ('Subway','Tsuen Wan','SKM',NULL);
COMMIT;
谢谢。
您在WHERE
子句中的列名WHERE
都有引号。 对于每个潜在的结果行,您都在检查'LocationCode' = 'SKM'
,这显然总是错误的。
删除列名周围的引号,我希望您会很好。
使用它时,请使用占位符( ?
)作为字符串值来清理代码:
Cursor cursor = database.rawQuery(
"SELECT * FROM WhereToEat WHERE LocationCode = ?",
new String[]{WhiteFarm});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.