안드로이드 개발 질문/답변
(글 수 45,052)
public class ScoreDB extends Activity {
private final static String DB_NAME = "score.db";
private final static String DB_TABLE="rps";
private final static int DB_VERSION=1;
private SQLiteDatabase db;
private static int w,l,d;
private static String namestr;
//Initiate
public void onCreate(Bundle SavedInstanceState){
super.onCreate(SavedInstanceState);
//Create DB
DBHelper dbHelper=new DBHelper(this);
db=dbHelper.getWritableDatabase();
}
//Write DataBase
private void writeDB(String Username1, int win, int lose, int draw)throws Exception{
ContentValues values=new ContentValues();
values.put("DB_ID",0);
values.put("USERNAME",Username1);
values.put("WIN", win);
values.put("LOSE",lose);
values.put("DRAW",draw);
long colNum=db.update(DB_TABLE,values,null,null);
if(colNum==0) db.insert(DB_TABLE, "",values);
}
//DB Read
private void readDB() throws Exception{
Cursor c=db.query(DB_TABLE,new String[]{"DB_ID", "USERNAME", "WIN", "LOSE", "DRAW"}, null, null, null, null, null);
if(c.getCount()==0) throw new Exception();
c.moveToFirst();
namestr=c.getString(0);
///////////////////////이부분이 문제 namestr에 값 저장이 안됩니다. ///////////////// Text 값 저장
w=c.getInt(1);
l=c.getInt(2);
d=c.getInt(3);
c.close();
}
//DataBase Helper
private static class DBHelper extends SQLiteOpenHelper{
//DB Helper
public DBHelper (Context context){
super(context, DB_NAME, null, DB_VERSION);
}
//DB Create
public void onCreate(SQLiteDatabase db){
db.execSQL("create table if not exists " + DB_TABLE + "(DB_ID primary key,USERNAME TEXT, WIN INTEGER, LOSE INTEGER, DRAW INTEGER )");
////////////////////DB ( USERNAME(TEXT) || WIN(Int) || LOSE(Int) || DRAW(Int) ) 형식으로 생성
}
//DB Upgrade
public void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion){
db.execSQL("drop table if exists " + DB_TABLE );
onCreate(db);
}//End of onUpgrade
}//End of DBHelper
}//End of Class
DB 열 (UserName, WIN, LOSE, DRAW) 를 만들었는데 이상하게 UserName (TEXT 형식) 부분만 값을 읽어올수가 없습니다.
Log 찍어보면 계속 Null 로 만오는데 Int 값들은 모두 제대로 되고 DB Browser 보면 "User"라고 저장해 놓은것을 확인할수가 있는데.. 읽어오는게 문제여서..화면에 뿌려주면..계속 NULL 로만 나오네요... 제가 함수(getStirng()) 를 잘못 쓴것일까요 ?




namestr=c.getString(0); 0번째 컬럼은 id 부분인 것 같은데요.
db.query(DB_TABLE,newString[]{"DB_ID","USERNAME","WIN","LOSE","DRAW"}, null, null, null, null, null);
이렇게 사용하셨으면 username 컬럼은 인덱스가 1이 됩니다.