Android - 检查来自 SQLite 数据库的值 table
Android - Checking value from an SQLite DB table
我正在尝试从新删除的 SQLite 数据库 table 行中读取数据。
基本上我的程序会在加载某个activity时删除一行,我想检查行内的值。
这是我的 get
代码:
public String getSlot() {
String slots = new String();
Cursor cursor = database.query(ChosenSlotDatabaseHandler.TABLE_CHOSEN_PARKING_SLOT,
chosenSlotColumn, null, null, null, null, null);
if( cursor.moveToFirst() ) {
slots = cursor.getString(0);
}
// make sure to close the cursor
cursor.close();
return slots;
}
我使用这些代码删除了值:
public static final String DELETE_SLOT = "DELETE FROM "
+ TABLE_CHOSEN_PARKING_SLOT + " WHERE "
+ "_ID = " + CHOSEN_ID + ";";
public void deleteSlotSQL()
{
database.execSQL(ChosenSlotDatabaseHandler.DELETE_SLOT);
}
这是我设置 TextView
值的条件:
if(slots == null)
{
chosenSlotView.setText("You have not parked");
}
else
{
chosenSlotView.setText("You are parked in : " + slots);
}
一开始我以为只有一行被删除后,getSlot()
就会returnnull,但是从这个Log.d
我运行看来不为null ] :
if(slots != null)
{
Log.d("notnull","not null dude");
}else
{
Log.d("null","Yay null");
}
log
returns "not null dude"..
关于如何获取 slots
值以便我可以设置 TextView
值的任何建议??
首先,您应该避免使用像 "not null dude" 这样的日志,您会发现它们很有趣且具有启发性,但过一会儿我们将无法编写干净专业的代码。
我的第二个建议是使用常量而不是 hadcoded 字符串。创建一个 class 常量并在其中添加字符串
public static final String NOT_PARKED = "You have not parked"
我的第三个建议是看看 ormlite http://logic-explained.blogspot.ro/2011/12/using-ormlite-in-android-projects.html
如果日志没有问题,可能是 textview 有问题。尝试在条件之前将文本放在 textview 中以检查是否会设置。还要检查布局文件。
您的 slots
绝对不为空,因为:
字符串槽 = new String();
应该是
String slots = null;
Cursor cursor = database.query(ChosenSlotDatabaseHandler.TABLE_CHOSEN_PARKING_SLOT,
chosenSlotColumn, null, null, null, null, null);
if( cursor.moveToFirst() ) {
slots = cursor.getString(0);
}
// make sure to close the cursor
cursor.close();
return slots;
编辑:
没关系 String slot = null
,
尝试使用:
if(slots.isEmpty())
{
chosenSlotView.setText(notParked);
}
else
{
chosenSlotView.setText(isParked + slots);
}
我正在尝试从新删除的 SQLite 数据库 table 行中读取数据。
基本上我的程序会在加载某个activity时删除一行,我想检查行内的值。
这是我的 get
代码:
public String getSlot() {
String slots = new String();
Cursor cursor = database.query(ChosenSlotDatabaseHandler.TABLE_CHOSEN_PARKING_SLOT,
chosenSlotColumn, null, null, null, null, null);
if( cursor.moveToFirst() ) {
slots = cursor.getString(0);
}
// make sure to close the cursor
cursor.close();
return slots;
}
我使用这些代码删除了值:
public static final String DELETE_SLOT = "DELETE FROM "
+ TABLE_CHOSEN_PARKING_SLOT + " WHERE "
+ "_ID = " + CHOSEN_ID + ";";
public void deleteSlotSQL()
{
database.execSQL(ChosenSlotDatabaseHandler.DELETE_SLOT);
}
这是我设置 TextView
值的条件:
if(slots == null)
{
chosenSlotView.setText("You have not parked");
}
else
{
chosenSlotView.setText("You are parked in : " + slots);
}
一开始我以为只有一行被删除后,getSlot()
就会returnnull,但是从这个Log.d
我运行看来不为null ] :
if(slots != null)
{
Log.d("notnull","not null dude");
}else
{
Log.d("null","Yay null");
}
log
returns "not null dude"..
关于如何获取 slots
值以便我可以设置 TextView
值的任何建议??
首先,您应该避免使用像 "not null dude" 这样的日志,您会发现它们很有趣且具有启发性,但过一会儿我们将无法编写干净专业的代码。 我的第二个建议是使用常量而不是 hadcoded 字符串。创建一个 class 常量并在其中添加字符串
public static final String NOT_PARKED = "You have not parked"
我的第三个建议是看看 ormlite http://logic-explained.blogspot.ro/2011/12/using-ormlite-in-android-projects.html
如果日志没有问题,可能是 textview 有问题。尝试在条件之前将文本放在 textview 中以检查是否会设置。还要检查布局文件。
您的 slots
绝对不为空,因为:
字符串槽 = new String();
应该是
String slots = null;
Cursor cursor = database.query(ChosenSlotDatabaseHandler.TABLE_CHOSEN_PARKING_SLOT,
chosenSlotColumn, null, null, null, null, null);
if( cursor.moveToFirst() ) {
slots = cursor.getString(0);
}
// make sure to close the cursor
cursor.close();
return slots;
编辑:
没关系 String slot = null
,
尝试使用:
if(slots.isEmpty())
{
chosenSlotView.setText(notParked);
}
else
{
chosenSlotView.setText(isParked + slots);
}