我正在创建一个普通的android应用程序,试图在其中使用Sqlite数据库获取数据,但未检索到值,请告诉我我要去哪里了?
activity_main.xml
<Button
android:id="@+id/button_viewAll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="View Details"
/>
DatabaseHelper类
public static final String DATABASE_NAME = "Gym.db";
public static final String TABLE_NAME = "Exercise";
public static final String COL_1 = "exercise_id";
public static final String COL_2 = "exercise_name";
public static final String COL_3 = "body_part";
public static final String COL_4 = "description";
public DatabaseHelper( Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table " + TABLE_NAME +" (exercise_id INTEGER PRIMARY KEY AUTOINCREMENT,exercise_name TEXT,body_part TEXT,description TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
onCreate(db);
}
public Cursor getAllData() {
SQLiteDatabase db = this.getWritableDatabase();
Cursor res = db.rawQuery("select * from "+TABLE_NAME, null);
return res;
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
DatabaseHelper myDb;
Button texercise_id, texercise_name, tbody_part, tdescription;
Button btnviewAll;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myDb = new DatabaseHelper(this);
texercise_id = (Button)findViewById(R.id.button_viewAll);
texercise_name = (Button)findViewById(R.id.button_viewAll);
tbody_part = (Button)findViewById(R.id.button_viewAll);
tdescription = (Button)findViewById(R.id.button_viewAll);
btnviewAll = (Button)findViewById(R.id.button_viewAll);
viewAll();
}
public void viewAll() {
btnviewAll.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
Cursor res = myDb.getAllData();
if(res.getCount() == 1) {
// show message
showMessage("Error","Nothing found");
return;
}
StringBuffer buffer = new StringBuffer();
while (res.moveToNext()) {
buffer.append("exercise_id :"+ res.getString(0)+"\n");
buffer.append("exercise_name :"+ res.getString(1)+"\n");
buffer.append("body_part :"+ res.getString(2)+"\n");
buffer.append("description :"+ res.getString(3)+"\n\n");
}
// Show all data
showMessage("Data Found",buffer.toString());
}
}
);
}
public void showMessage(String title,String Message){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(true);
builder.setTitle(title);
builder.setMessage(Message);
builder.show();
}
}
我之前也尝试过此操作,但是从SQLite数据库检索数据时遇到了同样的问题。
答案 0 :(得分:0)
从您的代码看来,一切正常。 但是,似乎没有添加任何数据,也没有添加数据的方法。因此,该对话框未显示任何数据,但未显示表明无数据的消息。
要修复不显示任何数据的对话框,您需要检查游标是否没有行而不是没有行。所以换行:-
if(res.getCount() == 1) {
成为
if(res.getCount() < 1) {
要显示一些数据,您需要添加一些数据。要添加一些数据,您可以在 DatabaseHelper 类中提供一种方法,该方法允许添加数据。例如:-
public long addData(String excercise_name, String body_part, String description) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(COL_2,excercise_name);
cv.put(COL_3,body_part);
cv.put(COL_4,body_part);
return db.insert(TABLE_NAME,null,cv);
}
然后,您需要实际添加一些数据,例如,可以将以下方法添加到您的 MainActivity 中:-
private void addSomeDataButOnlyIfNoneExists() {
if (DatabaseUtils.queryNumEntries(myDb.getWritableDatabase(),DatabaseHelper.TABLE_NAME) < 1) {
myDb.addData("Squat","Legs","Squat");
myDb.addData("Push up","Arms","Push up");
myDb.addData("Run","Legs and Arms","Rapidly put one foot in front of the other");
}
}
您可以使用:-
调用该方法protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myDb = new DatabaseHelper(this);
addSomeDataButOnlyIfNoneExists(); //<<<<<<<<<< ADDED
........ rest of your code
使用以上命令,运行应用程序,然后单击查看详细信息按钮,结果为:-