2017-07-23 3 views
-1

Ich versuche, Speicherort in SQLite db zu speichern und Markierungen auf Karte zu setzen. Aber ich bekomme die folgenden Fehler:Fehler beim Speichern eines Android-Markers in SQLite

FATAL EXCEPTION: main Process: com.example.android.myfavplaces, PID: 2354 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.myfavplaces/com.example.android.myfavplaces.MapsActivity}: android.database.sqlite.SQLiteException: AUTOINCREMENT is only allowed on an INTEGER PRIMARY KEY (code 1): , while compiling: create table locations(loc_idinteger primary key autoincrement, loc_latdouble , loc_lngdouble ,loc_postext);

Hier ist mein Code:

public class LocationsSQLHelper extends SQLiteOpenHelper { 

    private SQLiteDatabase mDB; 
    public static final String DATABASE_NAME = "MyDBName.db"; 
    private static final String TABLE_NAME="locations"; 

    private static final String FIELD_ROW_ID="loc_id"; 
    private static final String FIELD_LAT="loc_lat"; 
    private static final String FIELD_LNG="loc_lng"; 
    private static final String FIELD_ZOOM="loc_pos"; 

    private static final int D_VERSION=1; 

    private static final String DB_NAME="markerlocations.db"; 
    private static final String DB_CREATE="create table "+TABLE_NAME+ "(" 
          +FIELD_ROW_ID + "integer primary key autoincrement, " 
          +FIELD_LAT + "double , " 
          +FIELD_LNG + "double ," 
          +FIELD_ZOOM + "text" 
          +");" 
        ; 


     public LocationsSQLHelper(Context context) { 
     super(context, DB_NAME, null, D_VERSION); 

     } 

    @Override 
     public void onCreate(SQLiteDatabase db) { 
     db.execSQL(DB_CREATE); 
    } 


    /** Inserts a new location to the table locations */ 
    public boolean insert(Integer id, Double lat, Double lon, String zoom) { 
    SQLiteDatabase db=this.getWritableDatabase(); 
     ContentValues contentValues=new ContentValues(); 
     contentValues.put("id", id); 
     contentValues.put("lat", 12.944400); 
     contentValues.put("lon", 75.785966); 
     contentValues.put(zoom,"zoom"); 
     return true; 
    } 

    /** Deletes all locations from the table */ 
    public long del(Integer id){ 
     SQLiteDatabase db = this.getWritableDatabase(); 
     return db.delete("locations", 
       "id = ? ", 
       new String[] { Long.toString(id) }); 
    } 

    public ArrayList<String> getAllLocations(){ 
    ArrayList<String> arrayList=new ArrayList<>(); 
      SQLiteDatabase db=this.getReadableDatabase(); 
     Cursor res=db.rawQuery("select * from locations", null); 
     res.moveToFirst(); 
     while(res.isAfterLast() == false){ 
      arrayList.add(res.getString(res.getColumnIndex(FIELD_ROW_ID))); 
      res.moveToNext(); 
     } 
     return arrayList; 
    } 

    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     db.execSQL("DROP TABLE IF EXISTS" + TABLE_NAME); 
     onCreate(db); 
    } 


} 

Antwort

1

einen Raum hinzufügen, bevor "integer Primärschlüssel", "double ..." und "Text" wie folgt:

+FIELD_ROW_ID + " integer primary key autoincrement, " +FIELD_LAT + " double , " +FIELD_LNG + " double ," +FIELD_ZOOM + " text" +");" 

Notieren Sie sich den Raum nach dem "Zeichen und vor integer, Doppel-und Text.

+0

Es ist mein Problem behoben, danke – javsadi

+0

@JaveriaA Ich bin froh, dir zu helfen. Sie können auch eine Antwort akzeptieren (indem Sie auf das Häkchen auf der linken Seite der Antwort klicken), um diese Frage als gelöst zu markieren. – andrei