Hyejin Han - OpensoureIsHope/OpensourceIsHope GitHub Wiki
-ํํ์ง์ Wiki Page์ ๋๋ค-
2016-11-17
- ํ์ฌ local์ cloneํด ์จ ์ํ์
- ๋ฐ์ดํฐ๋ฒ ์ด์ค ์์ฑ
๋ฐ์ดํฐ๋ฒ ์ด์ค์๋ ๋ด๋ถ์ ์ฅ์, ์ธ๋ถ์ ์ฐ๊ฒฐ์ด ๊ฐ๋ฅํฉ๋๋ค.
- ๋ด๋ถ์ ์ฅ์ ์๋๋ก์ด๋ ํ๋ก์ ํธ๋ฅผ ์งํํ๋ค ๋ณด๋ฉด 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;
}
}