我尝试将数据库中的数据显示为ListView
。 ListView
中的每个项目都有一个位置,我使用int
位置作为DELETE
查询的ID,因为它是相同的。但是,它没有做任何事情。我在按钮点击事件期间调用该方法。这是SQLite数据库类的函数:
public Cursor deleteFromRegSintomasWhereId(long id) {
String sql = "DELETE FROM Reg_sintomas WHERE _ID= ? ; " ;
String[] args = new String[]{id + ""};
Cursor result = this.db.rawQuery(sql, args);
return result;
}
这是我的活动,其中包含ListView
:
package com.example.bugdroid.menuexe.TabFragments;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.example.bugdroid.menuexe.Activities.DetalhesSintoma;
import com.example.bugdroid.menuexe.Activities.VerRegsSintomas;
import com.example.bugdroid.menuexe.CursorAdapter.RegistosCursorAdapter;
import com.example.bugdroid.menuexe.R;
import com.example.bugdroid.menuexe.database.DAL;
import com.example.bugdroid.menuexe.database.DBAccessMode;
public class TabSintomas extends Fragment {
private DAL dal;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final RelativeLayout rootView = (RelativeLayout) inflater.inflate(R.layout.fragment_tab_sintomas, container, false);
dal = new DAL(getActivity());
dal.connect(DBAccessMode.READ);
// Vai usar um cursor para percorrer a base de dados, usando o metodo "selectALLFromRegSintomas()" que vem da classe "DAL".
Cursor cursor = dal.selectALLFromRegSintomas();
// é precisar declarar o layout do fragment com que estamos a trabalhar
final View v = inflater.inflate(R.layout.fragment_tab_sintomas, container, false);
// Declarar a listview onde vao aparecer os dados
ListView lv = (ListView) v.findViewById(R.id.listv);
// Declarar o adapter que define como aparecem os dados na listview
RegistosCursorAdapter todoAdapter = new RegistosCursorAdapter(getActivity(), cursor, 0);
// Ligar o adapter
lv.setAdapter(todoAdapter);
// Nos fragments é preciso sempre fazer return do layout que queremos mostrar, neste caso (fragment_tab_sintomas);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(getActivity(), DetalhesSintoma.class);
intent.putExtra("myExtras", position+1);
startActivity(intent);
//Toast.makeText(getActivity(),"clique",
// Toast.LENGTH_SHORT).show();
}
});
return v;
}
}
这是单击项目时加载的活动:
package com.example.bugdroid.menuexe.Activities;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.example.bugdroid.menuexe.R;
import com.example.bugdroid.menuexe.database.DAL;
import com.example.bugdroid.menuexe.database.DBAccessMode;
import com.example.bugdroid.menuexe.database.DBContract;
import org.w3c.dom.Text;
public class DetalhesSintoma extends AppCompatActivity {
private DAL dal;
private TextView DataInicio;
private EditText HoraIncio;
private TextView DataFim;
private EditText HoraFim;
private TextView Intensidade;
private TextView Motivo;
private TextView Medicacao;
private Button Apagar;
private Button Concluido;
private int position;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detalhes_sintoma);
DataInicio = (TextView) findViewById(R.id.DiaInicioText);
// HoraIncio = (EditText) findViewById(R.id.HoraInicioText);
DataFim = (TextView) findViewById(R.id.DiaFimText);
// HoraFim = () findViewById(R.id.HoraFimText);
Intensidade = (TextView) findViewById(R.id.IntensidadeText);
Motivo = (TextView) findViewById(R.id.MotivoText);
Medicacao = (TextView) findViewById(R.id.MedicacaoText);
Apagar = (Button) findViewById(R.id.btnApagar);
Concluido = (Button) findViewById(R.id.btnConcluido);
Intent intent = getIntent();
position = intent.getIntExtra("myExtras", 0);
dal = new DAL(this);
dal.connect(DBAccessMode.READ);
Cursor res = dal.selectFromRegSintomasWhereId(position);
res.moveToFirst();
StringBuilder datahora = new StringBuilder();
while (!res.isAfterLast()) {
Intensidade.setText(res.getString(res.getColumnIndex(DBContract.Reg_sintomas.COL_Intensidade)));
String Data = res.getString(res.getColumnIndex(DBContract.Reg_sintomas.COL_D_Inicio));
String Hora = res.getString(res.getColumnIndex(DBContract.Reg_sintomas.COL_H_Inicio));
res.moveToNext();
DataInicio.setText(datahora.append(Data + " ás " + Hora));
}
Cursor res1 = dal.selectFromRegSintomasWhereId(position);
res1.moveToFirst();
StringBuilder datahoraF = new StringBuilder();
while (!res1.isAfterLast()) {
String Data = res1.getString(res1.getColumnIndex(DBContract.Reg_sintomas.COL_D_Fim));
String Hora = res1.getString(res1.getColumnIndex(DBContract.Reg_sintomas.COL_H_Fim));
res1.moveToNext();
DataFim.setText(datahoraF.append(Data + " ás " + Hora));
}
Cursor res2 = dal.selectFromMotivoSintomassWhereId(position);
res2.moveToFirst();
StringBuilder sb = new StringBuilder();
while (!res2.isAfterLast()) {
sb.append(res2.getString(res2.getColumnIndex(DBContract.Motivo_sintomas.COL_Motivo)));
res2.moveToNext();
}
Motivo.setText(sb.toString());
Cursor res3 = dal.selectMedicacaoWhereId(position);
res3.moveToFirst();
StringBuilder sb2 = new StringBuilder();
while (!res3.isAfterLast()) {
sb2.append(res3.getString(res3.getColumnIndex(DBContract.Medicacao.COL_Medicacao)));
res3.moveToNext();
}
Medicacao.setText(sb2.toString());
Apagar.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
dal.deleteFromRegSintomasWhereId(position);
Intent intent = new Intent(DetalhesSintoma.this, VerRegsSintomas.class);
startActivity(intent);
}
});
}
}
这是我从上面的代码调用函数的地方:
Apagar.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
dal.deleteFromRegSintomasWhereId(position);
Intent intent = new Intent(DetalhesSintoma.this, VerRegsSintomas.class);
startActivity(intent);
}
});
如何修复此错误?有什么建议吗?
答案 0 :(得分:3)
DELETE
是命令,而不是查询。
您必须使用execSQL()
执行,而不是rawQuery()
。
请注意,execSQL()
不会返回任何内容。
这样可以正常工作:
public void deleteFromRegSintomasWhereId(long id)
{
String sql = "DELETE FROM Reg_sintomas WHERE _ID = ?";
String[] args = new String[]{id};
this.db.execSQL(sql, args);
}