samedi 11 avril 2015

Having trouble connecting to existing database in android

I have this existing database Called HealthySizing that I would like to use in my android app, it has six columns in a table named Shirts: Name, Brand, Price, Store, Size, and Description. Right now, I just want to see if I can at least connect to my database and write a query to retrieve just one column. I put my database Called HealthySizing into the assets folder and I have a DBHelper.java class but I just cannot figure out what's wrong. So below here's the error that I got and also I've posted my MainActivity.java, DBHelper.java, and the activity_main.xml files. If anyone could help me figure out how to finally get my database connection to work so that I can retrieve the data, that would be very helpful.





package com.example.ListViewFromSQLiteDB;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;

public class DBHelper extends SQLiteOpenHelper{

public SQLiteDatabase DB;
public String DBPath;
public static String DBName = "HealthySizing";
public static final int version = '3';
public static Context currentContext;
public static String tableName = "Shirts";


public DBHelper(Context context) {
super(context, DBName, null, version);
currentContext = context;
DBPath = "/data/data/" + context.getPackageName() + "/databases";
createDatabase();

}

@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub

}

private void createDatabase() {
boolean dbExists = checkDbExists();

if (dbExists) {
// do nothing
}




}

private boolean checkDbExists() {
SQLiteDatabase checkDB = null;

try {
String myPath = DBPath + DBName;
checkDB = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READONLY);

} catch (SQLiteException e) {

// database doesn't exist yet.

}

if (checkDB != null) {

checkDB.close();

}

return checkDB != null ? true : false;
}

}



package com.example.ListViewFromSQLiteDB;

import java.util.ArrayList;
import android.app.ListActivity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class MainActivity extends ListActivity {

private ArrayList<String> results = new ArrayList<String>();
private String tableName = DBHelper.tableName;
private SQLiteDatabase newDB;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
openAndQueryDatabase();

displayResultList();


}
private void displayResultList() {
TextView tView = new TextView(this);
//tView.setText("This data is retrieved from the database and only 4 " +
// "of the results are displayed");
getListView().addHeaderView(tView);

setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, results));
getListView().setTextFilterEnabled(true);

}
private void openAndQueryDatabase() {
try {
DBHelper dbHelper = new DBHelper(this.getApplicationContext());
newDB = dbHelper.getWritableDatabase();

Cursor c = newDB.rawQuery("SELECT Brand FROM " +
tableName, null);

if (c != null ) {
if (c.moveToFirst()) {
do {
String brand = c.getString(c.getColumnIndex("Brand"));
results.add(brand);

}while (c.moveToNext());
}
}
} catch (SQLiteException se ) {
Log.e(getClass().getSimpleName(), "Could not create or Open the database");
} finally {
if (newDB != null)
newDB.execSQL("DELETE FROM " + tableName);
newDB.close();
}

}

}



04-11 14:19:23.095 2341-2341/com.example.ListViewFromSQLiteDB E/SQLiteLog﹕ (14) cannot open file at line 30046 of [9491ba7d73]
04-11 14:19:23.095 2341-2341/com.example.ListViewFromSQLiteDB E/SQLiteLog﹕ (14) os_unix.c:30046: (2) open(/data/data/com.example.ListViewFromSQLiteDB/databasesHealthySizing.db) -
04-11 14:19:23.108 2341-2341/com.example.ListViewFromSQLiteDB E/SQLiteDatabase﹕ Failed to open database '/data/data/com.example.ListViewFromSQLiteDB/databasesHealthySizing.db'.
android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method)
at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:209)
at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:193)
at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463)
at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185)
at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177)
at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:806)
at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:791)
at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:694)
at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:669)
at com.example.ListViewFromSQLiteDB.DBHelper.checkDbExists(DBHelper.java:82)
at com.example.ListViewFromSQLiteDB.DBHelper.createDatabase(DBHelper.java:39)
at com.example.ListViewFromSQLiteDB.DBHelper.<init>(DBHelper.java:22)
at com.example.ListViewFromSQLiteDB.MainActivity.openAndQueryDatabase(MainActivity.java:41)
at com.example.ListViewFromSQLiteDB.MainActivity.onCreate(MainActivity.java:22)
at android.app.Activity.performCreate(Activity.java:5933)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
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)
04-11 14:19:23.183 2341-2341/com.example.ListViewFromSQLiteDB E/SQLiteLog﹕ (1) no such column: Brand
04-11 14:19:23.183 2341-2341/com.example.ListViewFromSQLiteDB E/MainActivity﹕ Could not create or Open the database
04-11 14:19:23.227 2341-2356/com.example.ListViewFromSQLiteDB D/OpenGLRenderer﹕ Render dirty regions requested: true
04-11 14:19:23.229 2341-2341/com.example.ListViewFromSQLiteDB D/﹕ HostConnection::get() New Host Connection established 0xa6d510b0, tid 2341
04-11 14:19:23.251 2341-2341/com.example.ListViewFromSQLiteDB D/Atlas﹕ Validating map...
04-11 14:19:23.382 2341-2356/com.example.ListViewFromSQLiteDB D/﹕ HostConnection::get() New Host Connection established 0xa6d51590, tid 2356
04-11 14:19:23.393 2341-2356/com.example.ListViewFromSQLiteDB I/OpenGLRenderer﹕ Initialized EGL, version 1.4
04-11 14:19:23.406 2341-2356/com.example.ListViewFromSQLiteDB D/OpenGLRenderer﹕ Enabling debug mode 0
04-11 14:19:23.445 2341-2356/com.example.ListViewFromSQLiteDB W/EGL_emulation﹕ eglSurfaceAttrib not implemented
04-11 14:19:23.445 2341-2356/com.example.ListViewFromSQLiteDB W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa6d6cd20, error=EGL_SUCCESS






<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://ift.tt/nIICcg"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="1">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
android:layout_weight="0.12" />
</LinearLayout>



Aucun commentaire:

Enregistrer un commentaire