GyuhoLee

[Add] DataBase(SQLite)

......@@ -21,6 +21,7 @@
</activity>
<activity android:name = ".ChoiceActivity"/>
<activity android:name = ".ExerciseActivity"/>
<activity android:name = ".ReportActivity"/>
</application>
</manifest>
\ No newline at end of file
......
package com.khuhacker.pocketgym;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DBHelper extends SQLiteOpenHelper {
//database 파일을 생성
public DBHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}
//실행할 때 DB 최초 생성
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE reports (pushup INTEGER, squat INTEGER, date TEXT);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS reports");
onCreate(db);
}
}
\ No newline at end of file
......@@ -8,6 +8,8 @@ import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.content.res.AssetFileDescriptor;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
......@@ -35,6 +37,8 @@ import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.Arrays;
public class ExerciseActivity extends AppCompatActivity implements SurfaceHolder.Callback, ImageReader.OnImageAvailableListener {
......@@ -51,12 +55,20 @@ public class ExerciseActivity extends AppCompatActivity implements SurfaceHolder
private int count = 0;
private boolean up = true;
private TextView countView;
DBHelper dbHelper;
String getTime;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_exercise);
dbHelper = new DBHelper(this, "reports.db", null, 1);
long now = System.currentTimeMillis();
Date date = new Date(now);
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
getTime = sdf.format(date);
surfaceView = findViewById(R.id.surfaceView);
surfaceView.getHolder().addCallback(this);
......@@ -294,6 +306,16 @@ public class ExerciseActivity extends AppCompatActivity implements SurfaceHolder
if(!up) {
count++;
countView.setText("Count: " + count);
//db에 값 증가
SQLiteDatabase db = dbHelper.getReadableDatabase();
String sql = "SELECT pushup FROM reports WHERE date=" + getTime + ";";
Cursor cs = db.rawQuery(sql, null);
int before = cs.getInt(0);
db = dbHelper.getWritableDatabase();
sql = "UPDATE reports SET pushup='" + before + 1 + "' WHERE date=" + getTime + ";";
db.execSQL(sql);
}
up = true;
......
......@@ -3,15 +3,42 @@ package com.khuhacker.pocketgym;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import java.sql.Date;
import java.text.SimpleDateFormat;
public class FirstActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
DBHelper dbHelper = new DBHelper(this, "reports.db", null, 1);
//오늘 포함 7일 DB가 생성되지 않은 경우, 생성
long day = 3600000 * 24;
SQLiteDatabase db;
String sql;
for(int i = 0; i < 7; i++) {
long now = System.currentTimeMillis() - (day * i);
Date date = new Date(now);
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
String getTime = sdf.format(date);
db = dbHelper.getReadableDatabase();
sql = "SELECT * FROM reports WHERE date=" + getTime + ";";
Cursor cursor = db.rawQuery(sql, null);
if (cursor.getCount() == 0) {
sql = String.format("INSERT INTO reports VALUES('0','0','" + getTime + "');");
db.execSQL(sql);
}
}
}
public void onClick(View view) {
......@@ -20,7 +47,11 @@ public class FirstActivity extends AppCompatActivity {
startActivity(intent);
}
else if(view.getId() == R.id.reportBtn){
Intent intent = new Intent(this, ReportActivity.class);
startActivity(intent);
}
}
}
......
package com.khuhacker.pocketgym;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import androidx.appcompat.app.AppCompatActivity;
import java.sql.Date;
import java.text.SimpleDateFormat;
public class ReportActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_choice);
}
}