2016-06-13 4 views
-1

WHERE 절에 정수를 사용하여 행을 선택하는 SQL 쿼리를 작성하려면 어떻게해야합니까?WHERE 절을 SELECT 쿼리에 추가하려면 어떻게합니까?

다음은 쿼리를 사용하려는 코드 조각이지만 아래 코드는 작동하지 않습니다 (결과가 반환되지 않음).

public Cursor forEditPurpose(int pos){ 

    db=this.getReadableDatabase(); 
    Cursor res = db.rawQuery("SELECT name,phone FROM mecontact1 where _id = " + pos + "", null); 

    return res; 

} 

이것은 내가 필요한 모든 작업을 수행 내 전체 데이터베이스 코드 :

public class ContactDatabase extends SQLiteOpenHelper { 
    SQLiteDatabase db; 
    public static final String DATABASE_NAME="totalContact1.db"; 
    public static final String TABLE_NAME="mecontact1"; 
    public static final String NAME="name"; 
    public static final String PHONE="phone"; 
    public static final String UID="_id"; 


    public ContactDatabase(Context context) { 
     super(context, DATABASE_NAME, null, 1); 

    } 

    @Override 
    public void onCreate(SQLiteDatabase db) { 
     try { 
      db.execSQL("create table mecontact1" + 
        "(_id integer primary key , name text, phone text)"); 
     }catch(android.database.SQLException e){ 
       System.out.println("table create nhi ho rha"); 
     } 
    } 

    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     db.execSQL("DROP TABLE IF EXISTS mecontact1"); 
     onCreate(db); 
    } 

    public void insertContact(String nam,String mob,int autocrement){ 

     db=this.getWritableDatabase(); 
     ContentValues contentValues=new ContentValues(); 

     contentValues.put(NAME,nam); 
     contentValues.put(PHONE,mob); 
     contentValues.put(UID,autocrement); 

     db.insert(TABLE_NAME, null, contentValues); 
     db.close(); 
    } 

    public Cursor showData(){ 

     db=this.getReadableDatabase(); 
     Cursor res = db.rawQuery("SELECT * FROM mecontact1", null); 
     return res; 

    } 

    public Cursor nameData(String dataName){ 

     db=this.getReadableDatabase(); 
     Cursor res = db.rawQuery("SELECT * FROM mecontact1 WHERE name = '"+dataName+"'", null); 
     return res; 

    } 

    public Cursor phoneData(String dataNumber){ 

     db=this.getReadableDatabase(); 
     Cursor res = db.rawQuery("SELECT * FROM mecontact1 WHERE phone = '"+dataNumber+"'", null); 
     return res; 

    } 

    public Cursor phoneName(){ 

     db=this.getReadableDatabase(); 
     Cursor res = db.rawQuery("SELECT * FROM mecontact1 ", null); 
     return res; 

    } 

    public void deleteContact(String d,int pos){ 
     db=this.getWritableDatabase(); 

     db.execSQL("DELETE FROM "+ TABLE_NAME + " WHERE " + UID + " = " + pos +""); 
     db.execSQL("UPDATE " + TABLE_NAME + " set " + UID + " = (" + UID + "-1) where " + UID + " > " + String.valueOf(pos)); 

    } 

    public Cursor forEditPurpose(int pos){ 

     db=this.getReadableDatabase(); 
     Cursor res = db.rawQuery("SELECT name,phone FROM mecontact1 where _id = " + pos + "", null); 

     return res; 

    } 
} 
+1

** 작업하지 않음 **의 의미는 무엇입니까? 자세한 내용을 알려주십시오! –

+0

@PrerakSola이 쿼리는 어떤 행도 반환하지 않습니다. –

+0

@PrerakSola는이 올바른 쿼리입니까 ?? –

답변

0

당신은 시도 할 수 있습니다;

public Cursor forEditPurpose(int pos){ 

    db=this.getReadableDatabase(); 
    //don't forget the ? parameter in the selection 
    Cursor res = db.query(tableName,new String[]{"name","phone"},"_id =?",new String[]{""+pos},null,null,null); 
    return res; 

    } 
+0

고마워요 쿼리 작업 –

+0

아래로 투표하는 이유는 무엇입니까? –

+0

왜이 ans조차도 투표하는 것은 내 문제를 해결합니다. 제가이 도움을 주셔서 감사합니다. 그리고 Malith Lakshan이 내 투표가 아닙니다. –

0

나는이 전체 예입니다하지만 코드에서 당신은 아마 cursor.moveToFirst()을 누락 질의 결과의 커서를 반환 할 있으리라 믿고있어;

public Cursor getById(int _id){ 
    SQLiteDatabase db = this.getReadableDatabase(); 

    String selectQuery = "SELECT columns_to_return FROM table_name WHERE column_name = " + _id; 
    Cursor c = db.rawQuery(selectQuery, null); 
    c.moveToFirst(); 

    return c; 
} 

추가 설명이 필요하거나 도움을 요청할 경우 도움을 요청하십시오.

+0

@van Marincic은 정확히 형편입니다. –

+0

나는 당신을 도울 수있어서 기뻐요. :). 그건 그렇고 더 나은 연습 커서 이외의 개체를 반환합니다. –

+0

모든 것에 대해 감사드립니다. –

관련 문제