Hyejin Han - OpensoureIsHope/OpensourceIsHope GitHub Wiki

-ํ•œํ˜œ์ง„์˜ Wiki Page์ž…๋‹ˆ๋‹ค-

2016-11-17

  1. ํ˜„์žฌ local์— cloneํ•ด ์˜จ ์ƒํƒœ์ž„

๋กœ๊ทธ์ธ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ถ”๊ฐ€

  1. ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ƒ์„ฑ

๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์—๋Š” ๋‚ด๋ถ€์ €์žฅ์†Œ, ์™ธ๋ถ€์™€ ์—ฐ๊ฒฐ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

  • ๋‚ด๋ถ€์ €์žฅ์†Œ ์•ˆ๋“œ๋กœ์ด๋“œ ํ”„๋กœ์ ํŠธ๋ฅผ ์ง„ํ–‰ํ•˜๋‹ค ๋ณด๋ฉด DATA๋ฅผ ๋ณด๊ด€ํ•˜๊ณ  ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜๋Š” ๊ฒฝ์šฐ์—์‚ฌ์šฉ ex)ํšŒ์›๊ฐ€์ž…์„ ํ†ตํ•ด ํšŒ์›์ •๋ณด๋ฅผ ์ €์žฅํ• ๋•Œ

์•ˆ๋“œ๋กœ์ด๋“œ๋Š” SQLiteDatabase๋ผ๋Š” DATABASE ๋ฅผ ์ œ๊ณต ๋งŒ์•ฝ DATA๋ฅผ ์ €์žฅํ•ด์„œ ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์ƒ๊ธธ ๊ฒฝ์šฐ SQLiteDatabase ์™€ DatabaseHelper(DB์ƒ์„ฑ ๋ฐ ๊ด€๋ฆฌ๋ฅผ ๋„์™€์คŒ) -> ์ด๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์“ธ ์ˆ˜ ์žˆ์Œ


1. ํ…Œ์ด๋ธ” ๋งŒ๋“ค๊ธฐ

public class Databases { public static final class CreateDB implements BaseColumns { //public static final String _ID = "ID"; public static final String ID = "id"; public static final String NAME = "name"; public static final String IMAGE = "image"; public static final String PWD = "pwd"; public static final String MAJOR = "major"; public static final String GENDER = "gender"; public static final String _TABLENAME = "memberinfo"; // id name number time image public static final String _s = "create table "+_TABLENAME+"(" +_ID+" integer primary key autoincrement, " +ID+" text , " +PWD+" text , " +NAME+" text , " +MAJOR+" text , " +GENDER+" text , " +IMAGE+" text );"; } }

**2. ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๊ด€๋ฆฌํ•˜๋Š” DatabaseHelper ์ƒ์„ฑ ** - ์ด๊ณณ์—์„œ๋Š” ํ…Œ์ด๋ธ” ์‚ฝ์ž…/์‚ญ์ œ/์ˆ˜์ •/์กฐํšŒํ•˜๋Š” ๋ฉ”์†Œ๋“œ๋ฅผ ์ž‘์„ฑ

public class DbOpenHelper { public static final String DATABASE_NAME = "ossteam.db"; public static final int DATABASE_VERSION = 1; public static SQLiteDatabase mDB; private DatabaseHelper mDBHelper; private Context mCtx;

private ArrayList<ItemData> itemDatas = null;



public class DatabaseHelper extends SQLiteOpenHelper {

    private SQLiteDatabase mDB;

    // ์ƒ์„ฑ์ž
    public DatabaseHelper(Context context, String name,  CursorFactory factory, int version) {

        super(context, name, factory, version);
        getReadableDatabase();
    }

    // ์ตœ์ดˆ DB๋ฅผ ๋งŒ๋“ค๋•Œ ํ•œ๋ฒˆ๋งŒ ํ˜ธ์ถœ๋œ๋‹ค.
    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(Databases.CreateDB._s);
        open();
        this.mDB = mDB;

    }

    // ๋ฒ„์ „์ด ์—…๋ฐ์ดํŠธ ๋˜์—ˆ์„ ๊ฒฝ์šฐ DB๋ฅผ ๋‹ค์‹œ ๋งŒ๋“ค์–ด ์ค€๋‹ค.
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS "+ Databases.CreateDB._TABLENAME);
        onCreate(db);
    }
}

public DbOpenHelper(Context context){
    this.mCtx = context;
}

public DbOpenHelper open() throws SQLException {
    mDBHelper = new DatabaseHelper(mCtx, DATABASE_NAME, null, DATABASE_VERSION);
    mDBHelper.getWritableDatabase();
    return this;
}


// id name number check time image
public void DbInsertJoin(ItemData itemDatas){
    String query = "select * from memberinfo";

    open();

    mDBHelper.getWritableDatabase();
    ContentValues values = new ContentValues();
    System.out.println("์ฒดํฌ์šฉ์šฉ์šฉ1");

    Cursor c1 = mDB.rawQuery(query, null);
    int count = c1.getCount();
    values.put("ID", count);
    values.put("id", itemDatas.getId());
    values.put("pwd", itemDatas.getPwd());
    values.put("name", itemDatas.getName());
    values.put("major", itemDatas.getMajor());
    values.put("gender", itemDatas.getGender());
    values.put("image", itemDatas.getImg());

    System.out.println("์ฒดํฌ์šฉ์šฉ์šฉ2");

    mDB.insert("memberinfo", null, values);
    System.out.println("์ฒดํฌ์šฉ์šฉ์šฉ3");
    mDB.close();

}

/**
 * DBํ•ญ๋ชฉ ์—…๊ทธ๋ ˆ์ด๋“œ - ์ˆ˜์ •ํ•  ๋•Œ ์‚ฌ์šฉ
 * @param id
 * @param pwd
 * @param major
 * @param name
 * @param gender
 * @param img

public void DbUpdate(String id, String pwd, String name, String major ,String gender, String img){

    ContentValues values = new ContentValues();
    values.put("name",name);
    values.put("number",number);
    values.put("time",time);
    values.put("image",img);

    mDB.update("devices", values, "_id=?", new String[]{id});

}

*/

/**
 * ํ•ญ๋ชฉ ์‚ญ์ œํ•˜๋Š” ํ•จ์ˆ˜
 * @param id
 */
public void DbDeleteJoin(String id) {

    mDB.delete("memberinfo", "_id=?", new String[]{id});

}

//๋กœ๊ทธ์ธ์„ ์œ„ํ•ด ์ฒดํฌํ•˜๋Š” ํ•จ์ˆ˜ public String DbSelectUser(String id){ System.out.println("์ฒดํฌ์šฉ์šฉ์šฉ4"); open();

    String SQL = "select id, pwd from memberinfo";
    String a, b;
    b = "์ฐพ์ง€๋ชปํ•˜์˜€์Šต๋‹ˆ๋‹ค.";
       mDB = mDBHelper.getReadableDatabase();
        Cursor c1 = mDB.rawQuery(SQL, null);
    System.out.println("์ฒดํฌ์šฉ์šฉ์šฉ5");

    if (c1.moveToFirst()) {
            do {
                a = c1.getString(0);
                b = c1.getString(1);

                if (a.equals(id)) {
                    b = c1.getString(1);
                    break;
                }
            }
            while (c1.moveToNext());
        }
    System.out.println("์ฒดํฌ์šฉ์šฉ์šฉ6");

    return b;
}
/**
 * devicesํ…Œ์ด๋ธ”์— ์ €์žฅ๋˜์–ด์žˆ๋Š” ๊ฐ’๋“ค์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜ - ๋ฆฌ์ŠคํŠธ๋ทฐ ๋ฟŒ๋ฆด ๋•Œ ํ˜ธ์ถœ
 * @return
 */
public void DbSelectJoin() {
    mDB = mDBHelper.getReadableDatabase();
    Cursor c = mDB.rawQuery("select * from memberinfo", null);

// Log.i("dbtest" , "๊ฐฏ์ˆ˜ : " + String.valueOf(c.getCount()));

    int recordCount = c.getCount();
    println("cursor count : " + recordCount + "\n");
    itemDatas = new ArrayList<ItemData>();
    ItemData listViewItem = new ItemData();
    for (int i = 0; i < recordCount; i++) {
        c.moveToNext();

        String _id = c.getString(0);
        String id = c.getString(1);
        String pwd = c.getString(2);
        String name = c.getString(3);
        String major = c.getString(4);
        String gender = c.getString(5);
        println(name + id + pwd + major + gender + _id);

        listViewItem.id=id;
        listViewItem.name = name;
        listViewItem.pwd = pwd;
        listViewItem.major = major;
        listViewItem.gender = gender;

        listViewItem.name = name;
        itemDatas.add(listViewItem);
    }
    c.close();

} public void close(){ mDB.close(); }

}

3. ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์žˆ๋Š” ํ•„๋“œ๋ฅผ ๊ฐ€์ ธ๋‹ค ์“ธ ์ˆ˜ ์žˆ๋Š” itemdata ์ƒ์„ฑ

public class ItemData {

public int ID;
public String id;
public String name;
public String major;
public String gender;

public void setPwd(String pwd) {
    this.pwd = pwd;
}

public void setID(int ID) {
    this.ID = ID;
}

public void setId(String id) {
    this.id = id;
}

public void setName(String name) {
    this.name = name;
}

public void setMajor(String major) {
    this.major = major;
}

public void setGender(String gender) {
    this.gender = gender;
}

public void setImg(String img) {
    this.img = img;
}

public String pwd;
public String img;

public String getImg() {return img;}

public int getID() {
    return ID;
}

public String getId() {
    return id;
}

public String getMajor() {
    return major;
}

public String getGender() {
    return gender;
}

public String getPwd() {
    return pwd;
}

public String getName() {
    return name;
}

}

โš ๏ธ **GitHub.com Fallback** โš ๏ธ