Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.77 KB | None | 0 0
  1. sqlite> CREATE TABLE example(id INTEGER PRIMARY KEY, quantity INTEGER);
  2. sqlite> INSERT INTO example(id,quantity) VALUES(1,1) ON CONFLICT(id) DO UPDATE SET quantity=quantity+1;
  3. sqlite> SELECT * FROM example;
  4. id quantity
  5. ---------- ----------
  6. 1 1
  7. sqlite> INSERT INTO example(id,quantity) VALUES(1,1) ON CONFLICT(id) DO UPDATE SET quantity=quantity+1;
  8. sqlite> SELECT * FROM example;
  9. id quantity
  10. ---------- ----------
  11. 1 2
  12.  
  13. class DBHelper(context: Context) : SQLiteOpenHelper(context, DATABASE_NAME, null, DATABASE_VAR) {
  14. companion object {
  15. private val DATABASE_NAME = "stock1.db"
  16. private val DATABASE_VAR = 1
  17.  
  18. //table
  19. private val TABLE_NAME = "stock"
  20. private val COL_ID = "_id"
  21. private val COL_NAME = "name"
  22. private val COL_QUANTITY = "quantity"
  23. private val COL_DATE = "date"
  24. private val COL_LOCATION = "location"
  25. var quantit = 1
  26.  
  27. }
  28.  
  29. override fun onCreate(db: SQLiteDatabase?) {
  30.  
  31. val CREATE_TABLE_QUERY: String =
  32. ("CREATE TABLE $TABLE_NAME ($COL_ID INTEGER PRIMARY KEY, $COL_QUANTITY INTEGER,$COL_LOCATION TEXT, $COL_DATE DATE)")
  33. db!!.execSQL(CREATE_TABLE_QUERY)
  34.  
  35. }
  36.  
  37. override fun onUpgrade(db: SQLiteDatabase?, oldVersion: Int, newVersion: Int) {
  38. db!!.execSQL("DROP TABLE IF EXISTS $TABLE_NAME")
  39. onCreate(db!!)
  40. println("success")
  41. }
  42.  
  43. val allItem: List<Item>
  44. get() {
  45. val stItem = ArrayList<Item>()
  46. val selectQuery = "SELECT * FROM $TABLE_NAME"
  47. val db = this.writableDatabase
  48. val cursor = db.rawQuery(selectQuery, null)
  49. if (cursor.moveToFirst()) {
  50. do {
  51. val item = Item()
  52. item._id = cursor.getInt(cursor.getColumnIndex(COL_ID))
  53. item.quantity = cursor.getInt(cursor.getColumnIndex(COL_QUANTITY))
  54. // item.name = cursor.getString(cursor.getColumnIndex(COL_NAME))
  55. item.location = cursor.getString(cursor.getColumnIndex(COL_LOCATION))
  56. item.date= cursor.getString(cursor.getColumnIndex(COL_DATE))
  57.  
  58. stItem.add(item)
  59. } while (cursor.moveToNext())
  60. }
  61. db.close()
  62. return stItem
  63.  
  64. }
  65.  
  66. fun addItem(item: Item) {
  67. val selectQuery = "SELECT * FROM $TABLE_NAME "
  68. val db = this.writableDatabase
  69. val cursor = db.rawQuery(selectQuery, null)
  70. val values = ContentValues()
  71. values.put(COL_ID, item._id)
  72. values.put(COL_QUANTITY, item.quantity)
  73. values.put(COL_LOCATION, item.location)
  74. values.put(COL_DATE, item.date)
  75. db.insert(TABLE_NAME, null, values)
  76.  
  77. db.close()
  78. }
  79.  
  80. fun updateItem(item: Item): Int {
  81. val db = this.writableDatabase
  82. val values = ContentValues()
  83. values.put(COL_ID, item._id)
  84. values.put(COL_QUANTITY, item.quantity)
  85.  
  86. return db.update(TABLE_NAME, values, "$COL_ID=?", arrayOf(item._id.toString()))
  87. }
  88.  
  89. fun deleteItem(item: Item) {
  90. val db = this.writableDatabase
  91.  
  92. db.delete(TABLE_NAME, "$COL_ID=?", arrayOf(item._id.toString()))
  93. db.close()
  94. }
  95. }
  96.  
  97. class Adapter(
  98. internal var activity: Activity,
  99. internal var stitem: List<Item>,
  100. internal var edit_id: EditText
  101.  
  102. ) : BaseAdapter() {
  103. internal var inflater: LayoutInflater
  104.  
  105. init {
  106. inflater = activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
  107. }
  108.  
  109. override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
  110. val rowView: View
  111. rowView = inflater.inflate(R.layout.row_layout, null)
  112.  
  113. rowView.text_id.text = stitem[position]._id.toString()
  114. rowView.text_quantity.text = stitem[position].quantity.toString()
  115. rowView.setOnClickListener() {
  116.  
  117. edit_id.setText(rowView.text_id.text.toString())
  118. rowView.text_quantity.text = DBHelper.quantit.toString()
  119. }
  120. return rowView
  121. }
  122.  
  123. override fun getItem(position: Int): Any {
  124. return stitem[position]
  125. }
  126.  
  127. override fun getItemId(position: Int): Long {
  128. return stitem[position]._id.toLong()
  129. }
  130.  
  131. override fun getCount(): Int {
  132. return stitem.size
  133. }
  134. }
  135.  
  136. class Counting : AppCompatActivity() {
  137. internal lateinit var db: DBHelper
  138. internal var stItem: List<Item> = ArrayList<Item>()
  139.  
  140.  
  141.  
  142. override fun onCreate(savedInstanceState: Bundle?) {
  143. super.onCreate(savedInstanceState)
  144. setContentView(R.layout.activity_counting)
  145.  
  146. val date = findViewById<TextView>(R.id.edt_date).setText(record)
  147. val location = findViewById<TextView>(R.id.edt_location).setText(rec)
  148. println(location)
  149.  
  150.  
  151. db = DBHelper(this)
  152.  
  153. refreshData()
  154.  
  155. edt_id.setOnKeyListener(View.OnKeyListener { v, keyCode, event ->
  156.  
  157. if (keyCode == KeyEvent.KEYCODE_ENTER && event.action == KeyEvent.ACTION_UP) {
  158. //Perform Code
  159. edt_id.text.toString()
  160. println(Integer.parseInt(edt_id.text.toString()))
  161. val item = Item(
  162. Integer.parseInt(edt_id.text.toString()),
  163. Integer.parseInt(DBHelper.quantit.toString()),
  164. edt_location.text.toString(),
  165. edt_date.text.toString()
  166.  
  167.  
  168. )
  169. db.addItem(item)
  170. refreshData()
  171. edt_id.text = null
  172. }
  173. false
  174. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement