2011-06-14 71 views
0

我想把SQL数据库放在一起,但不知道如何使它工作。意图是有多个列,一些是整数,一些是在单元格中有字符串。对于这个应用程序,我希望重复是一个整数,并锻炼成一个字符串。这里是代码的相关部分:SQL数据库类型问题

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); 
} 

我把数据放在这个表中使用测试字符串。然后我尝试用以下查询来提取数据:

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

从那里我尝试将数据放入数组中,但它给了我一个错误。它告诉我在我宣布它时将KEY_REPS作为一个数字。但是,如果我将KEY_REPS声明为数字,它不会让我构建数据集。

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

我觉得我缺少如何创建我的数据库的关键部分。谁能帮忙?

从书中我试图按照代码(除使用整数)(从第一个答案评论)话虽这么说,如果有人知道一个很好的网站,教的SQLite的,因为它适用于Android的

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}; 

那将是真棒。我唯一能找到的是通用SQL站点,它们并不是非常有用。

+0

对于这一点,你问任何问题:请复制并粘贴错误消息的准确* *,因为他们出现了。简单地描述错误信息并不像粘贴它有用。与代码类似 - 我不确定是否按照您定义的方式显示“createExercise”,因为“date”未定义。 – 2011-06-14 17:11:23

回答

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

这里的代码不会做你想要的东西(我想)。你需要遍历游标并从那里获取数据。我很确定,如果你遵循Android示例代码来使用数据库,那么WorkoutDbAdapter.KEY_REPS是一个包含代表列名称的字符串常量。

尝试做这样的事情:

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

我正在关注一本我建立的书的例子。他们的代码工作,但它使用字符串,我试图使用整数。 新代码粘贴在问题底部 – easycheese 2011-06-14 16:48:26

+0

没有本书或代码的其余部分,我猜他们正在构建一个字符串数组的列名传递到另一个查询。继续保持以下示例,但替换:'Number [] reps = new Number [] {KEY_REPS};'用'String [] repsCol = new String [] {KEY_REPS};' – Pedantic 2011-06-14 16:55:21

+0

问题是我需要一个Number数组在我的下一个方法调用中使用。我找不到一个简单的方法将字符串数组转换为数字数组,所以我试图让数据库列为整数,但在这方面也失败了。 (我原来有字符串,它的工作)。 Oooh .... SQL数据库是否将所有内容存储为字符串? – easycheese 2011-06-14 16:58:42