Advertisement
Guest User

Untitled

a guest
May 25th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.45 KB | None | 0 0
  1. package com.example.aven.projekt2
  2.  
  3. import android.content.Context
  4. import android.database.SQLException
  5. import android.database.sqlite.SQLiteDatabase
  6. import android.database.sqlite.SQLiteException
  7. import android.database.sqlite.SQLiteOpenHelper
  8. import java.io.FileOutputStream
  9. import java.io.IOException
  10.  
  11.  
  12. /**
  13. * Created by Aven on 2018-05-14.
  14. */
  15. class DatabaseManager: SQLiteOpenHelper {
  16.  
  17. private val DB_PATH = "/data/data/com.example.aven.projekt2/databases/"
  18.  
  19. private val DB_NAME = "BrickList.db"
  20.  
  21. private var myDataBase: SQLiteDatabase? = null
  22.  
  23. private val myContext: Context
  24.  
  25. /**
  26. * Constructor
  27. * Takes and keeps a reference of the passed context in order to access to the application assets and resources.
  28. * @param context
  29. */
  30. constructor(context: Context):super(context, "BrickList.db", null, 1){
  31. this.myContext = context
  32. }
  33.  
  34. /**
  35. * Creates a empty database on the system and rewrites it with your own database.
  36. */
  37. @Throws(IOException::class)
  38. fun createDataBase() {
  39.  
  40. val dbExist = checkDataBase()
  41.  
  42. if (dbExist) {
  43. //do nothing - database already exist
  44. } else {
  45.  
  46. //By calling this method and empty database will be created into the default system path
  47. //of your application so we are gonna be able to overwrite that database with our database.
  48. this.readableDatabase
  49.  
  50. try {
  51.  
  52. copyDataBase()
  53.  
  54. } catch (e: IOException) {
  55.  
  56. throw Error("Error copying database")
  57.  
  58. }
  59.  
  60. }
  61.  
  62. }
  63.  
  64. /**
  65. * Check if the database already exist to avoid re-copying the file each time you open the application.
  66. * @return true if it exists, false if it doesn't
  67. */
  68. private fun checkDataBase(): Boolean {
  69.  
  70. var checkDB: SQLiteDatabase? = null
  71.  
  72. try {
  73. val myPath = DB_PATH + DB_NAME
  74. checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY)
  75.  
  76. } catch (e: SQLiteException) {
  77.  
  78. //database does't exist yet.
  79.  
  80. }
  81.  
  82. if (checkDB != null) {
  83.  
  84. checkDB.close()
  85.  
  86. }
  87.  
  88. return if (checkDB != null) true else false
  89. }
  90.  
  91. /**
  92. * Copies your database from your local assets-folder to the just created empty database in the
  93. * system folder, from where it can be accessed and handled.
  94. * This is done by transfering bytestream.
  95. */
  96. @Throws(IOException::class)
  97. private fun copyDataBase() {
  98.  
  99. //Open your local db as the input stream
  100. val myInput = myContext.assets.open(DB_NAME)
  101.  
  102. // Path to the just created empty db
  103. val outFileName = DB_PATH + DB_NAME
  104.  
  105. //Open the empty db as the output stream
  106. val myOutput = FileOutputStream(outFileName)
  107.  
  108. //transfer bytes from the inputfile to the outputfile
  109. val buffer = ByteArray(1024)
  110. var length = myInput.read(buffer)
  111. while (length > 0) {
  112. myOutput.write(buffer, 0, length)
  113. length = myInput.read(buffer)
  114. }
  115.  
  116. //Close the streams
  117. myOutput.flush()
  118. myOutput.close()
  119. myInput.close()
  120.  
  121. }
  122.  
  123. @Throws(SQLException::class)
  124. fun openDataBase() {
  125.  
  126. //Open the database
  127. val myPath = DB_PATH + DB_NAME
  128. myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE)
  129.  
  130. }
  131.  
  132. @Synchronized override fun close() {
  133.  
  134. if (myDataBase != null)
  135. myDataBase!!.close()
  136.  
  137. super.close()
  138.  
  139. }
  140.  
  141. override fun onCreate(db: SQLiteDatabase) {
  142.  
  143. }
  144.  
  145. override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {
  146.  
  147. }
  148.  
  149. // Add your public helper methods to access and get content from the database.
  150. // You could return cursors by doing "return myDataBase.query(....)" so it'd be easy
  151. // to you to create adapters for your views.
  152.  
  153. fun fetchName(code: String): String{
  154. val query = "SELECT NAME FROM Parts WHERE CODE LIKE \"$code\""
  155. val db = this.readableDatabase
  156. val cursor = db.rawQuery(query, null)
  157. var name: String = ""
  158. if(cursor.moveToFirst()){
  159. name = cursor.getString(0)
  160. }
  161. return name
  162. }
  163.  
  164. fun fetchColorName(colorId: Int): String{
  165. val query = "SELECT NAME FROM Colors WHERE CODE=$colorId"
  166. val db = this.readableDatabase
  167. val cursor = db.rawQuery(query, null)
  168. var colorName: String = ""
  169. if(cursor.moveToFirst()){
  170. colorName = cursor.getString(0)
  171. }
  172. return colorName
  173. }
  174.  
  175. fun fetchTypeName(code: String): String{
  176. val query = "SELECT NAME FROM ItemTypes WHERE CODE LIKE \"$code\""
  177. val db = this.readableDatabase
  178. val cursor = db.rawQuery(query, null)
  179. var typeName: String = ""
  180. if(cursor.moveToFirst()){
  181. typeName = cursor.getString(0)
  182. }
  183. return typeName
  184. }
  185.  
  186. fun fetchProjects(): ArrayList<Project>{
  187. val query = "SELECT _id, Name, Active FROM Inventories"
  188. val db = this.readableDatabase
  189. val cursor = db.rawQuery(query, null)
  190. var tempList = ArrayList<Project>()
  191. if(cursor.moveToFirst()) {
  192. tempList.add(Project(cursor.getString(0), cursor.getInt(1), cursor.getInt(2)))
  193. }
  194. while(cursor.moveToNext()){
  195. tempList.add(Project(cursor.getString(0), cursor.getInt(1), cursor.getInt(2)))
  196. }
  197. return tempList
  198. }
  199.  
  200. fun fetchCode(itemId: Int): String{
  201. val query = "SELECT Code FROM Codes WHERE CODE LIKE \"$itemId\""
  202. val db = this.readableDatabase
  203. val cursor = db.rawQuery(query, null)
  204. var code: String = ""
  205. if(cursor.moveToFirst()){
  206. code = cursor.getString(0)
  207. }
  208. return code
  209. }
  210.  
  211. fun loadProjectData(id: Int):ArrayList<Block>{
  212. val query = "SELECT * FROM InventoriesParts WHERE InventoryID=$id"
  213. val db = this.readableDatabase
  214. val cursor = db.rawQuery(query, null)
  215. var tempList = ArrayList<Block>()
  216. if(cursor.moveToFirst()){
  217. var tCode = fetchCode(cursor.getInt(3))
  218. var tTypeCode = cursor.getString(2)
  219. // tempList.add(Block(fetchName(tCode),fetchTypeName(tTypeCode)))
  220. }
  221.  
  222.  
  223. return tempList
  224. }
  225. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement