일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
- 짝사랑
- 가식의 껍데기
- rsnapshot
- 도전의 기회
- 커피레이크
- nmtui
- COC
- 퇴근버스 슈퍼스타K 2016
- 김영근 올패스
- 퇴근버스 조민욱
- 센티멘탈
- 사랑 그렇게 보내네 김영근
- 그게 무슨 의미가 있니
- 지리산 소울 김영근
- 넬 희망고문
- 흘러가는대로
- 해맑게 웃는 너
- i7-8700
- HttpURLConnection
- 내 자신의 생각
- account ban
- 흐린날의 낮술
- 영구정지
- 적절한 타이밍
- #만취
- 3rd party software
- 보고싶은 내맘
- 슈퍼스타k 2016
- CentOS 6 설치 오류
- 질투
- Today
- Total
끄적거림들...
Android DB 사용하기 예제 본문
Android 에서 DB 를 사용하는 예제 (name / contents 2개의 column)
public class AndroidDBHelper extends SQLiteOpenHelper{
public AndroidDBHelper(Context context) {
super(context, "android.db3", null,1);
}
@Override
public void onCreate(SQLiteDatabase db) {
String query="create table androidtable(name VARCHAR(30),contents VARCHAR(100))";
db.execSQL(query);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
public class AndroidDB {
SQLiteDatabase db;
Context context;
public AndroidDB(Context context){
AndroidDBHelper dbhelper= new AndroidDBHelper(context);
this.db=dbhelper.getWritableDatabase();
}
public void insertExample(String name, String contents){
String query="insert into androidtable (name,contents) VALUES ('"+name+"','"+contents+"')";
try{
db.execSQL(query);
}catch(Exception e){
e.printStackTrace();
}
db.close();
}
public void deleteExample(name){
String query="Delete From androidtable Where name = '"+name+"';
try{
db.execSQL(query);
}catch(Exception e){
e.printStackTrace();
}
db.close();
}
public void updateExample(String name, String contents){
String query = "update androidtable set name='"+name+"' , contents='"+contents+"' where name = "+name+";
try{
db.execSQL(query);
}catch(Exception e){
e.printStackTrace();
}
db.close();
}
public HashMap<String, String> selectExample(){
HashMap<String, String> map= new HashMap<String, String>();
String sql="select contents from androidtable";
Cursor c=db.rawQuery(query, null);
if( c.moveToFirst() ){
do{
map.put(c.getString(0),"");
}while( c.moveToNext());
}
c.close();
db.close();
return map;
}
}
사용할때
HashMap<String, String> map;
AndroidDB db= new AndroidDB(this);
map = db.selectExample();
'Androiddd' 카테고리의 다른 글
Android VersionCode VersionName 확인하기 (0) | 2016.09.05 |
---|---|
Android Network 상태 확인하기 (0) | 2016.09.05 |
FTP 파일 업로드하기 (FTPClient) (0) | 2016.09.05 |
HttpURLConnection 예제 (0) | 2016.09.05 |
HttpClient vs HttpURLConnection (0) | 2016.09.05 |