我正在尝试更新数据库中的两个表中的一个。我不断得到一个"没有这样的专栏"即使你有一个表中的列,也会出错。
以下是创建表格的代码:(基于答案的更新)
// 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 );";
这是我从一个表获取数据并更新另一个表的方法。错误发生在此方法最后的更新行中:
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.access$2200(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.access$2200(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语句中时,为什么我得到一个列并不存在。
答案 0 :(得分:2)
这里看似问题
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。如果更改了db结构,请卸载并重新安装应用程序。
答案 1 :(得分:2)
实际上您的表格未创建,因为您的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 );";
更改后,您必须卸载您的应用并再次安装新的。