
Problema con base de datos
Publicado por Nestor (3 intervenciones) el 27/02/2016 14:19:36
Hola, estoy intentando desarrollar una app en android utilizando una base de datos en sqlite, la cual ya la tengo poblada, y el problema es que cuando compilo y pruebo la app, Android pareciera no reconocer la db entonces crea una nueva db vacia.
Estoy trabajando con Android Studio, la base de datos la ubique en la siguiente direccion:
y no funciona, probe tambien probe colocarla en esta direccion:
Y tampoco funcionó.
Les dejo el codigo que utilizo.
DatabasesOpenHelper.java
DatabasesAccess.java
Y este es mi activity Estab.java
Estoy trabajando con Android Studio, la base de datos la ubique en la siguiente direccion:
1
C:\Users\Nestor\AndroidStudioProjects\myappE\app\src\main\assets\merlo.db
1
C:\Users\Nestor\AndroidStudioProjects\myapp\app\src\main\assets\databases\merlo.db
Les dejo el codigo que utilizo.
DatabasesOpenHelper.java
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
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class DatabaseOpenHelper extends SQLiteOpenHelper {
private static String DB_NAME = "merlo.db";
private SQLiteDatabase db;
private final Context context;
private String DB_PATH;
public DatabaseOpenHelper(Context context) {
super(context, DB_NAME, null, 1);
this.context = context;
DB_PATH = "/data/data/" + context.getPackageName() + "/" + "databases/";
}
public void createDataBase() throws IOException {
boolean dbExist = checkDataBase();
if (dbExist) {
} else {
this.getReadableDatabase();
try {
copyDataBase();
} catch (IOException e) {
throw new Error("Error copying database");
}
}
}
private boolean checkDataBase() {
File dbFile = new File(DB_PATH + DB_NAME);
return dbFile.exists();
}
private void copyDataBase() throws IOException {
InputStream myInput = context.getAssets().open(DB_NAME);
String outFileName = DB_PATH + DB_NAME;
OutputStream myOutput = new FileOutputStream(outFileName);
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
// Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}
public Cursor getData() {
String myPath = DB_PATH + DB_NAME;
db = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READONLY);
Cursor c = db.rawQuery("SELECT * FROM 071", null);
// Note: Master is the one table in External db. Here we trying to access the records of table from external db.
return c;
}
@Override
public void onCreate(SQLiteDatabase arg0) {
// TODO Auto-generated method stub
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
DatabasesAccess.java
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
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import java.util.ArrayList;
import java.util.List;
public class DatabaseAccess {
private SQLiteOpenHelper openHelper;
private SQLiteDatabase database;
private static DatabaseAccess instance;
public static final String TABLE = "071";
private DatabaseAccess(Context context) {
this.openHelper = new DatabaseOpenHelper(context);
}
/**
* Return a singleton instance of DatabaseAccess.
*
* @param context the Context
* @return the instance of DabaseAccess
*/
public static DatabaseAccess getInstance(Context context) {
if (instance == null) {
instance = new DatabaseAccess(context);
}
return instance;
}
/**
* Open the database connection.
*/
public void open() {
this.database = openHelper.getWritableDatabase();
}
/**
* Close the database connection.
*/
public void close() {
if (database != null) {
this.database.close();
}
}
/**
* Read all quotes from the database.
*
* @return a List of quotes
*/
public List<String> getQuotes() {
List<String> list = new ArrayList<>();
Cursor cursor = database.rawQuery("SELECT cui FROM " + TABLE, null);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
list.add(cursor.getString(0));
cursor.moveToNext();
}
cursor.close();
return list;
}
}
Y este es mi activity Estab.java
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
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
import android.support.v7.app.AppCompatActivity;
import android.widget.Adapter;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import java.io.IOException;
import java.util.List;
public class Estab extends AppCompatActivity {
private String org;
private String loc;
private String estab;
private TextView tV;
private ListView listView;
DatabaseOpenHelper databaseOpenHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_estab);
tV = (TextView) findViewById(R.id.estab_tV);
Bundle bundle = getIntent().getExtras();
org = bundle.getString("txt");
tV.setText(org);
ListView lw = (ListView) findViewById(R.id.lw);
String[] from = new String[] { "_cue", "cui", "name", "street","nro","loc","cp","rpv","tel","mail","ubicacion" };
int[] to = new int[] { R.id.textView1, R.id.textView2,R.id.textView3, R.id.textView4, R.id.textView5, R.id.textView6, R.id.textView7, R.id.textView8, R.id.textView9, R.id.textView10 , R.id.textView11 };
databaseOpenHelper = new DatabaseOpenHelper(this);
try {
databaseOpenHelper.createDataBase();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Cursor c = databaseOpenHelper.getData();
SimpleCursorAdapter adapter = new SimpleCursorAdapter(
getApplicationContext(), R.layout.content_estab, c, from, to);
lw.setAdapter(adapter);
}
}
Valora esta pregunta


0