データベースでプライマリーキーを日付と自動連番のIDにしているのですが、同じ日付のデータを入れても連番が0からあがりません。日付データの隠れた秒数の部分があって邪魔しているのかと思い、下のコードで試してみても連番が上がらず、解決できないのでよろしければご助言お願いいたします。
Java
1日付処理 2 3//部品の取得 4sDate = (EditText) findViewById(R.id.date); 5 6//今日の日付を初期入力 7Date time = new Date(); 8time.getTime(); 9SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd", Locale.JAPAN); 10Date d = truncateTime(time); 11String today = format.format(d); 12sDate.setText(today);
Java
1日付の秒数を合わせるメソッド 2 3static Date truncateTime(Date d) { 4 Instant instant = d.toInstant(); 5 ZonedDateTime zonedDateTime = instant.atZone(ZoneId.of("Asia/Tokyo", ZoneId.SHORT_IDS)); 6 ZonedDateTime truncated = zonedDateTime.truncatedTo(ChronoUnit.DAYS); 7 return Date.from(truncated.toInstant()); 8 }
Java
1Insert文とそこにつながるコード 2 3final int price = Integer.parseInt(mPrice.getText().toString()); 4final String category = (String) gCategory.getSelectedItem(); 5final String memo = gMemo.getText().toString(); 6final String date = gDate.getText().toString(); 7insertData(db, price, category, memo, date); 8 9public void insertData(SQLiteDatabase db, int price, String category, String memo, String date) { 10 11 ContentValues values = new ContentValues(); 12 values.put("date", date); 13 values.put("memo", memo); 14 values.put("category", category); 15 values.put("price", price); 16 17 db.insert("kakeibodb", null, values); 18 }
Java
1データベース作成 2 3// データーベースのバージョン 4private static final int DATABASE_VERSION = 1; 5 6// データーベース名 7private static final String DATABASE_NAME = "KakeiboDB.db"; 8private static final String TABLE_NAME = "kakeibodb"; 9private static final String PRIMARY_KEY = "date"; 10private static final String COLUMN_CATEGORY = "category"; 11private static final String COLUMN_PRICE = "price"; 12private static final String COLUMN_MEMO = "memo"; 13private static final String _ID = "_id"; 14 15 16private KakeiboDatabase kDB; 17private SQLiteDatabase db; 18 19 20 21private static final String SQL_DELETE_ENTRIES = 22"DROP TABLE IF EXISTS " + TABLE_NAME; 23 24 25KakeiboDatabase(Context context) { 26super(context, DATABASE_NAME, null, DATABASE_VERSION); 27 28} 29 30@Override 31public void onCreate(SQLiteDatabase db) { 32 33 // テーブル作成 34 // SQLiteファイルがなければSQLiteファイルが作成される 35 db.execSQL( 36 "CREATE TABLE " + TABLE_NAME + " (" + 37 _ID + " INTEGER," + 38 PRIMARY_KEY + " TEXT," + 39 COLUMN_CATEGORY + " TEXT," + 40 COLUMN_PRICE + " INTEGER," + 41 COLUMN_MEMO + " TEXT," + 42 "PRIMARY KEY(" + _ID + "," + PRIMARY_KEY + "))" 43 ); 44 45 Log.d("debug", "onCreate(SQLiteDatabase db)"); 46 47} 48 49@Override 50public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 51// アップデートの判別 52 db.execSQL( 53 SQL_DELETE_ENTRIES 54 ); 55 onCreate(db); 56} 57 58public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) { 59 onUpgrade(db, oldVersion, newVersion); 60}
Java
1idをどうくらべているか 2Cursor cursor = db.query( 3 "kakeibodb", 4 new String[] {"category", "memo", "price", "_id"}, 5 "date=?", 6 new String[] {date}, 7 null, 8 null, 9 null); 10 boolean next = cursor.moveToFirst(); 11 12while (next) { 13 Daily daily = new Daily(); 14 daily.setCategory(cursor.getString(0));// categoryを取得 15 daily.setMemo(cursor.getString(1));// memoを取得 16 daily.setPrice(String.valueOf(cursor.getInt(2)));// priceを取得 17 daily.set_id(cursor.getInt(3));//IDを取得 18 19 // 次の行が存在するか確認 20 next = cursor.moveToNext(); 21 dataset.add(daily); 22 } 23 24↓ 25 26public class Daily { 27 long id; 28 private String category; 29 private String memo; 30 private String price; 31 private int _id; 32 33 34~中略~ 35 36 public int get_id() { 37 return _id; 38 } 39 40 public void set_id(int _id) { 41 this._id = _id; 42 } 43} 44 45↓ 46dailyListに格納して一覧表示のうち選択された行のポジションのIdを取得、Intentによる受け渡し 47int _id = dailyList.get(position).get_id(); 48Intent intent = new Intent(getApplication(), Delete.class); 49intent.putExtra("ID", _id); 50startActivity(intent); 51↓ 52Delete Class 53 54int _id = intent.getIntExtra("ID", 0); 55 56kDB = new KakeiboDatabase(getApplicationContext()); 57 SQLiteDatabase db = kDB.getReadableDatabase(); 58 59 String category, memo; 60 String price = ""; 61 Cursor cursor = db.query( 62 "kakeibodb", 63 new String[]{"category", "memo", "price", "_id"}, 64 "date=?", 65 new String[]{date}, 66 null, 67 null, 68 null); 69 boolean next = cursor.moveToFirst(); 70 71while (next) { 72 if (_id == cursor.getInt(3)) { 73 ~中略~ 74 } 75 // 次の行が存在するか確認 76 next = cursor.moveToNext(); 77 } 78 cursor.close(); 79 80 81
回答1件
あなたの回答
tips
プレビュー