GyuhoLee

[Add] DataBase(SQLite)

...@@ -21,6 +21,7 @@ ...@@ -21,6 +21,7 @@
21 </activity> 21 </activity>
22 <activity android:name = ".ChoiceActivity"/> 22 <activity android:name = ".ChoiceActivity"/>
23 <activity android:name = ".ExerciseActivity"/> 23 <activity android:name = ".ExerciseActivity"/>
24 + <activity android:name = ".ReportActivity"/>
24 </application> 25 </application>
25 26
26 </manifest> 27 </manifest>
...\ No newline at end of file ...\ No newline at end of file
......
1 +package com.khuhacker.pocketgym;
2 +
3 +import android.content.Context;
4 +import android.database.sqlite.SQLiteDatabase;
5 +import android.database.sqlite.SQLiteOpenHelper;
6 +
7 +public class DBHelper extends SQLiteOpenHelper {
8 +
9 + //database 파일을 생성
10 + public DBHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
11 + super(context, name, factory, version);
12 + }
13 +
14 + //실행할 때 DB 최초 생성
15 + @Override
16 + public void onCreate(SQLiteDatabase db) {
17 + db.execSQL("CREATE TABLE reports (pushup INTEGER, squat INTEGER, date TEXT);");
18 + }
19 +
20 + @Override
21 + public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
22 + db.execSQL("DROP TABLE IF EXISTS reports");
23 + onCreate(db);
24 + }
25 +}
...\ No newline at end of file ...\ No newline at end of file
...@@ -8,6 +8,8 @@ import android.Manifest; ...@@ -8,6 +8,8 @@ import android.Manifest;
8 import android.content.Context; 8 import android.content.Context;
9 import android.content.pm.PackageManager; 9 import android.content.pm.PackageManager;
10 import android.content.res.AssetFileDescriptor; 10 import android.content.res.AssetFileDescriptor;
11 +import android.database.Cursor;
12 +import android.database.sqlite.SQLiteDatabase;
11 import android.graphics.Bitmap; 13 import android.graphics.Bitmap;
12 import android.graphics.Canvas; 14 import android.graphics.Canvas;
13 import android.graphics.Color; 15 import android.graphics.Color;
...@@ -35,6 +37,8 @@ import java.io.FileInputStream; ...@@ -35,6 +37,8 @@ import java.io.FileInputStream;
35 import java.io.IOException; 37 import java.io.IOException;
36 import java.nio.ByteBuffer; 38 import java.nio.ByteBuffer;
37 import java.nio.channels.FileChannel; 39 import java.nio.channels.FileChannel;
40 +import java.sql.Date;
41 +import java.text.SimpleDateFormat;
38 import java.util.Arrays; 42 import java.util.Arrays;
39 43
40 public class ExerciseActivity extends AppCompatActivity implements SurfaceHolder.Callback, ImageReader.OnImageAvailableListener { 44 public class ExerciseActivity extends AppCompatActivity implements SurfaceHolder.Callback, ImageReader.OnImageAvailableListener {
...@@ -51,12 +55,20 @@ public class ExerciseActivity extends AppCompatActivity implements SurfaceHolder ...@@ -51,12 +55,20 @@ public class ExerciseActivity extends AppCompatActivity implements SurfaceHolder
51 private int count = 0; 55 private int count = 0;
52 private boolean up = true; 56 private boolean up = true;
53 private TextView countView; 57 private TextView countView;
58 + DBHelper dbHelper;
59 + String getTime;
54 60
55 @Override 61 @Override
56 protected void onCreate(Bundle savedInstanceState) { 62 protected void onCreate(Bundle savedInstanceState) {
57 super.onCreate(savedInstanceState); 63 super.onCreate(savedInstanceState);
58 setContentView(R.layout.activity_exercise); 64 setContentView(R.layout.activity_exercise);
59 65
66 + dbHelper = new DBHelper(this, "reports.db", null, 1);
67 + long now = System.currentTimeMillis();
68 + Date date = new Date(now);
69 + SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
70 + getTime = sdf.format(date);
71 +
60 surfaceView = findViewById(R.id.surfaceView); 72 surfaceView = findViewById(R.id.surfaceView);
61 surfaceView.getHolder().addCallback(this); 73 surfaceView.getHolder().addCallback(this);
62 74
...@@ -294,6 +306,16 @@ public class ExerciseActivity extends AppCompatActivity implements SurfaceHolder ...@@ -294,6 +306,16 @@ public class ExerciseActivity extends AppCompatActivity implements SurfaceHolder
294 if(!up) { 306 if(!up) {
295 count++; 307 count++;
296 countView.setText("Count: " + count); 308 countView.setText("Count: " + count);
309 +
310 + //db에 값 증가
311 + SQLiteDatabase db = dbHelper.getReadableDatabase();
312 + String sql = "SELECT pushup FROM reports WHERE date=" + getTime + ";";
313 + Cursor cs = db.rawQuery(sql, null);
314 + int before = cs.getInt(0);
315 +
316 + db = dbHelper.getWritableDatabase();
317 + sql = "UPDATE reports SET pushup='" + before + 1 + "' WHERE date=" + getTime + ";";
318 + db.execSQL(sql);
297 } 319 }
298 320
299 up = true; 321 up = true;
......
...@@ -3,15 +3,42 @@ package com.khuhacker.pocketgym; ...@@ -3,15 +3,42 @@ package com.khuhacker.pocketgym;
3 import androidx.appcompat.app.AppCompatActivity; 3 import androidx.appcompat.app.AppCompatActivity;
4 4
5 import android.content.Intent; 5 import android.content.Intent;
6 +import android.database.Cursor;
7 +import android.database.sqlite.SQLiteDatabase;
6 import android.os.Bundle; 8 import android.os.Bundle;
9 +import android.util.Log;
7 import android.view.View; 10 import android.view.View;
8 11
12 +import java.sql.Date;
13 +import java.text.SimpleDateFormat;
14 +
9 public class FirstActivity extends AppCompatActivity { 15 public class FirstActivity extends AppCompatActivity {
10 16
11 @Override 17 @Override
12 protected void onCreate(Bundle savedInstanceState) { 18 protected void onCreate(Bundle savedInstanceState) {
13 super.onCreate(savedInstanceState); 19 super.onCreate(savedInstanceState);
14 setContentView(R.layout.activity_first); 20 setContentView(R.layout.activity_first);
21 +
22 + DBHelper dbHelper = new DBHelper(this, "reports.db", null, 1);
23 +
24 +
25 + //오늘 포함 7일 DB가 생성되지 않은 경우, 생성
26 + long day = 3600000 * 24;
27 + SQLiteDatabase db;
28 + String sql;
29 + for(int i = 0; i < 7; i++) {
30 + long now = System.currentTimeMillis() - (day * i);
31 + Date date = new Date(now);
32 + SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
33 + String getTime = sdf.format(date);
34 + db = dbHelper.getReadableDatabase();
35 + sql = "SELECT * FROM reports WHERE date=" + getTime + ";";
36 + Cursor cursor = db.rawQuery(sql, null);
37 + if (cursor.getCount() == 0) {
38 + sql = String.format("INSERT INTO reports VALUES('0','0','" + getTime + "');");
39 + db.execSQL(sql);
40 + }
41 + }
15 } 42 }
16 43
17 public void onClick(View view) { 44 public void onClick(View view) {
...@@ -20,7 +47,11 @@ public class FirstActivity extends AppCompatActivity { ...@@ -20,7 +47,11 @@ public class FirstActivity extends AppCompatActivity {
20 startActivity(intent); 47 startActivity(intent);
21 } 48 }
22 else if(view.getId() == R.id.reportBtn){ 49 else if(view.getId() == R.id.reportBtn){
23 - 50 + Intent intent = new Intent(this, ReportActivity.class);
51 + startActivity(intent);
24 } 52 }
25 } 53 }
26 } 54 }
55 +
56 +
57 +
......
1 +package com.khuhacker.pocketgym;
2 +
3 +import android.database.sqlite.SQLiteDatabase;
4 +import android.os.Bundle;
5 +import android.util.Log;
6 +
7 +import androidx.appcompat.app.AppCompatActivity;
8 +
9 +import java.sql.Date;
10 +import java.text.SimpleDateFormat;
11 +
12 +public class ReportActivity extends AppCompatActivity {
13 +
14 + @Override
15 + protected void onCreate(Bundle savedInstanceState) {
16 + super.onCreate(savedInstanceState);
17 + setContentView(R.layout.activity_choice);
18 + }
19 +
20 +}