2012-04-28 51 views
1

我想知道如何搜索存儲到我的數據庫中的特定信息。比方說,我想搜索存儲在iName中的信息,我會怎麼做?我需要做一些SQL查詢。 對不起,這個基本的問題,但即時通訊新的整個安卓場景。謝謝Android - 如何搜索SQL數據庫中的信息

package f.s.l; 

import android.content.ContentValues; 
import android.content.Context; 
import android.database.Cursor; 
import android.database.SQLException; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteDatabase.CursorFactory; 
import android.database.sqlite.SQLiteOpenHelper; 

public class HotOrNot { 
public static final String KEY_ROWID ="_id"; 
public static final String KEY_NAME ="persons_name"; 
public static final String KEY_HOTNESS ="persons_hotness"; 

private static final String DATABASE_NAME ="HotOrNotdb"; 
private static final String DATABASE_TABLE ="peopleTable"; 
private static final int DATABASE_VERSION =1; 

private DbHelper ourHelper; 
private final Context ourContext; 
private SQLiteDatabase ourDatabase; 

private static class DbHelper extends SQLiteOpenHelper{ 

public DbHelper(Context context) { 
    super(context, DATABASE_NAME, null, DATABASE_VERSION); 
    // TODO Auto-generated constructor stub 
} 

@Override 
public void onCreate(SQLiteDatabase db) { 
    // TODO Auto-generated method stub 
    db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" + 
      KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + 
      KEY_NAME + " TEXT NOT NULL, " + 
      KEY_HOTNESS + " TEXT NOT NULL);" 


      ); 

} 

@Override 
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
    // TODO Auto-generated method stub 
    db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE); 
    onCreate(db); 

} 

} 
public HotOrNot(Context c){ 
ourContext =c; 
} 
public HotOrNot open() throws SQLException{ 
ourHelper = new DbHelper(ourContext); 
ourDatabase = ourHelper.getWritableDatabase(); 
return this; 
} 
public void close(){ 

ourHelper.close(); 
} 
public long createEntry(String name, String hotness) { 
// TODO Auto-generated method stub 
ContentValues cv = new ContentValues(); 
cv.put(KEY_NAME, name); 
cv.put(KEY_HOTNESS, hotness); 
return ourDatabase.insert(DATABASE_TABLE, null, cv); 

} 
public String getData() { 
// TODO Auto-generated method stub 
String [] columns = new String[]{ KEY_ROWID, KEY_NAME, KEY_HOTNESS}; 
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null); 
String result = ""; 

int iRow = c.getColumnIndex(KEY_ROWID); 
int iName = c.getColumnIndex(KEY_NAME); 
int iHotness = c.getColumnIndex(KEY_HOTNESS); 

for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) { 


    result = result + c.getString(iRow) + " "+ c.getString(iName) + " " + c.getString(iHotness) + "\n"; 
} 



return result; 
} 
} 
+0

也許一個教程將是適當的。 – 2012-04-28 20:00:25

回答

2

你需要寫像getData()你已經擁有的功能,但它傳遞的名稱...例如:

public Cursor getData(String name) { 
    ... 
    Cursor c = ourDatabase.query(DATABASE_TABLE, columns, KEY_NAME + "=?", new String[] { name }, null, null, null); 
    ... 
}