Advertisement
Guest User

Untitled

a guest
Oct 5th, 2013
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. private void initControls() {
  2. // TODO Auto-generated method stub
  3. userInput = (EditText) findViewById (R.id.editTextDialogUserInput);
  4.  
  5. save = (Button) findViewById (R.id.btSave);
  6. cancel = (Button) findViewById (R.id.btCancel);
  7. save.setOnClickListener(this);
  8. cancel.setOnClickListener(this);
  9.  
  10. Bundle extras = getIntent().getExtras();
  11. if (extras != null) {
  12.  
  13. stID = extras.getString("dog_id");
  14. dog_name = extras.getString("dog_name");
  15. cursor = dbHelper.fetchbBreedByName(dog_name);
  16.  
  17. strDesc = cursor.getString(cursor.getColumnIndexOrThrow("description"));
  18. Log.d("Animal ID", "Animal ID is " + stID + " and breed is " + dog_name);
  19. userInput.setText(strDesc);
  20. }
  21.  
  22. }
  23.  
  24. private void checkDatabaseConnection() {
  25. // TODO Auto-generated method stub
  26. dbHelper = new DBHelper(this);
  27.  
  28. try {
  29.  
  30. dbHelper.createDataBase();
  31.  
  32. } catch (IOException ioe) {
  33.  
  34. throw new Error("Unable to create database");
  35.  
  36. }
  37.  
  38. try {
  39.  
  40. dbHelper.openDataBase();
  41.  
  42. } catch (SQLException sqle) {
  43.  
  44. throw sqle;
  45.  
  46. }
  47.  
  48.  
  49. }
  50.  
  51.  
  52. @Override
  53. public void onClick(View v) {
  54. // TODO Auto-generated method stub
  55. switch(v.getId()){
  56. case R.id.btSave:
  57. if(userInput.equals("")){
  58. Toast.makeText(this, "No input", Toast.LENGTH_SHORT).show();
  59.  
  60. }
  61. else {
  62. id = Long.valueOf(stID);
  63. dbHelper.updateDescription( id, userInput.getText().toString() );
  64. Toast.makeText(this, "Description has been updated successfully!",
  65. Toast.LENGTH_SHORT).show();
  66.  
  67. strDesc = cursor.getString(cursor.getColumnIndexOrThrow("description"));
  68.  
  69. Log.d("Updated", dog_name + " " + strDesc);
  70. Intent i = new Intent(this, DogClass.class);
  71. startActivity(i);
  72. finish();
  73. }
  74. break;
  75. case R.id.btCancel:
  76. userInput.setText("");
  77. break;
  78. }
  79. }
  80.  
  81. @Override
  82. protected void onDestroy() {
  83. // TODO Auto-generated method stub
  84. dbHelper.close(); // close DB
  85. cursor.close(); // close cursor
  86. super.onDestroy();
  87. }
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103. package com.pet101.util;
  104.  
  105. import java.io.FileOutputStream;
  106. import java.io.IOException;
  107. import java.io.InputStream;
  108. import java.io.OutputStream;
  109.  
  110. import android.annotation.SuppressLint;
  111. import android.content.ContentValues;
  112. import android.content.Context;
  113. import android.database.Cursor;
  114. import android.database.SQLException;
  115. import android.database.sqlite.SQLiteDatabase;
  116. import android.database.sqlite.SQLiteException;
  117. import android.database.sqlite.SQLiteOpenHelper;
  118. import android.util.Log;
  119.  
  120. public class DBHelper extends SQLiteOpenHelper{
  121.  
  122. //The Android's default system path of your application database.
  123. @SuppressLint("SdCardPath")
  124. private static String DB_PATH = "/data/data/com.pet101/databases/";
  125. private static String DB_NAME = "PetDB";
  126. static String KEY_ID = "_id";
  127. static String KEY_ANIMALTYPE = "animaltype";
  128. static String KEY_BREED = "breed";
  129. static String KEY_DESCRIPTION = "description";
  130. static String KEY_DIET = "diet";
  131. static String KEY_SHELTER = "shelter";
  132. static String KEY_MEDICATION = "medication";
  133. static String KEY_HYGIENE = "hygiene";
  134.  
  135. static String DB_TABLE = "tblAnimalInfo";
  136. private SQLiteDatabase myDataBase;
  137. private final Context myContext;
  138. private static final int DATABASE_VERSION = 1;
  139.  
  140. /**
  141. * Constructor
  142. * Takes and keeps a reference of the passed context in order to access to the application assets and resources.
  143. * @param context
  144. */
  145.  
  146. public DBHelper(Context context) {
  147. super(context, DB_NAME, null, DATABASE_VERSION);
  148. this.myContext = context;
  149. }
  150.  
  151. /**
  152. * Creates a empty database on the system and rewrites it with your own database.
  153. * */
  154.  
  155. public void createDataBase() throws IOException {
  156.  
  157. boolean dbExist = checkDataBase();
  158.  
  159. myDataBase = null;
  160.  
  161. if (dbExist) {
  162. // do nothing - database already exist
  163. } else {
  164.  
  165. // By calling this method and empty database will be created into
  166. // the default system path
  167.  
  168. myDataBase = this.getReadableDatabase();
  169. myDataBase.close();
  170.  
  171. try {
  172. copyDataBase();
  173. } catch (IOException e) {
  174. throw new Error("Error copying database");
  175. }
  176. }
  177. }
  178.  
  179. /**
  180. * Check if the database already exist to avoid re-copying the file each time you open the application.
  181. * @return true if it exists, false if it doesn't
  182. */
  183.  
  184. private boolean checkDataBase(){
  185. SQLiteDatabase checkDB = null;
  186. try{
  187. String myPath = DB_PATH + DB_NAME;
  188. checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
  189. }catch(SQLiteException e){
  190. //database doesn't exist yet.
  191. }
  192. if(checkDB != null){
  193. checkDB.close();
  194. }
  195.  
  196. return checkDB != null ? true : false;
  197. }
  198.  
  199. /**
  200. * Copies your database from your local assets-folder to the just created empty database in the
  201. * system folder, from where it can be accessed and handled.
  202. * This is done by transfering bytestream.
  203. * */
  204.  
  205. private void copyDataBase() throws IOException{
  206.  
  207. //Open your local db as the input stream
  208. InputStream myInput = myContext.getAssets().open(DB_NAME);
  209.  
  210. // Path to the just created empty db
  211. String outFileName = DB_PATH + DB_NAME;
  212.  
  213. //Open the empty db as the output stream
  214. OutputStream myOutput = new FileOutputStream(outFileName);
  215.  
  216. //transfer bytes from the inputfile to the outputfile
  217. byte[] buffer = new byte[1024];
  218. int length;
  219. while ((length = myInput.read(buffer))>0){
  220. myOutput.write(buffer, 0, length);
  221. }
  222.  
  223. //Close the streams
  224. myOutput.flush();
  225. myOutput.close();
  226. myInput.close();
  227.  
  228. }
  229.  
  230. public void openDataBase() throws SQLException {
  231. //Open the database
  232. myDataBase = this.getReadableDatabase();
  233. String myPath = DB_PATH + DB_NAME;
  234. myDataBase = SQLiteDatabase.openDatabase( myPath, null,
  235. SQLiteDatabase.NO_LOCALIZED_COLLATORS | SQLiteDatabase.OPEN_READWRITE );
  236.  
  237. }
  238.  
  239. @Override
  240. public synchronized void close() {
  241. if(myDataBase != null)
  242. myDataBase.close();
  243. super.close();
  244. }
  245.  
  246. @Override
  247. public void onCreate(SQLiteDatabase db) {
  248. try {
  249. createDataBase();
  250. } catch (IOException e) {
  251. // TODO Auto-generated catch block
  252. e.printStackTrace();
  253. }
  254. }
  255.  
  256. @Override
  257. public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  258. Log.w("Test", "Upgrading DB from version " + oldVersion + " to " +
  259. newVersion + ", which will destroy all old data");
  260. db.execSQL("DROP TABLE IF EXISTS " + DB_TABLE);
  261. onCreate(db);
  262. }
  263.  
  264. public Cursor getAllAnimals()
  265. {
  266. Cursor localCursor = //
  267. this.myDataBase.query(DB_TABLE, new String[] {
  268. KEY_ID, KEY_DESCRIPTION, KEY_DIET, KEY_SHELTER,
  269. KEY_HYGIENE, KEY_MEDICATION, KEY_BREED }, null, null, null, null, null);
  270.  
  271. if (localCursor != null)
  272. localCursor.moveToFirst();
  273. return localCursor;
  274. }
  275.  
  276. public String[] getAllAnimals(CharSequence animaltype) throws SQLException
  277. {
  278. Cursor localCursor =
  279. this.myDataBase.query(DB_TABLE, new String[] {
  280. KEY_ID, KEY_BREED },
  281. KEY_ANIMALTYPE + " = '" + animaltype + "'", null,
  282. null, null, null, null);
  283.  
  284. String[] values = new String[localCursor.getCount()];
  285. int i = 0;
  286. for (localCursor.moveToFirst(); !localCursor.isAfterLast(); localCursor.moveToNext()) {
  287. values[i] = localCursor.getString(localCursor.getColumnIndex(KEY_BREED));
  288. i++;
  289. }
  290. localCursor.close();
  291. return values;
  292.  
  293. }
  294.  
  295. public Cursor fetchbBreedByName(CharSequence breed) throws SQLException {
  296.  
  297. Cursor mCursor = null;
  298. if (breed == null || breed.length () == 0) {
  299. mCursor = myDataBase.query(DB_TABLE, new String[] { KEY_ID, KEY_DESCRIPTION,
  300. KEY_DIET, KEY_SHELTER, KEY_HYGIENE, KEY_MEDICATION, KEY_BREED },
  301. null, null, null, null, null);
  302.  
  303. }
  304. else {
  305. mCursor = myDataBase.query(DB_TABLE, new String[] { KEY_ID, KEY_DESCRIPTION,
  306. KEY_DIET, KEY_SHELTER, KEY_HYGIENE, KEY_MEDICATION, KEY_BREED },
  307. KEY_BREED + " like '%" + breed + "%'", null, null, null, null);
  308. }
  309.  
  310. if (mCursor != null) {
  311. mCursor.moveToFirst();
  312. }
  313.  
  314. return mCursor;
  315. }
  316.  
  317. public long addAnimalInfo( String ANIMALTYPE, String DESCRIPTION, String DIET,
  318. String SHELTER, String HYGIENE, String MEDICATION, String BREED ) {
  319.  
  320. ContentValues cv = new ContentValues();
  321. cv.put(KEY_ANIMALTYPE, ANIMALTYPE);
  322. cv.put(KEY_DESCRIPTION, DESCRIPTION);
  323. cv.put(KEY_DIET, DIET);
  324. cv.put(KEY_SHELTER, SHELTER);
  325. cv.put(KEY_HYGIENE, HYGIENE);
  326. cv.put(KEY_MEDICATION, MEDICATION);
  327. cv.put(KEY_BREED, BREED);
  328.  
  329. return myDataBase.insert(DB_TABLE, null, cv);
  330.  
  331. }
  332.  
  333. public long addDescription( String DESCRIPTION, String BREED ) {
  334.  
  335. ContentValues cv = new ContentValues();
  336. cv.put(KEY_DESCRIPTION, DESCRIPTION);
  337. cv.put(KEY_BREED, BREED);
  338.  
  339. return myDataBase.insert(DB_TABLE, null, cv);
  340.  
  341. }
  342.  
  343. public long addDiet( String DIET, String BREED ) {
  344.  
  345. ContentValues cv = new ContentValues();
  346. cv.put(KEY_DIET, DIET);
  347. cv.put(KEY_BREED, BREED);
  348.  
  349. return myDataBase.insert(DB_TABLE, null, cv);
  350.  
  351. }
  352.  
  353. public long addShelter( String SHELTER, String BREED ) {
  354.  
  355. ContentValues cv = new ContentValues();
  356. cv.put(KEY_SHELTER, SHELTER);
  357. cv.put(KEY_BREED, BREED);
  358.  
  359. return myDataBase.insert(DB_TABLE, null, cv);
  360.  
  361. }
  362.  
  363. public long addHygiene( String HYGIENE, String BREED ) {
  364.  
  365. ContentValues cv = new ContentValues();
  366. cv.put(KEY_HYGIENE, HYGIENE);
  367. cv.put(KEY_BREED, BREED);
  368.  
  369. return myDataBase.insert(DB_TABLE, null, cv);
  370.  
  371. }
  372.  
  373. public long addMedication( String MEDICATION, String BREED ) {
  374.  
  375. ContentValues cv = new ContentValues();
  376. cv.put(KEY_MEDICATION, MEDICATION);
  377. cv.put(KEY_BREED, BREED);
  378.  
  379. return myDataBase.insert(DB_TABLE, null, cv);
  380.  
  381. }
  382.  
  383. public boolean updateDescription( long lId, String DESCRIPTION ) {
  384.  
  385. ContentValues cvUpdate = new ContentValues();
  386. cvUpdate.put(KEY_DESCRIPTION, DESCRIPTION);
  387.  
  388. return myDataBase.update(DB_TABLE, cvUpdate, KEY_ID + " = " + lId, null)> 0;
  389.  
  390. }
  391.  
  392. public boolean updateDiet( long lId, String DIET) {
  393. ContentValues cvUpdate = new ContentValues();
  394.  
  395. cvUpdate.put(KEY_DIET, DIET);
  396.  
  397. return myDataBase.update(DB_TABLE, cvUpdate, KEY_ID + " = " + lId, null)> 0;
  398. }
  399.  
  400. public void updateShelter( long lId, String SHELTER ) {
  401. ContentValues cvUpdate = new ContentValues();
  402.  
  403. cvUpdate.put(KEY_SHELTER, SHELTER);
  404.  
  405. myDataBase.update(DB_TABLE, cvUpdate, KEY_ID + " = " + lId, null);
  406. }
  407.  
  408. public void updateHygiene( long lId,String HYGIENE ) {
  409. ContentValues cvUpdate = new ContentValues();
  410.  
  411. cvUpdate.put(KEY_HYGIENE, HYGIENE);
  412.  
  413. myDataBase.update(DB_TABLE, cvUpdate, KEY_ID + " = " + lId, null);
  414. }
  415.  
  416. public void updateMedication( long lId, String MEDICATION ) {
  417. ContentValues cvUpdate = new ContentValues();
  418.  
  419. cvUpdate.put(KEY_MEDICATION, MEDICATION);
  420.  
  421. myDataBase.update(DB_TABLE, cvUpdate, KEY_ID + " = " + lId, null);
  422. }
  423.  
  424.  
  425. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement