0

部屋番号とその座標をデータベースに挿入し、それらにアクセスしてそれらの部屋への道順を表示しようとしていますが、データベースへのデータの挿入に失敗しているようです。

これが私のデータベースハンドラーです:

public class MRLDatabaseHandler extends SQLiteOpenHelper {



    //private Context context;
    //private String locationsFile;


    private static final int DATABASE_VERSION = 1;

    private static final String DATABASE_NAME = "MRL_LocationsDB3";

    static final String TABLE_LOCATIONS = "locations"; //table name

    private static final String KEY_ID = "_GPSPoint_ID";
    private static final String KEY_RNUMBER = "_RNumber";
    private static final String KEY_LAT = "_Latitude";
    private static final String KEY_LONGI = "_Longitude";
    private static final String KEY_ALTI = "_Altitude";


    public MRLDatabaseHandler(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        //this.context = context;
        //locationsFile = context.getString(R.string.mrl_locations);
    }


    @Override
    public void onCreate(SQLiteDatabase db) {
        String CREATE_LOCATIONS_TABLE = "CREATE TABLE " + TABLE_LOCATIONS + "("
                + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_RNUMBER + " TEXT," + KEY_LAT + " DOUBLE,"
                + KEY_LONGI + " DOUBLE,"  + KEY_ALTI + " DOUBLE"+ ")";
        db.execSQL(CREATE_LOCATIONS_TABLE);

        //populating the locations table
        /**MRL_LocationsLoader locationLoader = new MRL_LocationsLoader(context, db, locationsFile);
            try {
                locationLoader.loadData();

            }catch (IOException e){
                e.printStackTrace();
                } ***/


    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

        db.execSQL("DROP TABLE IF EXISTS " + TABLE_LOCATIONS);

        // Create tables again
        onCreate(db);

    }

//////CRUD IMPLEMETATION
    // Adding new coordinate
        void addCoordinate( MRL_LocationsModelClass point) {
            SQLiteDatabase db = this.getWritableDatabase();

            ContentValues values = new ContentValues();
            values.put(KEY_ID , point.get_GPSPoint_ID());
            values.put(KEY_RNUMBER, point.get_RNumber()); 
            values.put(KEY_LAT, point.get_Latitude()); 
            values.put(KEY_LONGI, point.get_Longitude()); 
            values.put(KEY_ALTI, point.get_Altitude()); 


            // Inserting Row
            db.insert(TABLE_LOCATIONS, null, values);
            db.close(); // Closing database connection
        }

    // Getting single coordinate from SQLite database
    MRL_LocationsModelClass getPoint(int id) {
        SQLiteDatabase db = this.getReadableDatabase();

        Cursor cursor = db.query(TABLE_LOCATIONS, new String[] { KEY_ID,
                KEY_RNUMBER, KEY_LAT, KEY_LONGI, KEY_ALTI }, KEY_ID + "=?",
                new String[] { String.valueOf(id) }, null, null, null, null);
        if (cursor != null)
            cursor.moveToFirst();

        MRL_LocationsModelClass locationsModelClass = new MRL_LocationsModelClass(
                Integer.parseInt(cursor.getString(0)), cursor.getString(1),
                Double.parseDouble(cursor.getString(2)),
                Double.parseDouble(cursor.getString(3)),
                Double.parseDouble(cursor.getString(4)));
        // return contact
        return locationsModelClass;

    }

これを使用するクラスは次のとおりです。

public class DrawPath extends Activity {
     MRLDatabaseHandler db = new MRLDatabaseHandler(this);
    int dest = 9, src = 6;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Inserting Coordinates
                Log.d("Insert: ", "Inserting ..");
                db.addCoordinate(new MRL_LocationsModelClass("L101", -12.804646,28.240218,  (double) 1223));
                db.addCoordinate(new MRL_LocationsModelClass("C102", -12.8048,28.240736, (double) 1227));
                db.addCoordinate(new MRL_LocationsModelClass("Z103", -12.805274,28.240529, (double) 1225));
                db.addCoordinate(new MRL_LocationsModelClass("A104", -12.805347,28.238821, (double) 1232));
                db.addCoordinate(new MRL_LocationsModelClass("NH201",-12.804497,28.24068, (double) 1222));



        MRL_LocationsModelClass srcPoint = db.getPoint(src);
        MRL_LocationsModelClass destPoint = db.getPoint(dest);
        SharedData data = SharedData.getInstance();
        data.setAPIKEY("0Fm62I15vXD_pVpF8WXdTK0ooUd2Wm8rxsT3Kvg");
        data.setSrc_lat(srcPoint.get_Latitude());
        data.setSrc_lng(srcPoint.get_Longitude());
        data.setDest_lat(destPoint.get_Latitude());
        data.setDest_lng(destPoint.get_Longitude());
        startActivity(new Intent(DrawPath.this,RoutePath.class)); 

    }



}

そして、ここに私のlogcatがあります:

03-13 05:19:26.337: D/Insert:(618): Inserting ..
03-13 05:19:30.940: E/Database(618): Error inserting _Longitude=28.240736 _RNumber=C102 _Altitude=1227.0 _Latitude=-12.8048 _GPSPoint_ID=0
03-13 05:19:30.940: E/Database(618): android.database.sqlite.SQLiteConstraintException: error code 19: constraint failed
03-13 05:19:30.940: E/Database(618):    at android.database.sqlite.SQLiteStatement.native_execute(Native Method)
03-13 05:19:30.940: E/Database(618):    at android.database.sqlite.SQLiteStatement.execute(SQLiteStatement.java:55)
03-13 05:19:30.940: E/Database(618):    at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1549)
03-13 05:19:30.940: E/Database(618):    at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1410)
03-13 05:19:30.940: E/Database(618):    at com.agarwal.route.MRLDatabaseHandler.addCoordinate(MRLDatabaseHandler.java:84)
03-13 05:19:30.940: E/Database(618):    at com.agarwal.route.DrawPath.onCreate(DrawPath.java:20)
03-13 05:19:30.940: E/Database(618):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-13 05:19:30.940: E/Database(618):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
03-13 05:19:30.940: E/Database(618):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
03-13 05:19:30.940: E/Database(618):    at android.app.ActivityThread.access$2300(ActivityThread.java:125)
03-13 05:19:30.940: E/Database(618):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
03-13 05:19:30.940: E/Database(618):    at android.os.Handler.dispatchMessage(Handler.java:99)
03-13 05:19:30.940: E/Database(618):    at android.os.Looper.loop(Looper.java:123)
03-13 05:19:30.940: E/Database(618):    at android.app.ActivityThread.main(ActivityThread.java:4627)
03-13 05:19:30.940: E/Database(618):    at java.lang.reflect.Method.invokeNative(Native Method)
03-13 05:19:30.940: E/Database(618):    at java.lang.reflect.Method.invoke(Method.java:521)
03-13 05:19:30.940: E/Database(618):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-13 05:19:30.940: E/Database(618):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-13 05:19:30.940: E/Database(618):    at dalvik.system.NativeStart.main(Native Method)
4

1 に答える 1

2

INTEGER PRIMARY KEY列に値 0 を何度も追加しようとしています。これにより、制約エラーが発生します。

これを修正するには、次の行を から削除しますaddCoordinate()

values.put(KEY_ID , point.get_GPSPoint_ID());

自動生成された id 値を取得するには、次の戻り値を取得しますinsert()

long id = db.insert(TABLE_LOCATIONS, null, values);
point.set_GPSPoint_ID(id);

(古い質問ですが、編集によってぶつかりました。)

于 2014-02-28T06:43:11.937 に答える