在我的应用程序中,我有一个ID_CLI NAME_CLI WHERE_CLI
2345 IGOR LVIV
4566 OLEG KIEV
1234 GIAN REGGIO
,其中包含以下结构和数据
(这只是一个例子)
MainActivity
现在我的EditText
我有一个Button
和一个EditText 2345
,所以如果我输入Button
,我会按TextView
我那将在2345 IGOR LVIV
中打印public class DataBaseHandler extends SQLiteOpenHelper {
private static DataBaseHandler sInstance;
private static final String DATABASE_NAME = "Clienti.db";
private static final String TABLE_CLIENTS = "clienti_table";
private static final String ID_CLI = "ID_CLI";
private static final String COD_CLI = "COD_CLI";
private static final String RS1_CLI = "RS1_CLI";
public static synchronized DataBaseHandler getInstance(Context context) {
// Per inizializzare il db in altre activities inserire :
// DataBaseHandler myDB;
// myDB = DataBaseHandler.getInstance(this);
if (sInstance == null) {
sInstance = new DataBaseHandler(context.getApplicationContext());
}
return sInstance;
}
private DataBaseHandler(Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_TABLE_CLIENTS = "CREATE TABLE IF NOT EXISTS " + TABLE_CLIENTS + " (ID_CLI TEXT,COD_CLI TEXT,RS1 TEXT)";
db.execSQL(CREATE_TABLE_CLIENTS);
}
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_CLIENTS);
onCreate(db);
}
public void insertCLI(String ID,String COD,String RS1){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(ID_CLI,ID);
values.put(COD_CLI,COD);
values.put(RS1_CLI,RS1);
db.insertWithOnConflict(TABLE_CLIENTI, null, values, SQLiteDatabase.CONFLICT_REPLACE);
}
public Cursor getDataTable()
{
SQLiteDatabase db = this.getWritableDatabase();
return db.rawQuery("SELECT * FROM " + TABLE_CLIENTS);
}
}
实际上我还在网上搜索过类似的东西,但还没找到任何可以解决我问题的东西。
您对我该怎么办有任何建议吗?
这是我的数据库
public override void OnCreate()
{
// your code
base.OnCreate();
}
答案 0 :(得分:1)
在数据库处理程序类
中添加此方法public static String dbRawSearch(String id) {
String dbString = "";
SQLiteDatabase db = this.getWritableDatabase();
String query = "SELECT * FROM " + TABLE_CLIENTS + " WHERE ID_CLI = " + id";
//Cursor points to a location in your results
Cursor c = db.rawQuery(query, null);
//Move to the first row in your results
c.moveToFirst();
//Position after the last row means the end of the results
while (!c.isAfterLast()) {
if (c.getString(c.getColumnIndex("ID_CLI")) != null) {
dbString += c.getString(c.getColumnIndex("ID_CLI")) + " " + c.getString(c.getColumnIndex("NAME_CLI")) + " " + c.getString(c.getColumnIndex("WHERE_CLI"));
dbString += "\n";
}
c.moveToNext();
}
db.close();
return dbString;
}
然后转到您的活动,在初始化editText,button和textView之后,执行以下操作:
DataBaseHandler dbHandler = new DataBaseHandler(this);
String searchKeyword = "";
String result = "";
yourButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(!TextUtils.isEmpty(yourEditText.getText().toString())) {
searchKeyWord = yourEditText.getText().toString();
result = dbHandler.dbRawSearch(searchKeyWord );
if(result != null){
yourTextView.setText(result);
}
}
}
});
答案 1 :(得分:0)
在DataBaseHandler
班级
public Cursor getRowByIdCli(String idCli){
SQLiteDatabase db = this.getWritableDatabase();
return db.rawQuery("SELECT * FROM " + TABLE_CLIENTS + " WHERE ID_CLI = ? ", new String[]{idCli} );
}