我有一个表(类别),它有两列(_id,name)。
我有一个显示列名称的微调器。
现在,我需要将列:“_ id”插入到另一个表中......但我不知道该怎么做。
有什么建议吗?
我粘贴我正在使用的代码:
public Cursor recuperaCategoria()
{
final UsuariosSQLiteHelper usdbh =new UsuariosSQLiteHelper(this, "DBLlistaCompra", null, 1);
final SQLiteDatabase db = usdbh.getWritableDatabase();
String tableName = "Categorias";
String[] columns = {"_id","Nombre"};
return db.query(tableName, columns, null, null, null, null, null);
}
public void recCatSpinner() {
final Spinner addCatSpinner = (Spinner) findViewById(R.id.spIdCategoria);
catCursor = recuperaCategoria();
catCursor.moveToPosition(1);
catAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item);
addCatSpinner.setAdapter(catAdapter);
if (catCursor.moveToFirst()) {
do {
catAdapter.add(catCursor.getString(1));
Valor = catCursor.getString(1);
Log.v("valor Add Cat 100", Valor);
}
while (catCursor.moveToNext());
int id = catCursor.getInt(0);
String name = catCursor.getString(1);
Log.v("Dentro cursor","1");
if (db != null) {
Toast.makeText(getBaseContext(),catCursor.getString(0),Toast.LENGTH_SHORT).show();
db.close();
}
}
startManagingCursor(catCursor);
//catCursor.close();
addCatSpinner.setOnItemSelectedListener(
new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent,
View view2, int pos, long id) {
//recID();
valor = parent.getItemAtPosition(pos).toString();
Log.v("valor Add Cat", valor);
}
public void onNothingSelected(AdapterView<?> parent) {
// view.setText("nada");
}
});
catCursor.close();
}
当我从微调器中选择一个项目(它显示“Nombre”列)时,我想这样,我将光标“_id”中的第一列保存到一个变量中。
答案 0 :(得分:1)
将值读取到集合,然后编写脚本以将其插入另一个taBLE
SQLiteDatabase db = getReadableDatabase();
String[] columns = { _id };
String selection = "column_name" + "=?";
String orderBy = "_id ASC";
String[] selectionArgs = new String[] { "something" };
String groupBy = null;
String having = null;
Cursor cursor = db.query("table_name", "columns", selection, selectionArgs, groupBy, having, orderBy);
while(cursor.moveToNext()) {
int id = cursor.getInt(0);
String name = cursor.getString(1);
// Add values to a collection
}
if(db.isOpen()) db.close();
SQLiteDatabase db = getWritableDatabase();
for(int id : idCollection) {
ContentValues values = new ContentValues();
values.put("_id", id);
try {
int id = db.insertOrThrow(TABLE_NAME, null, values);
}
catch (SQLException sqlEx) {
// do something with exception
}
}
if(db.isOpen()) db.close();