
Android Stuido ayuda con base de datos al crear el APK
Publicado por Juan (10 intervenciones) el 14/11/2016 12:33:45
Tengo creada una aplicacion al completo en android studio, que introduce, borra y visualiza datos de una base de datos. Mi problema es que cuando la emulo va perfectamente. Primero sobrescribe la que se crea por una que tiene todo y despues puedo usarla perfectamente, puedo borrar datos, puedo introducir nuevos datos y puedo ver los datos pero cuando genero el APK no sobrescribe la BD digamos tengo una aplicacion perfecta pero con la BD vacia. Es sin sentido. Una ayudita porfavor.
Este es el codigo de mi aplicacion por si ven algun error:
Este es el codigo de mi aplicacion por si ven algun error:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
/*Button actualizar;*/
Button borrar;
Button insertar;
Button consultar;
TextView textView;
EditText editText;
EditText editText2;
SQLiteDatabase db;
@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/*Desde el try hasta el segundo catch sirve para sobrescribir una BD que tengas creada*/
try { String destpath = "/data/data/" + getPackageName() + "/databases/Libros";
File f = new File(destpath);
if (!f.exists()) {
CopyDB(getBaseContext().getAssets().open("Libros2"), new FileOutputStream(destpath));}}
catch (FileNotFoundException e) {e.printStackTrace();}
catch (IOException e) {e.printStackTrace();}
finally{Toast.makeText(this, "Base de datos Sobrescrita", Toast.LENGTH_SHORT).show();}
/*actualizar = (Button) findViewById(R.id.actualizar);*/
borrar = (Button) findViewById(R.id.borrar);
insertar = (Button) findViewById(R.id.insertar);
consultar= (Button) findViewById(R.id.consultar);
textView= (TextView) findViewById(R.id.textView);
editText = (EditText) findViewById(R.id.editText);
editText2 = (EditText) findViewById(R.id.editText2);
/*actualizar.setOnClickListener(this);*/
borrar.setOnClickListener(this);
insertar.setOnClickListener(this);
consultar.setOnClickListener(this);
BDAlumnos libros =
new BDAlumnos (this, "Libros", null, 1);
db = libros.getWritableDatabase();}
@Override
public void onClick(View v){
switch (v.getId()){
/*case R.id.actualizar:
db.execSQL("UPDATE Alumnos SET autor='" + editText.getText() + "' WHERE codigo=5" );
break;*/
case R.id.borrar:
String[] argsbo = new String[]{editText2.getText().toString()};
db.execSQL("DELETE FROM Libros WHERE titulo=?", argsbo);
Toast.makeText(this, "Registro borrado", Toast.LENGTH_SHORT).show();
break;
case R.id.insertar:
db.execSQL("INSERT INTO Libros (autor,titulo)VALUES ('" +editText.getText()+ "','" +editText2.getText()+ "')");
Toast.makeText(this, "Nuevo libro añadido", Toast.LENGTH_SHORT).show();
break;
case R.id.consultar:
String[] args = new String[] {editText.getText().toString()};
Cursor c= db.rawQuery(" SELECT * FROM Libros WHERE autor=? ", args);
if (c.moveToFirst()){
textView.setText("");
do {
String autor= c.getString(1);
String titulo= c.getString(2);
textView.append("Autor: " + autor + " Titulo: " + titulo + "\n");
}while(c.moveToNext());
}
break;
default:
break;
}
}
public void CopyDB(InputStream inputStream,OutputStream outputStream)
throws IOException {
byte[] buffer = new byte[1024];
int length;
while ((length = inputStream.read(buffer))> 0){
outputStream.write(buffer, 0, length);
}
inputStream.close();
outputStream.close();}
Valora esta pregunta


0