我的数据库中有两个表。表 2 的条目与其自己的条目一起显示在一个表 1 中
例如,如果表 1DATABASE_TABLE
和表 2DATBASE_TABLE2
各有 5 个条目,当我进入并查看数据库时,表 1 带有 10 个条目,而表显示为空。
我的 DatabaseManager.java 处理数据库的所有操作
package com.example.draft;
import java.sql.SQLException;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
// Class handling all database operations
public class DatabaseManager
{
//setting variables to use later on in tables and database
public static final String KEY_EXERCISENAME = "exercisename";
public static final String KEY_DURATION = "duration";
private static final String DATABASE_NAME = "MyDatabase";
private static final String DATABASE_TABLE = "weekOne";
private static final String DATABASE_TABLE2 = "weektwo";
private static final int DATABASE_VERSION = 3;
//setting variables to reference DbHelper, its Context, and SQLiteDatabase
private DbHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;
//sub class which creates the database
private static class DbHelper extends SQLiteOpenHelper
{
public DbHelper(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
//method to create the database and table
@Override
public void onCreate(SQLiteDatabase db)
{
// TODO Auto-generated method stub
db.execSQL
("CREATE TABLE " + DATABASE_TABLE + " (" +
KEY_EXERCISENAME + " TEXT NOT NULL, " +
KEY_DURATION + " TEXT NOT NULL);"
);
db.execSQL
("CREATE TABLE " + DATABASE_TABLE2 + " (" +
KEY_EXERCISENAME + " TEXT NOT NULL, " +
KEY_DURATION + " TEXT NOT NULL);"
);
}
//method to show the table if it exists
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE2);
onCreate(db);
}
}
public DatabaseManager(Context c)
{
ourContext = c;
}
public DatabaseManager open()
{
ourHelper = new DbHelper(ourContext);
ourDatabase = ourHelper.getWritableDatabase();
return this;
}
//creating entry in table for treadmill in table week 1 with the help of ContentValues
public long createEntry(String treadmillTimings)
{
// TODO Auto-generated method stub
ContentValues cv = new ContentValues();
//enterting each exercise name corresponding to their respective edit Texts
cv.put(KEY_EXERCISENAME, "Treadmill");
cv.put(KEY_DURATION, treadmillTimings);
return ourDatabase.insert(DATABASE_TABLE, null,cv);
}
//creating entry in table for stepperTimings in table week 1 with the help of ContentValues
public long week1createEntry1 (String stepperTimings)
{
ContentValues cv1 = new ContentValues();
cv1.put(KEY_EXERCISENAME, "Stepper");
cv1.put(KEY_DURATION, stepperTimings);
return ourDatabase.insert(DATABASE_TABLE, null,cv1);
}
//creating entry in table for Stationary Rowing in table week 1 with the help of ContentValues
public long week1createEntry2 (String stationaryRowingTimings)
{
ContentValues cv2 = new ContentValues();
cv2.put(KEY_EXERCISENAME, "Stationary Rowing");
cv2.put(KEY_DURATION, stationaryRowingTimings);
return ourDatabase.insert(DATABASE_TABLE, null,cv2);
}
//creating entry in table for exercise bike in table week 1 with the help of ContentValues
public long week1createEntry3 (String exerciseBikeTimings)
{
ContentValues cv3 = new ContentValues();
cv3.put(KEY_EXERCISENAME, "Exercise Bike");
cv3.put(KEY_DURATION, exerciseBikeTimings);
return ourDatabase.insert(DATABASE_TABLE, null,cv3);
}
//creating entry in table for elliptical trainer in table week 1 with the help of ContentValues
public long week1createEntry4 (String ellipticalTrainerTimings)
{
ContentValues cv4 = new ContentValues();
cv4.put(KEY_EXERCISENAME, "Stationary Rowing");
cv4.put(KEY_DURATION, ellipticalTrainerTimings);
return ourDatabase.insert(DATABASE_TABLE, null,cv4);
}
//displaying/reading data in the table using cursor
public String week1getData()
{
// TODO Auto-generated method stub
String[] columns = new String[]{KEY_EXERCISENAME, KEY_DURATION};
Cursor cur = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
//creating a result(string type variable) to store the text and display it.
String result = "";
int iExerciseName = cur.getColumnIndex(KEY_EXERCISENAME);
int iDuration = cur.getColumnIndex(KEY_DURATION);
// cursor start from the first position, keeps moving to the next as long as the position in not after that last.
for(cur.moveToFirst(); !cur.isAfterLast(); cur.moveToNext())
{
/*getting the rows, exercise name and duration in the tables of database and setting it to result.
.The next time it loops, it will still have the previous result*/
result = result + cur.getString(iExerciseName) + " " + cur.getString(iDuration) + "\n";
}
return result;
}
public long week2createEntry(String treadmillTimingsweek2)
{
// TODO Auto-generated method stub
ContentValues cv = new ContentValues();
//Entering each exercise name corresponding to their respective edit Texts
cv.put(KEY_EXERCISENAME, "Treadmill");
cv.put(KEY_DURATION, treadmillTimingsweek2);
return ourDatabase.insert(DATABASE_TABLE2, null,cv);
}
//creating entry in table for stepperTimings in table week 1 with the help of ContentValues
public long week2createEntry1 (String stepperTimingsweek2)
{
ContentValues cv1 = new ContentValues();
cv1.put(KEY_EXERCISENAME, "Stepper");
cv1.put(KEY_DURATION, stepperTimingsweek2);
return ourDatabase.insert(DATABASE_TABLE2, null,cv1);
}
//creating entry in table for Stationary Rowing in table week 1 with the help of ContentValues
public long week2createEntry2 (String stationaryRowingTimingsweek2)
{
ContentValues cv2 = new ContentValues();
cv2.put(KEY_EXERCISENAME, "Stationary Rowing");
cv2.put(KEY_DURATION, stationaryRowingTimingsweek2);
return ourDatabase.insert(DATABASE_TABLE2, null,cv2);
}
//creating entry in table for exercise bike in table week 1 with the help of ContentValues
public long week2createEntry3 (String exerciseBikeTimingsweek2)
{
ContentValues cv3 = new ContentValues();
cv3.put(KEY_EXERCISENAME, "Exercise Bike");
cv3.put(KEY_DURATION, exerciseBikeTimingsweek2);
return ourDatabase.insert(DATABASE_TABLE2, null,cv3);
}
//creating entry in table for elliptical trainer in table week 1 with the help of ContentValues
public long week2createEntry4 (String ellipticalTrainerTimingsweek2)
{
ContentValues cv4 = new ContentValues();
cv4.put(KEY_EXERCISENAME, "Stationary Rowing");
cv4.put(KEY_DURATION, ellipticalTrainerTimingsweek2);
return ourDatabase.insert(DATABASE_TABLE2, null,cv4);
}
public String week2getData()
{
// TODO Auto-generated method stub
String[] columns = new String[]{KEY_EXERCISENAME, KEY_DURATION};
Cursor cur = ourDatabase.query(DATABASE_TABLE2, columns, null, null, null, null, null);
//creating a result(string type variable) to store the text and display it.
String result = "";
int iExerciseName = cur.getColumnIndex(KEY_EXERCISENAME);
int iDuration = cur.getColumnIndex(KEY_DURATION);
// cursor start from the first position, keeps moving to the next as long as the position in not after that last.
for(cur.moveToFirst(); !cur.isAfterLast(); cur.moveToNext())
{
/*getting the rows, exercise name and duration in the tables of database and setting it to result.
.The next time it loops, it will still have the previous result*/
result = result + cur.getString(iExerciseName) + " " + cur.getString(iDuration) + "\n";
}
return result;
}
我正在显示DATABASE_TABLE
条目的 textView 是android:id = @+id/dbinfo
和DATABASE_TABLE2
条目android:id = "@+id/week2dbinfo
我想要的是DATABASE_TABLE
显示 5 个条目android:id = @+id/dbinfo
并DATABASE TABLE2
显示 5 个条目 android:id = "@+id/week2dbinfo
请帮忙