He creado una App para consultar la direccion de una persona colocando su nombre en un EditText, para la base de datos he usado el programa BD Browser para SQlite, cree un folder llamado assets y coloque alli el archivo .db. La apliacion abre en el emulador, pero al presionar el boton para buscar me aparece el error "Unfortunaly, My application has stopped" y se cierra. Gracias por cualquier ayuda
Otros datos: El emulador es Genymotion, use un dispositivo generico 5.0.0 API 21.
Los errores del logcat son los siguientes:
12-13 14:30:59.555 2211-2211/? E/libprocessgroup: failed to make and chown
/acct/uid_10059: Read-only file system
12-13 14:31:02.606 2211-2211/com.example.propietario.myapplication
E/SQLiteLog: (1) no such table: Table1
12-13 14:31:02.607 2211-2211/com.example.propietario.myapplication
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.propietario.myapplication, PID: 2211
android.database.sqlite.SQLiteException: no such table: Table1 (code 1): ,
while compiling: Select Address from Table1 where Name =''
at
android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native
Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1316)
at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1255)
at com.example.propietario.myapplication.DataBaseAccess.getAddress(DataBaseAccess.java:48)
at com.example.propietario.myapplication.MainActivity$1.onClick(MainActivity.java:37)
at android.view.View.performClick(View.java:4756)
at android.view.View$PerformClick.run(View.java:19749)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Los Codigos son los siguientes:
MainActivity.java :
package com.example.propietario.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
public EditText name;
public Button query_button;
public TextView result_address;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
name=findViewById(R.id.name);
query_button=findViewById(R.id.query_button);
result_address=findViewById(R.id.result);
//onclicklistener
query_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//instance of database access
DataBaseAccess dataBaseAccess=DataBaseAccess.getInstance(getApplicationContext());
dataBaseAccess.open();
//getting string
String n=name.getText().toString();
String address = dataBaseAccess.getAddress(n);
//set to textview
result_address.setText(address);
dataBaseAccess.close();
//database conection closed
}
});
}
}
DatabaseOpenHelper.java :
package com.example.propietario.myapplication;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DataBaseOpenHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME="MyExternalDatabase.db";
private static final int DATABASE_VERSION=1;
//constructor
public DataBaseOpenHelper(Context context){
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
}
DataBaseAccess.java :
package com.example.propietario.myapplication;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DataBaseAccess {
private SQLiteOpenHelper openHelper;
private SQLiteDatabase db;
private static DataBaseAccess instance;
Cursor c = null;
//private constructor
private DataBaseAccess(Context context){
this.openHelper=new DataBaseOpenHelper(context);
}
//to return
public static DataBaseAccess getInstance(Context context){
if (instance==null){
instance=new DataBaseAccess(context);
}
return instance;
}
//to open database
public void open(){
this.db=openHelper.getWritableDatabase();
}
//close database conetcion
public void close(){
if(db!=null){
this.db.close();
}
}
//method to query
public String getAddress(String name){
c=db.rawQuery("Select Address from Table1 where Name ='"+name+"'", new String[]{});
StringBuffer buffer = new StringBuffer();
while(c.moveToNext()){
String address = c.getString(0);
buffer.append(""+address);
}
return buffer.toString();
}
}
Agregue las lineas que indicaste, pero no estoy seguro de haberlas agregado correctamente porque sigue sin funcionar, estos son los codigos:
DatabaseOpenHelper.java:
package com.example.diego.databasetest2;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import com.readystatesoftware.sqliteasset.SQLiteAssetHelper;
public class DatabaseOpenHelper extends SQLiteAssetHelper {
private static final String
DATABASE_NAME="MyExternaldatabase.db";
private static final int DATABASE_VERSION=1;
public void onCreate(SQLiteDatabase sqLiteDatabase){
sqLiteDatabase.execSQL(DataBase.CREATE_TABLE);
}
//constructor
public DatabaseOpenHelper(Context context){
super(context, DATABASE_NAME,null, DATABASE_VERSION);
}
}
Para poder crear y llamar la base de datos, debo crear otra clase?
table1