2016-06-13 67 views
-1

如何編寫SQL查詢以在WHERE子句中使用整數來選擇行?如何將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

@Prera​​kSola此查詢不返回任何行 –

+0

@Prera​​kSola是這個正確的查詢? –

回答

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

爲什麼米給你下來投票,即使這個答案是解決我的問題.again我說感謝這個幫助。和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正好bro –

+0

我很高興我可以幫助你:)。順便說一下,更好的做法是返回除遊標以外的對象。 –

+0

感謝你的每一件事 –