Advertisement
Guest User

Untitled

a guest
Feb 25th, 2016
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.19 KB | None | 0 0
  1. package com.example.marci.parserv1;
  2.  
  3. /**
  4.  * Created by marci on 24.02.2016.
  5.  */
  6. import android.content.Context;
  7. import android.database.Cursor;
  8. import android.database.SQLException;
  9. import android.database.sqlite.SQLiteDatabase;
  10. import android.util.Log;
  11.  
  12. import java.io.IOException;
  13.  
  14. public class TestAdapter
  15. {
  16.     protected static final String TAG = "DataAdapter";
  17.  
  18.     private final Context mContext;
  19.     private SQLiteDatabase mDb;
  20.     private DataBaseHelper mDbHelper;
  21.  
  22.     public TestAdapter(Context context)
  23.     {
  24.         this.mContext = context;
  25.         mDbHelper = new DataBaseHelper(mContext);
  26.     }
  27.  
  28.     public TestAdapter createDatabase() throws SQLException
  29.     {
  30.         try
  31.         {
  32.             mDbHelper.createDataBase();
  33.         }
  34.         catch (IOException mIOException)
  35.         {
  36.             Log.e(TAG, mIOException.toString() + "  UnableToCreateDatabase");
  37.             throw new Error("UnableToCreateDatabase");
  38.         }
  39.         return this;
  40.     }
  41.  
  42.     public TestAdapter open() throws SQLException
  43.     {
  44.         try
  45.         {
  46.             mDbHelper.openDataBase();
  47.             mDbHelper.close();
  48.             mDb = mDbHelper.getWritableDatabase();
  49.         }
  50.         catch (SQLException mSQLException)
  51.         {
  52.             Log.e(TAG, "open >>"+ mSQLException.toString());
  53.             throw mSQLException;
  54.         }
  55.         return this;
  56.     }
  57.  
  58.     public void close()
  59.     {
  60.         mDbHelper.close();
  61.     }
  62.  
  63.  
  64.  
  65.     public Cursor getAll() {
  66.         try{
  67.         String[] columns = {"_id", "NAME", "DEPARTMENTS", "ADDRES"};
  68.         return mDb.query("university", columns, null, null, null, null, null);
  69.         }
  70.         catch (SQLException mSQLException)
  71.         {
  72.             Log.e(TAG, "getTestData >>"+ mSQLException.toString());
  73.             throw mSQLException;
  74.         }
  75.  
  76.     }
  77.    public Cursor getResult(String nazwa) {
  78.        Cursor cursor;
  79.  
  80.        cursor=mDb.rawQuery("SELECT * FROM university" + " WHERE "
  81.                + "NAME"  +
  82.                " LIKE  '"+nazwa+"%'",null);
  83.        return cursor;
  84.     }
  85.   public BuildingItem getBuilding(String namme) {
  86.  
  87.       String[] columns = {"_id", "NAME", "DEPARTMENTS", "ADDRES"};
  88.       String where = "NAME" + "=" + namme;
  89.       Cursor cursor = mDb.query("university", columns, where, null, null, null, null);
  90.       BuildingItem task = null;
  91.       if(cursor != null && cursor.moveToFirst()) {
  92.           String departments = cursor.getString(1);
  93.           String name = cursor.getString(2);
  94.           String addres = cursor.getString(3);
  95.           //boolean completed = cursor.getInt(COMPLETED_COLUMN) > 0 ? true : false;
  96.           task = new BuildingItem(name, departments, addres);
  97.       }
  98.       return task;
  99.   }
  100.  
  101.     public Cursor getTestData()
  102.     {
  103.         try
  104.         {
  105.             String sql ="SELECT * FROM university";
  106.  
  107.             Cursor mCur = mDb.rawQuery(sql, null);
  108.             if (mCur!=null)
  109.             {
  110.                 mCur.moveToNext();
  111.             }
  112.             return mCur;
  113.         }
  114.         catch (SQLException mSQLException)
  115.         {
  116.             Log.e(TAG, "getTestData >>"+ mSQLException.toString());
  117.             throw mSQLException;
  118.         }
  119.     }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement