Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private void initControls() {
- // TODO Auto-generated method stub
- userInput = (EditText) findViewById (R.id.editTextDialogUserInput);
- save = (Button) findViewById (R.id.btSave);
- cancel = (Button) findViewById (R.id.btCancel);
- save.setOnClickListener(this);
- cancel.setOnClickListener(this);
- Bundle extras = getIntent().getExtras();
- if (extras != null) {
- stID = extras.getString("dog_id");
- dog_name = extras.getString("dog_name");
- cursor = dbHelper.fetchbBreedByName(dog_name);
- strDesc = cursor.getString(cursor.getColumnIndexOrThrow("description"));
- Log.d("Animal ID", "Animal ID is " + stID + " and breed is " + dog_name);
- userInput.setText(strDesc);
- }
- }
- private void checkDatabaseConnection() {
- // TODO Auto-generated method stub
- dbHelper = new DBHelper(this);
- try {
- dbHelper.createDataBase();
- } catch (IOException ioe) {
- throw new Error("Unable to create database");
- }
- try {
- dbHelper.openDataBase();
- } catch (SQLException sqle) {
- throw sqle;
- }
- }
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- switch(v.getId()){
- case R.id.btSave:
- if(userInput.equals("")){
- Toast.makeText(this, "No input", Toast.LENGTH_SHORT).show();
- }
- else {
- id = Long.valueOf(stID);
- dbHelper.updateDescription( id, userInput.getText().toString() );
- Toast.makeText(this, "Description has been updated successfully!",
- Toast.LENGTH_SHORT).show();
- strDesc = cursor.getString(cursor.getColumnIndexOrThrow("description"));
- Log.d("Updated", dog_name + " " + strDesc);
- Intent i = new Intent(this, DogClass.class);
- startActivity(i);
- finish();
- }
- break;
- case R.id.btCancel:
- userInput.setText("");
- break;
- }
- }
- @Override
- protected void onDestroy() {
- // TODO Auto-generated method stub
- dbHelper.close(); // close DB
- cursor.close(); // close cursor
- super.onDestroy();
- }
- package com.pet101.util;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import android.annotation.SuppressLint;
- import android.content.ContentValues;
- import android.content.Context;
- import android.database.Cursor;
- import android.database.SQLException;
- import android.database.sqlite.SQLiteDatabase;
- import android.database.sqlite.SQLiteException;
- import android.database.sqlite.SQLiteOpenHelper;
- import android.util.Log;
- public class DBHelper extends SQLiteOpenHelper{
- //The Android's default system path of your application database.
- @SuppressLint("SdCardPath")
- private static String DB_PATH = "/data/data/com.pet101/databases/";
- private static String DB_NAME = "PetDB";
- static String KEY_ID = "_id";
- static String KEY_ANIMALTYPE = "animaltype";
- static String KEY_BREED = "breed";
- static String KEY_DESCRIPTION = "description";
- static String KEY_DIET = "diet";
- static String KEY_SHELTER = "shelter";
- static String KEY_MEDICATION = "medication";
- static String KEY_HYGIENE = "hygiene";
- static String DB_TABLE = "tblAnimalInfo";
- private SQLiteDatabase myDataBase;
- private final Context myContext;
- private static final int DATABASE_VERSION = 1;
- /**
- * Constructor
- * Takes and keeps a reference of the passed context in order to access to the application assets and resources.
- * @param context
- */
- public DBHelper(Context context) {
- super(context, DB_NAME, null, DATABASE_VERSION);
- this.myContext = context;
- }
- /**
- * Creates a empty database on the system and rewrites it with your own database.
- * */
- public void createDataBase() throws IOException {
- boolean dbExist = checkDataBase();
- myDataBase = null;
- if (dbExist) {
- // do nothing - database already exist
- } else {
- // By calling this method and empty database will be created into
- // the default system path
- myDataBase = this.getReadableDatabase();
- myDataBase.close();
- try {
- copyDataBase();
- } catch (IOException e) {
- throw new Error("Error copying database");
- }
- }
- }
- /**
- * Check if the database already exist to avoid re-copying the file each time you open the application.
- * @return true if it exists, false if it doesn't
- */
- private boolean checkDataBase(){
- SQLiteDatabase checkDB = null;
- try{
- String myPath = DB_PATH + DB_NAME;
- 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;
- }
- /**
- * Copies your database from your local assets-folder to the just created empty database in the
- * system folder, from where it can be accessed and handled.
- * This is done by transfering bytestream.
- * */
- private void copyDataBase() throws IOException{
- //Open your local db as the input stream
- InputStream myInput = myContext.getAssets().open(DB_NAME);
- // Path to the just created empty db
- String outFileName = DB_PATH + DB_NAME;
- //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();
- }
- public void openDataBase() throws SQLException {
- //Open the database
- myDataBase = this.getReadableDatabase();
- String myPath = DB_PATH + DB_NAME;
- myDataBase = SQLiteDatabase.openDatabase( myPath, null,
- SQLiteDatabase.NO_LOCALIZED_COLLATORS | SQLiteDatabase.OPEN_READWRITE );
- }
- @Override
- public synchronized void close() {
- if(myDataBase != null)
- myDataBase.close();
- super.close();
- }
- @Override
- public void onCreate(SQLiteDatabase db) {
- try {
- createDataBase();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- @Override
- public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
- Log.w("Test", "Upgrading DB from version " + oldVersion + " to " +
- newVersion + ", which will destroy all old data");
- db.execSQL("DROP TABLE IF EXISTS " + DB_TABLE);
- onCreate(db);
- }
- public Cursor getAllAnimals()
- {
- Cursor localCursor = //
- this.myDataBase.query(DB_TABLE, new String[] {
- KEY_ID, KEY_DESCRIPTION, KEY_DIET, KEY_SHELTER,
- KEY_HYGIENE, KEY_MEDICATION, KEY_BREED }, null, null, null, null, null);
- if (localCursor != null)
- localCursor.moveToFirst();
- return localCursor;
- }
- public String[] getAllAnimals(CharSequence animaltype) throws SQLException
- {
- Cursor localCursor =
- this.myDataBase.query(DB_TABLE, new String[] {
- KEY_ID, KEY_BREED },
- KEY_ANIMALTYPE + " = '" + animaltype + "'", null,
- null, null, null, null);
- String[] values = new String[localCursor.getCount()];
- int i = 0;
- for (localCursor.moveToFirst(); !localCursor.isAfterLast(); localCursor.moveToNext()) {
- values[i] = localCursor.getString(localCursor.getColumnIndex(KEY_BREED));
- i++;
- }
- localCursor.close();
- return values;
- }
- public Cursor fetchbBreedByName(CharSequence breed) throws SQLException {
- Cursor mCursor = null;
- if (breed == null || breed.length () == 0) {
- mCursor = myDataBase.query(DB_TABLE, new String[] { KEY_ID, KEY_DESCRIPTION,
- KEY_DIET, KEY_SHELTER, KEY_HYGIENE, KEY_MEDICATION, KEY_BREED },
- null, null, null, null, null);
- }
- else {
- mCursor = myDataBase.query(DB_TABLE, new String[] { KEY_ID, KEY_DESCRIPTION,
- KEY_DIET, KEY_SHELTER, KEY_HYGIENE, KEY_MEDICATION, KEY_BREED },
- KEY_BREED + " like '%" + breed + "%'", null, null, null, null);
- }
- if (mCursor != null) {
- mCursor.moveToFirst();
- }
- return mCursor;
- }
- public long addAnimalInfo( String ANIMALTYPE, String DESCRIPTION, String DIET,
- String SHELTER, String HYGIENE, String MEDICATION, String BREED ) {
- ContentValues cv = new ContentValues();
- cv.put(KEY_ANIMALTYPE, ANIMALTYPE);
- cv.put(KEY_DESCRIPTION, DESCRIPTION);
- cv.put(KEY_DIET, DIET);
- cv.put(KEY_SHELTER, SHELTER);
- cv.put(KEY_HYGIENE, HYGIENE);
- cv.put(KEY_MEDICATION, MEDICATION);
- cv.put(KEY_BREED, BREED);
- return myDataBase.insert(DB_TABLE, null, cv);
- }
- public long addDescription( String DESCRIPTION, String BREED ) {
- ContentValues cv = new ContentValues();
- cv.put(KEY_DESCRIPTION, DESCRIPTION);
- cv.put(KEY_BREED, BREED);
- return myDataBase.insert(DB_TABLE, null, cv);
- }
- public long addDiet( String DIET, String BREED ) {
- ContentValues cv = new ContentValues();
- cv.put(KEY_DIET, DIET);
- cv.put(KEY_BREED, BREED);
- return myDataBase.insert(DB_TABLE, null, cv);
- }
- public long addShelter( String SHELTER, String BREED ) {
- ContentValues cv = new ContentValues();
- cv.put(KEY_SHELTER, SHELTER);
- cv.put(KEY_BREED, BREED);
- return myDataBase.insert(DB_TABLE, null, cv);
- }
- public long addHygiene( String HYGIENE, String BREED ) {
- ContentValues cv = new ContentValues();
- cv.put(KEY_HYGIENE, HYGIENE);
- cv.put(KEY_BREED, BREED);
- return myDataBase.insert(DB_TABLE, null, cv);
- }
- public long addMedication( String MEDICATION, String BREED ) {
- ContentValues cv = new ContentValues();
- cv.put(KEY_MEDICATION, MEDICATION);
- cv.put(KEY_BREED, BREED);
- return myDataBase.insert(DB_TABLE, null, cv);
- }
- public boolean updateDescription( long lId, String DESCRIPTION ) {
- ContentValues cvUpdate = new ContentValues();
- cvUpdate.put(KEY_DESCRIPTION, DESCRIPTION);
- return myDataBase.update(DB_TABLE, cvUpdate, KEY_ID + " = " + lId, null)> 0;
- }
- public boolean updateDiet( long lId, String DIET) {
- ContentValues cvUpdate = new ContentValues();
- cvUpdate.put(KEY_DIET, DIET);
- return myDataBase.update(DB_TABLE, cvUpdate, KEY_ID + " = " + lId, null)> 0;
- }
- public void updateShelter( long lId, String SHELTER ) {
- ContentValues cvUpdate = new ContentValues();
- cvUpdate.put(KEY_SHELTER, SHELTER);
- myDataBase.update(DB_TABLE, cvUpdate, KEY_ID + " = " + lId, null);
- }
- public void updateHygiene( long lId,String HYGIENE ) {
- ContentValues cvUpdate = new ContentValues();
- cvUpdate.put(KEY_HYGIENE, HYGIENE);
- myDataBase.update(DB_TABLE, cvUpdate, KEY_ID + " = " + lId, null);
- }
- public void updateMedication( long lId, String MEDICATION ) {
- ContentValues cvUpdate = new ContentValues();
- cvUpdate.put(KEY_MEDICATION, MEDICATION);
- myDataBase.update(DB_TABLE, cvUpdate, KEY_ID + " = " + lId, null);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement