即使我的 table 中有该列,获取列也不存在

Getting column doesn't exist even when I have the column in my table

我正在尝试更新数据库中的 2 个 table 之一。我一直收到 "no such column" 错误,即使我在 table 中有该列。

这是创建 table 的代码:(根据答案更新)

// DRIVE table creation statement
private static final String CREATE_DRIVE_TABLE = "create table " + TABLE_DRIVES + " (" +
        KEY_DRIVE_ID + " integer primary key autoincrement, " +
        KEY_START_STOP_TIME + " integer, " +
        KEY_DATE + " string, " +
        KEY_DISTANCE + " integer, " +
        KEY_LENGTH + " string, " +
        KEY_BUINESS_TRIP + "integer, " +
        KEY_LOCATION + "string, " +
        KEY_ODO + " string );";

这是我从一个 table 获取数据并更新另一个 table 的方法。错误发生在该方法最后的更新行中:

public  int setDriveDetails(long driveId)
{
    SQLiteDatabase db = this.getReadableDatabase();

    List<GPSDataModel> data = new ArrayList<GPSDataModel>();

    Cursor cursor = db.query(TABLE_DATA, new String[]{KEY_LAT, KEY_LNG, KEY_TIME}, KEY_DRIVE_ID + "=" + driveId, null, null, null, null);

    if (cursor.moveToFirst())
    {
        try {
            do {
                GPSDataModel dataPoint = new GPSDataModel();
                dataPoint.setLat(cursor.getString(cursor.getColumnIndex(KEY_LAT)));
                dataPoint.setLng(cursor.getString(cursor.getColumnIndex(KEY_LNG)));
                dataPoint.setTime(cursor.getInt(cursor.getColumnIndex(KEY_TIME)) );

                data.add(dataPoint);

            } while (cursor.moveToNext());

            //Get the start and stop time of the drive
            SimpleDateFormat sf = new SimpleDateFormat("HH:mm");
            String startTime = sf.format(data.get(0).getTime()); // start time of the drive as formatted string
            String stopTime = sf.format(data.get(data.size()-1).getTime()); // stop time of the drive as formatted string
            String driveTime = startTime+" - "+stopTime; // start and stop time of the drive

            // get the distance of the drive
            // using spherical distance calculator from Google Maps utils.
            double distance = 0;
            double lastLat = 0;
            double lastLng = 0;

            for (int i = 0; i < data.size(); i++)
            {
                double currentLat = Double.parseDouble(data.get(i).getLat());
                double currentLng = Double.parseDouble(data.get(i).getLng());
                if (i > 0) {
                    double d = SphericalUtil.computeDistanceBetween(new LatLng(
                            lastLat, lastLng), new LatLng(currentLat, currentLng));
                    distance = distance + d;
                }
                lastLat = currentLat;
                lastLng = currentLng;
            }

            //TODO: add the ODO calculations here.


            // set the distance and time of the drive to the data base.
            ContentValues driveDetails = new ContentValues();
            driveDetails.put(KEY_DISTANCE, distance);
            driveDetails.put(KEY_LENGTH, driveTime);

            db.update(TABLE_DRIVES, driveDetails, KEY_DRIVE_ID+ "=" +driveId,null); // add the distance and time to the drive details.

        } finally {
            db.close();
        }
    }
    else
        return -1; // return -1 if there is no data for that drive.

    return 0;
}

最后是日志:

java.lang.RuntimeException: Unable to start service drvr.drvlog.DataCollectionService@42c53608 with Intent { act=stop_drive cmp=drvr.drvlog/.DataCollectionService }: android.database.sqlite.SQLiteException: no such column: length (code 1): , while compiling: UPDATE drives SET length=?,distance=? WHERE _id=1
   at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2877)
   at android.app.ActivityThread.access00(ActivityThread.java:161)
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1362)
   at android.os.Handler.dispatchMessage(Handler.java:102)
   at android.os.Looper.loop(Looper.java:157)
   at android.app.ActivityThread.main(ActivityThread.java:5356)
   at java.lang.reflect.Method.invokeNative(Method.java)
   at java.lang.reflect.Method.invoke(Method.java:515)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
   at dalvik.system.NativeStart.main(NativeStart.java)
  Caused by: android.database.sqlite.SQLiteException: no such column: length (code 1): , while compiling: UPDATE drives SET length=?,distance=? WHERE _id=1
   at      android.database.sqlite.SQLiteConnection.nativePrepareStatement(SQLiteConnection.java)
   at   android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteCo nnection.java:1113)
    at  android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:690)
    at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
   at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
   at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
   at android.database.sqlite.SQLiteDatabase.updateWithOnConflict(SQLiteDatabase.java:1688)
   at android.database.sqlite.SQLiteDatabase.update(SQLiteDatabase.java:1636)
   at drvr.drvlog.DataBaseHelper.setDriveDetails(DataBaseHelper.java:251)
   at drvr.drvlog.DataCollectionService.stopDrive(DataCollectionService.java:94)
   at drvr.drvlog.DataCollectionService.onStartCommand(DataCollectionService.java:49)
   at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2860)
   at android.app.ActivityThread.access00(ActivityThread.java:161)
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1362)
   at android.os.Handler.dispatchMessage(Handler.java:102)
   at android.os.Looper.loop(Looper.java:157)
   at android.app.ActivityThread.main(ActivityThread.java:5356)
   at java.lang.reflect.Method.invokeNative(Method.java)
   at java.lang.reflect.Method.invoke(Method.java:515)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
   at dalvik.system.NativeStart.main(NativeStart.java)  

当列在 create table 语句中时,为什么我得到一个列不存在。

这里似乎有问题

 private static final String CREATE_DRIVE_TABLE = "create table " + TABLE_DRIVES + " (" +
        KEY_DRIVE_ID + " integer primary key autoincrement," +
        KEY_START_STOP_TIME + " integer," +
        KEY_DATE + " string" + <-- No space here, no comma
        KEY_DISTANCE + " integer" + <-- No space here
        KEY_LENGTH + " string" + <-- No space here
        KEY_BUINESS_TRIP + "integer" + <-- No space here
        KEY_LOCATION + "string" + <-- No space here
        KEY_ODO + " string );";

P.S。如果您更改修改了数据库结构,请卸载并重新安装该应用程序。

实际上你的table没有创建,因为你的Create Table SQL Command是错误的

试试下面一个

String CREATE_DRIVE_TABLE = "create table " + TABLE_DRIVES + " (" +
    KEY_DRIVE_ID + " integer primary key autoincrement," +
    KEY_START_STOP_TIME + " integer, " +
    KEY_DATE + " string, " +
    KEY_DISTANCE + " integer, " +
    KEY_LENGTH + " string, " +
    KEY_BUINESS_TRIP + "integer, " +
    KEY_LOCATION + "string, " +
    KEY_ODO + " string );";

更改后,您必须卸载您的应用并重新安装一个全新构建的应用。