2

我正在使用数据库,并且我有以下 DataBaseHandling 类:

public class DatabaseHandler extends SQLiteOpenHelper {

// All Static variables
// Database Version
private static final int DATABASE_VERSION = 4;

// Database Name
private static final String DATABASE_MEASURES = "measuresDataBase";

// Measures table name
private static final String TABLE_MEASURES = "measures";

// Measures Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_DATE = "date";
private static final String KEY_TIME_HOUR = "timeHour";
private static final String KEY_TIME_MINUTE = "timeMinute";
private static final String KEY_BE_INTAKE = "be_intake";
private static final String KEY_GLUCOSE = "glucose";
private static final String KEY_BOLUS = "bolus";
private static final String KEY_BASAL = "basal";

public DatabaseHandler(Context context) {
    super(context, DATABASE_MEASURES, null, DATABASE_VERSION);
}

// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
    String CREATE_MEASURES_TABLE = "CREATE TABLE " + TABLE_MEASURES + "("
            + KEY_ID + " INTEGER PRIMARY KEY," + KEY_DATE + " TEXT,"
            + KEY_TIME_HOUR + " INTEGER NOT NULL," + KEY_TIME_MINUTE
            + " INTEGER NOT NULL," + KEY_BE_INTAKE + " REAL NOT NULL,"
            + KEY_GLUCOSE + " REAL NOT NULL," + KEY_BOLUS
            + " REAL NOT NULL," + KEY_BASAL + "REAL NOT NULL," + ")";

    db.execSQL(CREATE_MEASURES_TABLE);
}

// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // Drop older table if existed

    db.execSQL("DROP TABLE IF EXISTS " + TABLE_MEASURES);

    // Create tables again
    onCreate(db);
}

// Adding new contact
public void addMeasure(Measures m) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(KEY_DATE, m.getDate());
    values.put(KEY_TIME_HOUR, m.getTimeHour());
    values.put(KEY_TIME_MINUTE, m.getTimeMinute());
    values.put(KEY_BE_INTAKE, m.getBolus());
    values.put(KEY_GLUCOSE, m.getGlucose()); // Contact Name
    values.put(KEY_BOLUS, m.getBolus()); // Contact Phone Number
    values.put(KEY_BASAL, m.getBasal());

    // Inserting Row
    db.insert(TABLE_MEASURES, null, values);
    db.close(); // Closing database connection
}

public Measures getLastMeasure(String date) {
    SQLiteDatabase db = this.getReadableDatabase();

    Cursor cursor = db.query(TABLE_MEASURES, new String[] { KEY_ID,
            KEY_DATE, KEY_TIME_HOUR, KEY_TIME_MINUTE, KEY_BE_INTAKE,
            KEY_GLUCOSE, KEY_BOLUS, KEY_BASAL }, KEY_DATE + "=?",
            new String[] { date }, null, null,
            KEY_TIME_HOUR + " DESC, " + KEY_TIME_MINUTE + " DESC", "1");
    if (cursor != null)
        cursor.moveToFirst();

    Measures m = new Measures(Long.parseLong(cursor.getString(0)), // id
            cursor.getString(1), // date
            Integer.parseInt(cursor.getString(2)), // timeHour
            Integer.parseInt(cursor.getString(3)), // timeMinute
            Double.parseDouble(cursor.getString(4)), // BE intake
            Double.parseDouble(cursor.getString(5)), // glucose
            Double.parseDouble(cursor.getString(6)), // bolus
            Double.parseDouble(cursor.getString(7)) // basal

    );
    // return m
    return m;
}

// Getting All Measures
public List<Measures> getAllMeasures() {
    List<Measures> measureList = new ArrayList<Measures>();
    // Select All Query
    String selectQuery = "SELECT  * FROM " + TABLE_MEASURES;

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    // looping through all rows and adding to list
    if (cursor.moveToFirst()) {
        do {
            Measures m = new Measures();
            m.setId(Long.parseLong(cursor.getString(0)));
            m.setDate(cursor.getString(1));
            m.setTimeHour(Integer.parseInt(cursor.getString(2)));
            m.setTimeMinute(Integer.parseInt(cursor.getString(3)));
            m.setBe_intake(Double.parseDouble(cursor.getString(4)));
            m.setGlucose(Double.parseDouble(cursor.getString(5)));
            m.setBolus(Double.parseDouble(cursor.getString(6)));
            m.setBasal(Double.parseDouble(cursor.getString(7)));
            // Adding measure to list
            measureList.add(m);
        } while (cursor.moveToNext());
    }

    // return contact list
    return measureList;
}

logcat 显示:

android.database.sqlite.SQLiteException:没有这样的列:基础(代码 1):,编译时:SELECT id、date、timeHour、timeMinute、be_intake、glucose、bolus、basal FROM mesaures WHERE date=? ORDER BY timeHour DESC LIMIT 1

我会感谢你所有的帮助

我的 Logcat(解决第一个问题后)

08-27 14:34:51.316: E/AndroidRuntime(772): 致命异常: main 08-27 14:34:51.316: E/AndroidRuntime(772): android.database.CursorIndexOutOfBoundsException: 请求索引 0,大小为0 08-27 14:34:51.316: E/AndroidRuntime(772): 在 android.database.AbstractCursor.checkPosition(AbstractCursor.java:418) 08-27 14:34:51.316: E/AndroidRuntime(772): 在 android .database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136) 08-27 14:34:51.316: E/AndroidRuntime(772): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50) 08-27 14:34 :51.316: E/AndroidRuntime(772): at com.example.t1diabetes.DatabaseHandler.getLastMeasure(DatabaseHandler.java:92) 08-27 14:34:51.316: E/AndroidRuntime(772): at com.example.t1diabetes。 Mymeasures.calculateBasal(Mymeasures.java:263) 08-27 14:34:51.316:E/AndroidRuntime(772): at com.example.t1diabetes.Mymeasures.access$1(Mymeasures.java:262) 08-27 14:34:51.316: E/AndroidRuntime(772): at com.example.t1diabetes.Mymeasures$1 .onClick(Mymeasures.java:99) 08-27 14:34:51.316: E/AndroidRuntime(772): 在 android.view.View.performClick(View.java:4084) 08-27 14:34:51.316: E /AndroidRuntime(772): 在 android.view.View$PerformClick.run(View.java:16966) 08-27 14:34:51.316: E/AndroidRuntime(772): 在 android.os.Handler.handleCallback(Handler. java:615) 08-27 14:34:51.316: E/AndroidRuntime(772): 在 android.os.Handler.dispatchMessage(Handler.java:92) 08-27 14:34:51.316: E/AndroidRuntime(772) : 在 android.os.Looper.loop(Looper.java:137) 08-27 14:34:51.316: E/AndroidRuntime(772): 在 android.app.ActivityThread.main(ActivityThread.java:4745) 08-27 14:34:51.316:E/AndroidRuntime(772):在 java.lang。reflect.Method.invokeNative(Native Method) 08-27 14:34:51.316: E/AndroidRuntime(772): at java.lang.reflect.Method.invoke(Method.java:511) 08-27 14:34:51.316 : E/AndroidRuntime(772): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 08-27 14:34:51.316: E/AndroidRuntime(772): 在 com.android。 internal.os.ZygoteInit.main(ZygoteInit.java:553) 08-27 14:34:51.316: E/AndroidRuntime(772): at dalvik.system.NativeStart.main(Native Method)553)08-27 14:34:51.316:E/AndroidRuntime(772):在 dalvik.system.NativeStart.main(本机方法)553)08-27 14:34:51.316:E/AndroidRuntime(772):在 dalvik.system.NativeStart.main(本机方法)

4

5 回答 5

5

在 KEY_BASAL 列之后使用一个空格。由于您的表列名称不正确,因此创建的表没有此列。所以在修改代码后重新安装你的数据库或更改版本。然后你会得到正确的表和列

String CREATE_MEASURES_TABLE = "CREATE TABLE " + TABLE_MEASURES + "("
            + KEY_ID + " INTEGER PRIMARY KEY," + KEY_DATE + " TEXT,"
            + KEY_TIME_HOUR + " INTEGER NOT NULL," + KEY_TIME_MINUTE
            + " INTEGER NOT NULL," + KEY_BE_INTAKE + " REAL NOT NULL,"
            + KEY_GLUCOSE + " REAL NOT NULL," + KEY_BOLUS
            + " REAL NOT NULL," + KEY_BASAL + " REAL NOT NULL" + ")";


if (cursor != null && cursor .getCount()>0){

        cursor.moveToFirst();

///

}
于 2013-08-27T04:26:29.893 回答
2

有一个问题是您在 Key_basal 处遗漏了一个空格,并且还在 onCreate 方法的末尾放置了一个额外的逗号。你的代码:

// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_MEASURES_TABLE = "CREATE TABLE " + TABLE_MEASURES + "("
        + KEY_ID + " INTEGER PRIMARY KEY," + KEY_DATE + " TEXT,"
        + KEY_TIME_HOUR + " INTEGER NOT NULL," + KEY_TIME_MINUTE
        + " INTEGER NOT NULL," + KEY_BE_INTAKE + " REAL NOT NULL,"
        + KEY_GLUCOSE + " REAL NOT NULL," + KEY_BOLUS
        + " REAL NOT NULL," + KEY_BASAL + "REAL NOT NULL," + ")";

db.execSQL(CREATE_MEASURES_TABLE);
}

它应该是 :

// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_MEASURES_TABLE = "CREATE TABLE " + TABLE_MEASURES + "("
        + KEY_ID + " INTEGER PRIMARY KEY," + KEY_DATE + " TEXT,"
        + KEY_TIME_HOUR + " INTEGER NOT NULL," + KEY_TIME_MINUTE
        + " INTEGER NOT NULL," + KEY_BE_INTAKE + " REAL NOT NULL,"
        + KEY_GLUCOSE + " REAL NOT NULL," + KEY_BOLUS
        + " REAL NOT NULL," + KEY_BASAL + " REAL NOT NULL" + ")";

db.execSQL(CREATE_MEASURES_TABLE);
}
于 2013-08-27T04:29:29.063 回答
2

删除,查询结束

KEY_BASAL + "REAL NOT NULL," + ")";
                          ^
                          | Here

并为 Concat 字符串使用空间

KEY_BASAL + "REAL NOT NULL," + ")";
             ^
             | Here

祝你好运

于 2017-06-21T05:43:17.133 回答
1

自动增量 KEY_ID或将一些数据KEY_ID放入此处,并将您的完整日志猫放在这里:

String CREATE_MEASURES_TABLE = "CREATE TABLE " + TABLE_MEASURES + "("
        + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_DATE + " TEXT,"
        + KEY_TIME_HOUR + " INTEGER NOT NULL," + KEY_TIME_MINUTE
        + " INTEGER NOT NULL," + KEY_BE_INTAKE + " REAL NOT NULL,"
        + KEY_GLUCOSE + " REAL NOT NULL," + KEY_BOLUS
        + " REAL NOT NULL," + KEY_BASAL + " REAL NOT NULL" + ")";
于 2013-08-27T05:14:42.240 回答
0

我在使用 junit 测试时遇到了这个错误。我的错误很简单但很邪恶。

我混合getContext()getMockConetext()

于 2013-11-25T16:55:27.050 回答