-5
I don’t have much programming experience and I’m having trouble with a BD query. It’s only returning the last data entered. I need to list all records entered in this table.
@Override
public void onCreate(SQLiteDatabase db) {
String table_altura = "CREATE TABLE "+TABLE1+"(altura_id INTEGER PRIMARY KEY AUTOINCREMENT,altura_data TEXT," +
"altura TEXT)";
String table_perimetro = "CREATE TABLE "+TABLE2+"(perimetro_id INTEGER PRIMARY KEY AUTOINCREMENT,perimetro_data TEXT," +
"perimetro TEXT)";
String table_peso = "CREATE TABLE "+TABLE3+"(peso_id INTEGER PRIMARY KEY AUTOINCREMENT,peso_data TEXT,peso TEXT)";
String table_vacina = "CREATE TABLE "+TABLE4+"(vacina_id INTEGER PRIMARY KEY AUTOINCREMENT,vacina_data TEXT," +
"vacina TEXT)";
String table_doc = "CREATE TABLE "+TABLE5+"(doc_id INTEGER PRIMARY KEY AUTOINCREMENT,num_cpf TEXT,num_sus TEXT," +
"num_convenio TEXT,num_rg TEXT)";
String table_dados_bebe = "CREATE TABLE "+TABLE6+"(dados_bebe_id INTEGER PRIMARY KEY AUTOINCREMENT,nome_bebe TEXT,sobrenome_bebe TEXT," +
"data_nasc_bebe TEXT,tipo_sangue TEXT,naturalidade TEXT,maternidade TEXT,hora_nasc TEXT,tipo_parto TEXT,genero TEXT, foto_bebe TEXT)";
db.execSQL(table_altura);
db.execSQL(table_perimetro);
db.execSQL(table_peso);
db.execSQL(table_vacina);
db.execSQL(table_doc);
db.execSQL(table_dados_bebe);
}
__________________________________________________________________________________________________
void addVacina(String vacina_data, String vacina){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put("vacina_data", vacina_data);
cv.put("vacina", vacina);
long result = db.insert(TABLE4,null, cv);
if(result == -1){
Toast.makeText(context, "Erro.", Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(context, "Adicionado com sucesso!", Toast.LENGTH_SHORT).show();
}
}
__________________________________________________________________________________________________
Cursor readAllDataVacina(){
String query = "SELECT * FROM " + TABLE4;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = null;
if(db != null){
cursor = db.rawQuery(query, null);
}
return cursor;
}
__________________________________________________________________________________________________
public class VacinaActivity extends AppCompatActivity {
MyDatabaseHelper myDB = new MyDatabaseHelper(this);
private TextView vac_bcg, vac_bcg_data, vac_hepb, vac_hepb_data;
private String data_bcg, data_hepb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().hide();
setContentView(R.layout.vacina_activity);
vac_bcg = findViewById(R.id.tvBcg);
vac_bcg_data = findViewById(R.id.tvDataBcg);
vac_hepb = findViewById(R.id.tvHepatiteB);
vac_hepb_data = findViewById(R.id.tvDataHepatiteB);
Cursor cursor = myDB.readAllDataVacina();
if(cursor.getCount() != 0) {
while (cursor.moveToNext()) {
vac_bcg_data.setText(data_bcg = cursor.getString(1));
vac_hepb_data.setText(data_hepb = cursor.getString(1));
}
}
}
Please clarify your specific problem or provide Additional Details to Highlight Exactly what you need. As it’s Currently Written, it’s hard to Tell Exactly what you’re asking.
–