๋ชจ๋์ฝ๋ฉ์ฑ์ ๋ง๋ค๋ฉด์ ์ฌ์ฉ์์ ๋๋ค์, ์ด๋ฉ์ผ, ์ ํํ ์ธ์ด, ์ฆ๊ฒจ์ฐพ๋ ๋ฌธ์ ๋ฅผ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ ์ฅํด์ผ ํ ์ผ์ด ์๊ฒผ๋ค. ๊ฐ๊ฐ์ ์ฌ์ฉ์๋ง๋ค ์ ํํ ์ธ์ด์ ๋ฌธ์ ๋ค์ด ๋ค๋ฅด๊ธฐ ๋๋ฌธ์ด๋ค.
์ฒ์์๋ 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
+) ์ถ๊ฐ
//๋ฐ์ดํฐ ์์
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);
}
//==============================================
'ANDROID' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[์๋๋ก์ด๋] ์นด์นด์ค ๋ก๊ทธ์ธ ์ฐ๋ (0) | 2020.08.03 |
---|---|
[์๋๋ก์ด๋] intent ์ฌ๋ฌ ๊ฐ์ ๊ฐ ๋๊ธฐ๋ ๋ฐฉ๋ฒ (0) | 2020.08.03 |
[์๋๋ก์ด๋] SharedPreference ์ฌ์ฉ๋ฒ (ํ์ฌ ๋ฐ์ดํฐ ์ ์ฅ/์ฑ ์ค์ ๋ง๋ค ๋ ์ ์ฉ) (0) | 2020.08.02 |
[์๋๋ก์ด๋] ListView ์ฌ์ฉ๋ฒ (0) | 2020.08.02 |
[์๋๋ก์ด๋] ๋ก๊ทธ์ธ ์ฐ๋ HashKey ์ป๋ ๋ฐฉ๋ฒ (0) | 2020.08.02 |