【问题标题】:Error when storing a Android marker in SQLite [closed]在 SQLite 中存储 Android 标记时出错 [关闭]
【发布时间】:2017-12-29 09:38:06
【问题描述】:

我正在尝试在 SQLite 数据库中保存位置并在地图上放置标记。 但我收到以下错误:

致命异常:主进程:com.example.android.myfavplaces,PID:2354 java.lang.RuntimeException:无法启动活动 ComponentInfo{com.example.android.myfavplaces/com.example.android.myfavplaces.MapsActivity} : android.database.sqlite.SQLiteException: AUTOINCREMENT 只允许在 INTEGER PRIMARY KEY (code 1): 上,同时编译:创建表位置(loc_idinteger 主键 autoincrement, loc_latdouble, loc_lngdouble,loc_postext);

这是我的代码:

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);
    }


}

【问题讨论】:

    标签: java android sqlite google-maps google-maps-markers


    【解决方案1】:

    在“整数主键”、“双...”和“文本”前加一个空格,如下:

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

    注意 " 符号之后和整数、双精度和文本之前的空格。

    【讨论】:

    • 它解决了我的问题,谢谢
    • @JaveriaA 很高兴为您提供帮助。此外,您可以接受一个答案(通过单击答案左侧的复选标记)将此问题标记为已解决。
    猜你喜欢
    • 1970-01-01
    • 2019-04-19
    • 2015-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-11
    • 1970-01-01
    相关资源
    最近更新 更多