我正在学习有关使用Android Java设置SQLite数据库的教程。我创建了一个数据库和一个简单的表来包含有关食物及其卡路里的数据。我设置了一些按钮,其中包含输入数据,查看,获取特定条目的信息,修改和删除它的功能。
除了获取特定条目的按钮外,一切正常。当我点击“获取信息”按钮时,根据我输入的行ID,它应该返回食物名称和卡路里值。
任何人都可以帮忙查看我的代码,检查错误或缺失的内容吗?
这是类的代码。我已经排除了一些与之无关的部分。 创建数据库:
public class FormDatabase
{
public static final String KEY_ROWID = "_id";
public static final String KEY_FOOD = "food_name";
public static final String KEY_CALORIE = "food_calories";
private static final String DATABASE_NAME = "Calories";
private static final String DATABASE_TABLE = "FoodTable";
private static final int DATABASE_VERSION = 2;
private DbHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;
private static class DbHelper extends SQLiteOpenHelper
{
public DbHelper(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +
KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT , " +
KEY_FOOD + " TEXT NOT NULL , " +
KEY_CALORIE + " TEXT NOT NULL);"
);
}
public String getFood(long l) throws SQLException{
// get data of food name
String[] columns = new String[]{ KEY_ROWID, KEY_FOOD, KEY_CALORIE};
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, KEY_ROWID + "=" + l, null, null, null, null);
if (c != null){
c.moveToFirst();
String food = c.getString(1);
return food;
}
return null;
}
public String getCalorie(long l) throws SQLException{
// get data of food calorie
String[] columns = new String[]{ KEY_ROWID, KEY_FOOD, KEY_CALORIE};
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, KEY_ROWID + "=" +
l, null, null, null, null);
if (c != null){
c.moveToFirst();
String calorie = c.getString(2);
return calorie;
}
return null;
}
另一个用于设置主页的类:
public class DatabaseMain extends Activity implements OnClickListener{
Button sqlUpdate, sqlView, sqlModify, sqlGetInfo, sqlDelete;
EditText sqlFood, sqlCalorie, sqlRow;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.database_main);
sqlUpdate = (Button) findViewById(R.id.bSQLUpdate);
sqlFood = (EditText) findViewById(R.id.etSQLFood);
sqlCalorie = (EditText) findViewById(R.id.etSQLCalorie);
sqlView = (Button) findViewById (R.id.bSQLopenView);
sqlView.setOnClickListener(this);
sqlUpdate.setOnClickListener(this);
sqlRow = (EditText) findViewById(R.id.etSQLrowInfo);
sqlModify = (Button) findViewById(R.id.bSQLmodify);
sqlGetInfo = (Button) findViewById(R.id.bgetInfo);
sqlDelete = (Button) findViewById(R.id.bSQLdelete);
sqlDelete.setOnClickListener(this);
sqlModify.setOnClickListener(this);
sqlDelete.setOnClickListener(this);
}
public void onClick(View arg0)
{
// When click on buttons, data entry into database, view, modify and
delete row
switch (arg0.getId())
{
case R.id.bgetInfo:
try {
String s = sqlRow.getText().toString();
long l = Long.parseLong(s);
FormDatabase foodcal = new FormDatabase(this);
foodcal.open();
String returnedFood = foodcal.getFood(l);
String returnedCalories = foodcal.getCalorie(l);
foodcal.close();
sqlFood.setText(returnedFood);
sqlCalorie.setText(returnedCalories);
}catch (Exception e)
{
String error = e.toString();
Dialog d = new Dialog(this);
d.setTitle("This is an error!");
TextView tv = new TextView(this);
tv.setText(error);
d.setContentView(tv);
d.show();
}
break;
}
}
database_main layout xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width ="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView1"
android:text="Food"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"/>
<EditText
android:id="@+id/etSQLFood"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text" >
</EditText>
<TextView
android:id="@+id/textView3"
android:text="Food Calorie"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<EditText
android:id="@+id/etSQLCalorie"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number" >
</EditText>
<Button
android:text="Update SQLite Database"
android:id="@+id/bSQLUpdate"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
<Button
android:text="View"
android:id="@+id/bSQLopenView"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
<TextView
android:id="@+id/textView2"
android:text="Enter Row ID"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<EditText
android:id="@+id/etSQLrowInfo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number" >
<requestFocus></requestFocus>
</EditText>
<Button
android:text="Get Information"
android:id="@+id/bgetInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
<Button
android:text="Edit Entry"
android:id="@+id/bSQLmodify"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
<Button
android:text="Delete Entry"
android:id="@+id/bSQLdelete"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
</LinearLayout>
答案 0 :(得分:0)
在switch语句中添加一个检查,添加一个默认大小写:
switch (arg0.getId())
{
case R.id.bgetInfo:
... do something...
break;
default:
//this will handle everything else
// write out the argument for debugging, eg:
Console.WriteLine(arg0.getId().ToString());
break;
}
这将告诉你即将发生的事情。如果arg0.getId()不是R.id.bgetInfo,则事件不会触发......
答案 1 :(得分:0)
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, KEY_ROWID + "=" +
l, null, null, null, null);
而是使用上面的语句使用这个
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, KEY_ROWID + "=?",
new String[] { String.valueOf(l) }, null, null, null, null);