안드로이드 개발 질문/답변
(글 수 45,052)
에러 없이 잘 돌아가는데 뭐가 문제인지 잘 모르겠습니다.분명히 DB파일도 생성되었구요.
일단 c.getCount가 무조건 0이 나오네요 ;;
아~ 그리고 DB를 외부에서 메모장등으로도 열어볼수 있나요?? 시도해봤는데 이상한 언어만 나오드라구요
<메인 액티비티 onCreate안>
this.gdb=new GroupDBHelper(this);
gdb.open();
gdb.open();
gdb.insert("새폴더", item.ISBN);
int a=gdb.getCount("새폴더"); // 여기서 a값이 0이 나오네요 ;; insert가 잘 안되는건지;;
int a=gdb.getCount("새폴더"); // 여기서 a값이 0이 나오네요 ;; insert가 잘 안되는건지;;
<DB 어댑터>
public class GroupDBHelper {
public static final String KEY_NAME="name";
public static final String KEY_ISBN="ISBN";
public static final String KEY_ID="_id";
public static final String DB_NAME="RM_DB.db";
public static final String DB_TABLE="GROUP";
public static final int DB_VERSION=1;
private static final String ClassName=GroupDBHelper.class.getSimpleName();
private static final String[] COLS=new String[]{
KEY_ID,KEY_NAME,KEY_ISBN };
public static final String DB_CREATE="create table "+DB_TABLE+" (_id integer primary key autoincrement,"+
"name text not null, ISBN text not null);";
private SQLiteDatabase db;
private DBOpenHelper dbOpenHelper;
private final Context mCtx;
private class DBOpenHelper extends SQLiteOpenHelper{
public DBOpenHelper(Context context){
super(context,GroupDBHelper.DB_NAME,null,GroupDBHelper.DB_VERSION);
}
public static final String KEY_NAME="name";
public static final String KEY_ISBN="ISBN";
public static final String KEY_ID="_id";
public static final String DB_NAME="RM_DB.db";
public static final String DB_TABLE="GROUP";
public static final int DB_VERSION=1;
private static final String ClassName=GroupDBHelper.class.getSimpleName();
private static final String[] COLS=new String[]{
KEY_ID,KEY_NAME,KEY_ISBN };
public static final String DB_CREATE="create table "+DB_TABLE+" (_id integer primary key autoincrement,"+
"name text not null, ISBN text not null);";
private SQLiteDatabase db;
private DBOpenHelper dbOpenHelper;
private final Context mCtx;
private class DBOpenHelper extends SQLiteOpenHelper{
public DBOpenHelper(Context context){
super(context,GroupDBHelper.DB_NAME,null,GroupDBHelper.DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
try{
db.execSQL(DB_CREATE);
}catch(SQLException e)
{
Log.e("DB",GroupDBHelper.ClassName, e);
}
}
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
try{
db.execSQL(DB_CREATE);
}catch(SQLException e)
{
Log.e("DB",GroupDBHelper.ClassName, e);
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVer, int newVer) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS "+GroupDBHelper.DB_TABLE);
this.onCreate(db);
}
}
// end inner class
public GroupDBHelper(Context context){
this.mCtx=context;
}
public GroupDBHelper open()throws SQLException{
dbOpenHelper=new DBOpenHelper(mCtx);
db=dbOpenHelper.getWritableDatabase();
return this;
}
public void cleanup(){
if(this.db!=null){
this.db.close();
this.db=null;
}
}
public void insert(String name,String ISBN){
ContentValues values=new ContentValues();
values.put(KEY_NAME, name);
values.put(KEY_ISBN,ISBN);
this.db.insert(GroupDBHelper.DB_TABLE, null, values);
}
public void update(long id,String name,String ISBN){
ContentValues values=new ContentValues();
values.put(KEY_NAME, name);
values.put(KEY_ISBN,ISBN);
this.db.update(GroupDBHelper.DB_TABLE, values, KEY_ID+"="+id, null);
}
public void delete(long id){
this.db.delete(GroupDBHelper.DB_TABLE, KEY_ID+"="+id, null);
}
public int getCount(String name){
Cursor c=null;
int count=0;
try{
c=this.db.query(true, GroupDBHelper.DB_TABLE, GroupDBHelper.COLS, KEY_NAME+"="+name,
null, null, null,null,null);
if(c.getCount()>0){
count=c.getCount();
}
}catch(SQLException e){
Log.v("DB", GroupDBHelper.ClassName, e);
}finally{
if(c!=null && !c.isClosed()){
c.close();
}
}
return count;
}
public ArrayList<String> getAllName(){
ArrayList<String> arr=new ArrayList<String> ();
Cursor c=null;
try{
c=this.db.query(GroupDBHelper.DB_TABLE, GroupDBHelper.COLS,
null, null, null,null,null);
int count=c.getCount();
c.moveToFirst();
for(int i=0;i<count;i++)
{
String str=c.getString(1);
if(arr.contains(str)==false){
arr.add(str);
}
c.moveToNext();
}
}catch(SQLException e){
Log.v("DB", GroupDBHelper.ClassName, e);
}finally{
if(c!=null && !c.isClosed()){
c.close();
}
}
return arr;
}
public ArrayList<String> getISBN(String name){
ArrayList<String> arr=new ArrayList<String> ();
Cursor c=null;
try{
c=this.db.query(true, GroupDBHelper.DB_TABLE, GroupDBHelper.COLS, KEY_NAME+"="+name,
null, null, null,null,null);
if(c.getCount()>0){
c.moveToFirst();
int num=c.getCount();
for(int i=0;i<num;i++)
{
if(name==c.getString(1))
{
arr.add(c.getString(2));
}
c.moveToNext();
}
}
}catch(SQLException e){
Log.v("DB", GroupDBHelper.ClassName, e);
}finally{
if(c!=null && !c.isClosed()){
c.close();
}
}
return arr;
}
}
public void onUpgrade(SQLiteDatabase db, int oldVer, int newVer) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS "+GroupDBHelper.DB_TABLE);
this.onCreate(db);
}
}
// end inner class
public GroupDBHelper(Context context){
this.mCtx=context;
}
public GroupDBHelper open()throws SQLException{
dbOpenHelper=new DBOpenHelper(mCtx);
db=dbOpenHelper.getWritableDatabase();
return this;
}
public void cleanup(){
if(this.db!=null){
this.db.close();
this.db=null;
}
}
public void insert(String name,String ISBN){
ContentValues values=new ContentValues();
values.put(KEY_NAME, name);
values.put(KEY_ISBN,ISBN);
this.db.insert(GroupDBHelper.DB_TABLE, null, values);
}
public void update(long id,String name,String ISBN){
ContentValues values=new ContentValues();
values.put(KEY_NAME, name);
values.put(KEY_ISBN,ISBN);
this.db.update(GroupDBHelper.DB_TABLE, values, KEY_ID+"="+id, null);
}
public void delete(long id){
this.db.delete(GroupDBHelper.DB_TABLE, KEY_ID+"="+id, null);
}
public int getCount(String name){
Cursor c=null;
int count=0;
try{
c=this.db.query(true, GroupDBHelper.DB_TABLE, GroupDBHelper.COLS, KEY_NAME+"="+name,
null, null, null,null,null);
if(c.getCount()>0){
count=c.getCount();
}
}catch(SQLException e){
Log.v("DB", GroupDBHelper.ClassName, e);
}finally{
if(c!=null && !c.isClosed()){
c.close();
}
}
return count;
}
public ArrayList<String> getAllName(){
ArrayList<String> arr=new ArrayList<String> ();
Cursor c=null;
try{
c=this.db.query(GroupDBHelper.DB_TABLE, GroupDBHelper.COLS,
null, null, null,null,null);
int count=c.getCount();
c.moveToFirst();
for(int i=0;i<count;i++)
{
String str=c.getString(1);
if(arr.contains(str)==false){
arr.add(str);
}
c.moveToNext();
}
}catch(SQLException e){
Log.v("DB", GroupDBHelper.ClassName, e);
}finally{
if(c!=null && !c.isClosed()){
c.close();
}
}
return arr;
}
public ArrayList<String> getISBN(String name){
ArrayList<String> arr=new ArrayList<String> ();
Cursor c=null;
try{
c=this.db.query(true, GroupDBHelper.DB_TABLE, GroupDBHelper.COLS, KEY_NAME+"="+name,
null, null, null,null,null);
if(c.getCount()>0){
c.moveToFirst();
int num=c.getCount();
for(int i=0;i<num;i++)
{
if(name==c.getString(1))
{
arr.add(c.getString(2));
}
c.moveToNext();
}
}
}catch(SQLException e){
Log.v("DB", GroupDBHelper.ClassName, e);
}finally{
if(c!=null && !c.isClosed()){
c.close();
}
}
return arr;
}
}