mardi 10 mars 2015

Error in copying database from asset folder

I am making a simple app using sqlite database, put in asset folder and copy and use it. i used below code to make it



import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DatabaseHandler extends SQLiteOpenHelper {

// All Static variables
// Database Version
private static final int DATABASE_VERSION = 1;

// Database Name
private static final String DATABASE_NAME = "numerology.sqlite";

// Contacts table name
private static final String TABLE_CONTACTS = "detail";
public Context context;

// private static final String KEY_LIKE = "like";

public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);

this.context = context;
try {
copyDataBase(DATABASE_NAME);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {

}

private void copyDataBase(String dbname) throws IOException {
// Open your local db as the input stream
InputStream myInput = context.getAssets().open(dbname);
// Path to the just created empty db
String outFileName = "/data/data/com.example.firstapp/databases/"
+ dbname;
// Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(outFileName);
// transfer bytes from the inputfile to the outputfile
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();
}

// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACTS);

// Create tables again
onCreate(db);
}

// Getting All Contacts
public Utility getDetail(String type, String no) {
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_CONTACTS
+ " where type = '" + type + "' and no = '" + no + "'";

Utility news = null;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);

// looping through all rows and adding to list
if (cursor.moveToFirst()) {
news = new Utility(cursor.getString(1));
}

// return contact list
cursor.close();
return news;
}

}


And use this code in in my activity class



public class DetailActivity extends Activity {
// TextView contact;
DatabaseHandler db;
TextView detail;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.detail);
// contact = (TextView) findViewById(R.id.contact);
db = new DatabaseHandler(getApplicationContext());

Utility msg = db.getDetail("LoveAttitude", "1" );

detail.setText(msg.toString());

}
}


When i run this code , give sqlite exception on first time, but on second time run it works like charm.


I think problem in copying database but after many research i cant find solution. help me


Aucun commentaire:

Enregistrer un commentaire