我知道问题已被多次询问,但我找不到我的错误。我想在创建表时我做的一切都是正确的。
错误消息是:
android.database.sqlite.SQLiteException:table GPSLocation没有名为Number的列(代码1):
我多次检查空白和标点符号。请帮我找出错误。
这是我的数据库类:
private GPSDatabase(Context context) {
super(context, DB_NAME, null, VERSION);
}
public static GPSDatabase getInstance (final Context context){
if (INSTANCE == null){
INSTANCE = new GPSDatabase(context);
}
return INSTANCE;
}
我在这里创建表
@Override
public void onCreate(final SQLiteDatabase db) {
String createQuery =
"CREATE TABLE " + TABLE_NAME + " (" + ID_COLUMN + " INTEGER PRIMARY KEY, " + NUMBER_COLUMN + " INTEGER DEFAULT NULL, " + DISTANCE_COLUMN + " INTEGER DEFAULT NULL, " + LATITUDE_COLUMN + " REAL DEFAULT NULL, " + LONGITUDE_COLUMN + " REAL DEFAULT NULL)";
db.execSQL(createQuery);
Log.d(TAG, "Table created");
}
onUpgrade()方法会破坏现有的表
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
String dropTable = "DROP TABLE IF EXISTS " + TABLE_NAME;
db.execSQL(dropTable);
Log.d(TAG,"Table deleted");
onCreate(db);
}
创建新的数据库条目
public GPSLocation createGPSLocation (final GPSLocation loc){
long newID = -1;
try{
Log.d(TAG,"createGPSLocation begin");
SQLiteDatabase db = getWritableDatabase();
ContentValues values = new ContentValues();
values.put(NUMBER_COLUMN, loc.getNumber());
values.put(DISTANCE_COLUMN, loc.getDistance());
values.put(LATITUDE_COLUMN, loc.getLatitude());
values.put(LONGITUDE_COLUMN, loc.getLongitude());
newID = db.insert(TABLE_NAME, null, values);
db.close();
}catch (SQLiteException e){
Log.e(TAG,"insert");
}finally {
Log.d(TAG,"insert with ID " + newID);
}
Log.d(TAG, "createGPSLocation successful");
return readGPSLocation(newID);
}
public GPSLocation readGPSLocation(final long id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_NAME, new String[]{ID_COLUMN, NUMBER_COLUMN, DISTANCE_COLUMN,
LATITUDE_COLUMN, LONGITUDE_COLUMN}, ID_COLUMN + " = ?", new String[]{String.valueOf(id)}, null, null, null);
GPSLocation loc = null;
if (cursor != null && cursor.getCount() > 0) {
cursor.moveToFirst();
loc = new GPSLocation(cursor.getInt(cursor.getColumnIndex(NUMBER_COLUMN)));
loc.setId(cursor.getLong(cursor.getColumnIndex(ID_COLUMN)));
loc.setDistance(cursor.getInt(cursor.getColumnIndex(DISTANCE_COLUMN)));
loc.setLatitude(cursor.getDouble(cursor.getColumnIndex(LATITUDE_COLUMN)));
loc.setLongitude(cursor.getDouble(cursor.getColumnIndex(LATITUDE_COLUMN)));
}
db.close();
Log.d(TAG, "read");
return loc;
}
}
答案 0 :(得分:0)
下面是更新后的代码:
<ModelMapping>
你的bean类:
<ModelMapping>
的活动:
public class GPSDatabase extends SQLiteOpenHelper {
public static final String DB_NAME = "GPSDatabase";
public static final int VERSION = 1;
public static final String TABLE_NAME = "GPSLocation";
public static final String ID_COLUMN = "id";
public static final String NUMBER_COLUMN = "number";
public static final String DISTANCE_COLUMN = "distance";
public static final String LATITUDE_COLUMN = "latitude";
public static final String LONGITUDE_COLUMN = "longitude";
public static final String TAG = "GPSDatabase";
public GPSDatabase(Context context) {
super(context, DB_NAME, null, VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String createQuery =
"CREATE TABLE IF NOT EXISTS " + TABLE_NAME + " (" + ID_COLUMN + " INTEGER PRIMARY KEY, " + NUMBER_COLUMN + " INTEGER DEFAULT NULL, " + DISTANCE_COLUMN + " INTEGER DEFAULT NULL, " + LATITUDE_COLUMN + " REAL DEFAULT NULL, " + LONGITUDE_COLUMN + " REAL DEFAULT NULL)";
db.execSQL(createQuery);
Log.d(TAG, "Table created");
}
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
String dropTable = "DROP TABLE IF EXISTS " + TABLE_NAME;
db.execSQL(dropTable);
Log.d(TAG, "Table deleted");
onCreate(db);
}
public GPSLocation createGpsLocation(GPSLocation loc) {
SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
long newID = -1;
try {
Log.d(TAG, "createGPSLocation begin");
ContentValues values = new ContentValues();
values.put(NUMBER_COLUMN, loc.getNumber());
values.put(DISTANCE_COLUMN, loc.getDistance());
values.put(LATITUDE_COLUMN, loc.getLatitude());
values.put(LONGITUDE_COLUMN, loc.getLongitude());
newID = sqLiteDatabase.insert(TABLE_NAME, null, values);
sqLiteDatabase.close();
} catch (SQLiteException e) {
Log.e(TAG, "insert");
} finally {
Log.d(TAG, "insert with ID " + newID);
}
Log.d(TAG, "createGPSLocation successful");
return loc;
}
public GPSLocation readGPSLocation(final long id) {
SQLiteDatabase db = this.getReadableDatabase();
GPSLocation loc = null;
String query = "SELECT * FROM " + TABLE_NAME + " where id= " + ID_COLUMN;
Cursor cursor = db.rawQuery(query, null);
if (cursor != null && cursor.getCount() > 0) {
cursor.moveToFirst();
do {
loc = new GPSLocation();
loc.setNumber(cursor.getInt(cursor.getColumnIndex(NUMBER_COLUMN)));
loc.setDistance(cursor.getInt(cursor.getColumnIndex(DISTANCE_COLUMN)));
loc.setLatitude(cursor.getDouble(cursor.getColumnIndex(LATITUDE_COLUMN)));
loc.setLongitude(cursor.getDouble(cursor.getColumnIndex(LATITUDE_COLUMN)));
} while (cursor.moveToNext());
db.close();
Log.d(TAG, "read");
}
return loc;
}
}
main.xml中:
public class GPSLocation {
int number, distance;
double latitude, longitude;
public GPSLocation(int number, int distance, double latitude, double longitude) {
this.number = number;
this.distance = distance;
this.latitude = latitude;
this.longitude = longitude;
}
public GPSLocation() {
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public int getDistance() {
return distance;
}
public void setDistance(int distance) {
this.distance = distance;
}
public double getLatitude() {
return latitude;
}
public void setLatitude(double latitude) {
this.latitude = latitude;
}
public double getLongitude() {
return longitude;
}
public void setLongitude(double longitude) {
this.longitude = longitude;
}
}
数据库: