samedi 11 avril 2015

Trouble connecting to existing database in android

I'm having some trouble connecting to my database. So I have a database called HealthySizing, and that's also the name of the file, there's no extension to it like .sqlite3 or .db and this table has the meta table that you need to have and also it has a table called Shirts which has 6 columns, but it doesn't have any ID column though. So when I run my app the only error that I get is that it cannot create or open the database. I've posted below the DBHelper.java, MainActivity.java, and activity_main.xml. If someone could help me fix this problem and get the data to come out of the database, that'd be great.





package com.example.ListViewFromSQLiteDB;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Locale;

public class DBHelper extends SQLiteOpenHelper {

private static final String DATABASE_PATH = "/data/data/com.example.ListViewFromSQLiteDB/databases";
private static final String DATABASE_NAME = "HealthySizing";
private static final int SCHEMA_VERSION = 3;
public SQLiteDatabase dbSqlite;
private final Context myContext;
public static String tableName = "Shirts";

public DBHelper(Context context) {
super(context, DATABASE_NAME, null, SCHEMA_VERSION);
this.myContext = context;
}

public void onCreate(SQLiteDatabase db) {

}

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}

public void createDatabase() {
createDB();
}

private void createDB() {

boolean dbExist = DBExists();

if (!dbExist) {

this.getReadableDatabase();
copyDBFromResource();

}

}

private boolean DBExists() {

SQLiteDatabase db = null;

try {

String databasePath = DATABASE_PATH + DATABASE_NAME;
db = SQLiteDatabase.openDatabase(databasePath, null, SQLiteDatabase.OPEN_READWRITE);
db.setLocale(Locale.getDefault());
db.setLockingEnabled(true);
db.setVersion(1);

} catch (SQLiteException e) {
Log.e("SqlHelper", "database not found");
}

if (db != null) {
db.close();
}

return db != null ? true : false;

}

private void copyDBFromResource() {

InputStream inputStream = null;
OutputStream outStream = null;
String dbFilePath = DATABASE_PATH + DATABASE_NAME;

try {

inputStream = myContext.getAssets().open(DATABASE_NAME);
outStream = new FileOutputStream(dbFilePath);

byte[] buffer = new byte[1024];
int length;

while((length = inputStream.read(buffer)) > 0) {
outStream.write(buffer, 0, length);
}

outStream.flush();
outStream.close();
inputStream.close();

} catch (IOException e) {
throw new Error("Problem copying database from resource file");
}

}

@Override
public synchronized void close() {
if (dbSqlite != null) {
dbSqlite.close();
}
super.close();
}

}



package com.example.ListViewFromSQLiteDB;

import java.sql.SQLException;
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);
DBHelper dbHelper = new DBHelper(this.getApplicationContext());
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 Name FROM " +
tableName, null);

if (c != null ) {

if (c.moveToFirst()) {

do {

String name = c.getString(c.getColumnIndex("Name"));
results.add(name);

} while (c.moveToNext());

}
//c.close();
}

//String name = c.getString(c.getInt(1));
//results.add(name);
//c.close();

}

catch (SQLiteException se ) {
Log.e(getClass().getSimpleName(), "Could not create or Open the database");
}

finally {
try {
if (newDB != null){
newDB.execSQL("DELETE FROM " + tableName );

newDB.close();
}
} catch (SQLiteException e) {
e.printStackTrace();
}
}

}

}



04-11 19:26:00.728 1933-1933/com.example.ListViewFromSQLiteDB I/art﹕ Not late-enabling -Xcheck:jni (already on)
04-11 19:26:00.798 1933-1933/com.example.ListViewFromSQLiteDB E/MainActivity﹕ Could not create or Open the database
04-11 19:26:00.838 1933-1945/com.example.ListViewFromSQLiteDB W/art﹕ Suspending all threads took: 8.691ms
04-11 19:26:00.854 1933-1945/com.example.ListViewFromSQLiteDB I/art﹕ Background sticky concurrent mark sweep GC freed 1680(81KB) AllocSpace objects, 0(0B) LOS objects, 0% free, 689KB/689KB, paused 12.131ms total 28.447ms
04-11 19:26:00.890 1933-1948/com.example.ListViewFromSQLiteDB D/OpenGLRenderer﹕ Render dirty regions requested: true
04-11 19:26:00.894 1933-1933/com.example.ListViewFromSQLiteDB D/﹕ HostConnection::get() New Host Connection established 0xae0f8ee0, tid 1933
04-11 19:26:00.902 1933-1933/com.example.ListViewFromSQLiteDB D/Atlas﹕ Validating map...
04-11 19:26:00.955 1933-1948/com.example.ListViewFromSQLiteDB D/﹕ HostConnection::get() New Host Connection established 0xae0f8f90, tid 1948
04-11 19:26:00.970 1933-1948/com.example.ListViewFromSQLiteDB I/OpenGLRenderer﹕ Initialized EGL, version 1.4
04-11 19:26:00.983 1933-1948/com.example.ListViewFromSQLiteDB D/OpenGLRenderer﹕ Enabling debug mode 0
04-11 19:26:00.998 1933-1948/com.example.ListViewFromSQLiteDB W/EGL_emulation﹕ eglSurfaceAttrib not implemented
04-11 19:26:00.999 1933-1948/com.example.ListViewFromSQLiteDB W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa6c83c00, error=EGL_SUCCESS
04-11 19:26:12.961 1933-1933/com.example.ListViewFromSQLiteDB E/MainActivity﹕ Could not create or Open the database
04-11 19:26:13.103 1933-1948/com.example.ListViewFromSQLiteDB W/EGL_emulation﹕ eglSurfaceAttrib not implemented
04-11 19:26:13.103 1933-1948/com.example.ListViewFromSQLiteDB W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa6c83c00, 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