开发者

SQL Database Type Problem

I am trying to put together an SQL database but don't really know how to make it work. The intention is to have multiple columns, some with integers, some with strings in their cells. For this app, I want repetitions to be an integer and exercise to be a string. Here is the relevant parts of the code:

public static final String KEY_ROWID = "_id";
public static final String KEY_DATE = "date";
public static final String KEY_EXERCISE = "exercise";
public static final String KEY_REPS = "repetitions";

private static final开发者_高级运维 String DATABASE_CREATE = "create table " + DATABASE_TABLE + " (" 
    + KEY_ROWID + " integer primary key autoincrement, "
    + KEY_DATE + " text not null, " 
    + KEY_EXERCISE + " text not null, "
    + KEY_REPS + " int not null, "

public long createExercise(String exercise, int reps) {
    ContentValues initialValues = new ContentValues();
    initialValues.put(KEY_DATE, date);
    initialValues.put(KEY_EXERCISE, exercise);
    initialValues.put(KEY_REPS, reps);

return mDb.insert(DATABASE_TABLE, null, initialValues);
}

I put data in this table using test strings. Then I try to pull the data with the following query:

public Cursor graphQuery(String exercise, String workout) {
    return mDb.query(DATABASE_TABLE, new String[] {KEY_DATE, KEY_REPS}, null, null,    
        null, null, null);

From there I try to put the data into a number array but it gives me an error. It tells me to put KEY_REPS as a number when I declared it. But if I declare KEY_REPS as a number it doesn't let me build my databes.

Cursor cursor = mDbHelper.graphQuery(currentexercise, currentworkout);
startManagingCursor(cursor);
Number[] reps = new Number[]{workoutDbAdapter.KEY_REPS};  //error here

I feel like I am missing a key part in how to create my database. Can anyone help?

Code from book I am trying to follow (except using integers) (from comment on first answer)

private void fillData() {
    Cursor remindersCursor = mDbHelper.fetchAllReminders();
    startManagingCursor(remindersCursor);
// Create an array to specify the fields we want (only the TITLE)
    String[] from = new String[]{RemindersDbAdapter.KEY_TITLE};

That being said, if someone knows of a good website that teaches SQLite as it applies to Android that would be awesome. The only ones I have been able to find are generic SQL sites and they aren't very helpful.


Cursor cursor = mDbHelper.graphQuery(currentexercise, currentworkout);
startManagingCursor(cursor);
Number[] reps = new Number[]{WorkoutDbAdapter.KEY_REPS};  //error here

This code here doesn't do what (I think) you want it to. You need to iterate over the cursor and get the data from there. I'm pretty sure, if you followed the Android sample code for using databases that WorkoutDbAdapter.KEY_REPS is a string constant that holds reps column name.

Try doing something like this:

List<Number> allReps = new ArrayList<Number>();
Cursor cursor = mDbHelper.graphQuery(currentexercise, currentworkout);
while (cursor.moveToNext()) {
    int reps = cursor.getInt(cursor.getColumnIndexOrThrow(mDbHelper.KEY_REPS));
    allReps.add(reps);
}
Number[] repsArray = allReps.toArray(new Number[]{});
// do stuff with repsArray and don't forget to close cursor
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜