songining

๋ชจ๋‹์ฝ”๋”ฉ์•ฑ์„ ๋งŒ๋“ค๋ฉด์„œ ์‚ฌ์šฉ์ž์˜ ๋‹‰๋„ค์ž„, ์ด๋ฉ”์ผ, ์„ ํƒํ•œ ์–ธ์–ด, ์ฆ๊ฒจ์ฐพ๋Š” ๋ฌธ์ œ๋ฅผ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์ €์žฅํ•ด์•ผ ํ•  ์ผ์ด ์ƒ๊ฒผ๋‹ค.  ๊ฐ๊ฐ์˜ ์‚ฌ์šฉ์ž๋งˆ๋‹ค ์„ ํƒํ•œ ์–ธ์–ด์™€ ๋ฌธ์ œ๋“ค์ด ๋‹ค๋ฅด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. 

์ฒ˜์Œ์—๋Š” php๋ฅผ ์ค‘๊ฐ„์— ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋‚˜ ์ƒ๊ฐํ•ด์„œ XAMPP๋ฅผ ๊น”๊ณ  ๋‚œ๋ฆฌ๋ฅผ ํ”ผ์› ๋Š”๋ฐ ๊ฒฐ๊ตญ sqlite API๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ๊ฐ€์žฅ ์ข‹์€ ์„ ํƒ์ด์—ˆ๋‹ค.  SQLiteOpenHelper๋ฅผ ์ƒ์†๋ฐ›๋Š” DBHelper๋ผ๋Š” ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค๊ณ  ์•ˆ๋“œ๋กœ์ด๋“œ developer๊ฐ€์ด๋“œ์— ๋”ฐ๋ผ ๋ฉ”์†Œ๋“œ๋“ค์„ ๊ฐ๊ฐ ๊ตฌํ˜„ํ•˜์˜€๋‹ค. ์œ„์˜ ๋ฉ”์†Œ๋“œ๋“ค์€ ์›ํ•˜๋Š” ์‹คํ–‰๋ถ€๋ถ„์—์„œ ๊ฐ๊ฐ ํ˜ธ์ถœํ•˜์—ฌ ์‚ฌ์šฉํ•ด์ฃผ๋ฉด ๋œ๋‹ค. 

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }
}


class DBHelper extends SQLiteOpenHelper{

    public static final String DATABASE_NAME = "Morning-Coding";
    public static final String TABLE_NAME= "userdata";
    public static final String COL_1="ID";
    public static final String COL_2="name";
    public static final String COL_3="email";
    public static final String COL_4="language";
    public static final String COL_5="number";

    public DBHelper(@Nullable Context context){
        super(context,DATABASE_NAME,null,2);

    }

    //์‹คํ–‰ํ•  ๋•Œ ํ…Œ์ด๋ธ” ์ตœ์ดˆ ์ƒ์„ฑ
    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table " +TABLE_NAME+"(ID INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT , email TEXT, language TEXT, number TEXT)");
    }

    //๋ฒ„์ „ ์—…๊ทธ๋ ˆ์ด๋“œ
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
        onCreate(db);
    }

    //ํ…Œ์ด๋ธ”์— ์ •๋ณด ๋„ฃ๊ธฐ
    public boolean insertData(String name, String email, String language, String number){
        SQLiteDatabase db= this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(COL_2,name);
        contentValues.put(COL_3,email);
        contentValues.put(COL_4,language);
        contentValues.put(COL_5,number);

        long result = db.insert(TABLE_NAME,null,contentValues);

        if(result == -1){
            return false;
        }
        else{
            return true;
        }
    }

    //์„ ํƒํ•œ ๋ฐ์ดํ„ฐ๋งŒ ์กฐํšŒ
    public Cursor getOneData(String email){
        SQLiteDatabase db= this.getWritableDatabase();
        Cursor res = db.rawQuery("select * from "+ TABLE_NAME+ " where email='"+email+"'",null);
        return res;
    }

    //๋ฐ์ดํ„ฐ ์ „์ฒด ์กฐํšŒ
    public Cursor getAllData(){
        SQLiteDatabase db= this.getWritableDatabase();
        Cursor res = db.rawQuery("select * from "+ TABLE_NAME ,null);
        return res;
    }

    //๋ฐ์ดํ„ฐ ์ˆ˜์ •
    public boolean updateData(String id,String name,String email,String language,String number){
        SQLiteDatabase db= this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(COL_1,id);
        contentValues.put(COL_2,name);
        contentValues.put(COL_3,email);
        contentValues.put(COL_4,language);
        contentValues.put(COL_5,number);
        db.update(TABLE_NAME,contentValues,"ID=?",new String[]{id});
        return true;
    }
    //ํ…Œ์ด๋ธ” ์‚ญ์ œ
    public int deleteData(String id){
        SQLiteDatabase db = this.getWritableDatabase();
        return db.delete(TABLE_NAME,"ID=?",new String[] {id});
    }


}

 

์ฐธ๊ณ : https://medium.com/@eevee300/android-sqlite-%EC%82%AC%EC%9A%A9%ED%95%98%EA%B8%B0-94d17f06123

 

Android SQLite ์‚ฌ์šฉํ•˜๊ธฐ

Android ์—์„œ๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ๋ชจ๋ฐ”์ผ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋กœ SQLite๋ฅผ ์ œ๊ณตํ•œ๋‹ค. ๊ธฐ์กด์—๋Š” SQLite(SQLiteOpenHelper)๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜์˜€๋‹ค. ํ•˜์ง€๋งŒ 2017๋…„ Google I/O ์—์„œ Android…

medium.com

+) ์ถ”๊ฐ€ 

 

   //๋ฐ์ดํ„ฐ ์ˆ˜์ •
    public void updateLanguage(String email, String language) {
        ContentValues contentValues = new ContentValues();
        SQLiteDatabase db = this.getWritableDatabase();
        contentValues.put("language",language);
        db.update("userData",contentValues,"email=?",new String[] {email});

    }
    public void updateSolved(String email,String solvedProblem){
        ContentValues contentValues = new ContentValues();
        SQLiteDatabase db = this.getWritableDatabase();
        contentValues.put("solvedProblem",solvedProblem);
        db.update("userData",contentValues,"email=?",new String[] {email});
    }
    public void updateCorrect(String email,String correctProblem){
        ContentValues contentValues = new ContentValues();
        SQLiteDatabase db = this.getWritableDatabase();
        contentValues.put("correct",correctProblem);
        db.update("userData",contentValues,"email=?",new String[] {email});
    }
    //์ปฌ๋Ÿผ์ถ”๊ฐ€
    public void addColumn(){
        SQLiteDatabase db = this.getWritableDatabase();
        String str="alter table userData add column correct text";
        db.execSQL(str);
    }

 

//์ปค์„œ ์•Œ๋งž์€ ์œ„์น˜๋กœ ์˜ฎ๊ธฐ๊ธฐ 

 //===================DB=====================
        // db์— ๋ฐ์ดํ„ฐ ์žˆ๋Š”์ง€ ๊ฒ€์‚ฌ
        cursor=dbHelper().getAllData();
        while (cursor.moveToNext()) {
            if (email.equals(cursor.getString(2))) {
                found = true;
                break;
            }
        }
        //db์— ์—†์œผ๋ฉด ๋ฐ์ดํ„ฐ ์ถ”๊ฐ€
        if (found == false) {
            cursor.moveToFirst();
            favouriteProblem = null;
            solvedProblem = null;
            correctProblem=null;
            language="JAVA PYTHON C";
            dbHelper().insertData(name, email, language, favouriteProblem,solvedProblem,correctProblem);
            cursor = dbHelper().getOneData(email);
        }
        //==============================================