Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- sqlite> CREATE TABLE example(id INTEGER PRIMARY KEY, quantity INTEGER);
- sqlite> INSERT INTO example(id,quantity) VALUES(1,1) ON CONFLICT(id) DO UPDATE SET quantity=quantity+1;
- sqlite> SELECT * FROM example;
- id quantity
- ---------- ----------
- 1 1
- sqlite> INSERT INTO example(id,quantity) VALUES(1,1) ON CONFLICT(id) DO UPDATE SET quantity=quantity+1;
- sqlite> SELECT * FROM example;
- id quantity
- ---------- ----------
- 1 2
- class DBHelper(context: Context) : SQLiteOpenHelper(context, DATABASE_NAME, null, DATABASE_VAR) {
- companion object {
- private val DATABASE_NAME = "stock1.db"
- private val DATABASE_VAR = 1
- //table
- private val TABLE_NAME = "stock"
- private val COL_ID = "_id"
- private val COL_NAME = "name"
- private val COL_QUANTITY = "quantity"
- private val COL_DATE = "date"
- private val COL_LOCATION = "location"
- var quantit = 1
- }
- override fun onCreate(db: SQLiteDatabase?) {
- val CREATE_TABLE_QUERY: String =
- ("CREATE TABLE $TABLE_NAME ($COL_ID INTEGER PRIMARY KEY, $COL_QUANTITY INTEGER,$COL_LOCATION TEXT, $COL_DATE DATE)")
- db!!.execSQL(CREATE_TABLE_QUERY)
- }
- override fun onUpgrade(db: SQLiteDatabase?, oldVersion: Int, newVersion: Int) {
- db!!.execSQL("DROP TABLE IF EXISTS $TABLE_NAME")
- onCreate(db!!)
- println("success")
- }
- val allItem: List<Item>
- get() {
- val stItem = ArrayList<Item>()
- val selectQuery = "SELECT * FROM $TABLE_NAME"
- val db = this.writableDatabase
- val cursor = db.rawQuery(selectQuery, null)
- if (cursor.moveToFirst()) {
- do {
- val item = Item()
- item._id = cursor.getInt(cursor.getColumnIndex(COL_ID))
- item.quantity = cursor.getInt(cursor.getColumnIndex(COL_QUANTITY))
- // item.name = cursor.getString(cursor.getColumnIndex(COL_NAME))
- item.location = cursor.getString(cursor.getColumnIndex(COL_LOCATION))
- item.date= cursor.getString(cursor.getColumnIndex(COL_DATE))
- stItem.add(item)
- } while (cursor.moveToNext())
- }
- db.close()
- return stItem
- }
- fun addItem(item: Item) {
- val selectQuery = "SELECT * FROM $TABLE_NAME "
- val db = this.writableDatabase
- val cursor = db.rawQuery(selectQuery, null)
- val values = ContentValues()
- values.put(COL_ID, item._id)
- values.put(COL_QUANTITY, item.quantity)
- values.put(COL_LOCATION, item.location)
- values.put(COL_DATE, item.date)
- db.insert(TABLE_NAME, null, values)
- db.close()
- }
- fun updateItem(item: Item): Int {
- val db = this.writableDatabase
- val values = ContentValues()
- values.put(COL_ID, item._id)
- values.put(COL_QUANTITY, item.quantity)
- return db.update(TABLE_NAME, values, "$COL_ID=?", arrayOf(item._id.toString()))
- }
- fun deleteItem(item: Item) {
- val db = this.writableDatabase
- db.delete(TABLE_NAME, "$COL_ID=?", arrayOf(item._id.toString()))
- db.close()
- }
- }
- class Adapter(
- internal var activity: Activity,
- internal var stitem: List<Item>,
- internal var edit_id: EditText
- ) : BaseAdapter() {
- internal var inflater: LayoutInflater
- init {
- inflater = activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
- }
- override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
- val rowView: View
- rowView = inflater.inflate(R.layout.row_layout, null)
- rowView.text_id.text = stitem[position]._id.toString()
- rowView.text_quantity.text = stitem[position].quantity.toString()
- rowView.setOnClickListener() {
- edit_id.setText(rowView.text_id.text.toString())
- rowView.text_quantity.text = DBHelper.quantit.toString()
- }
- return rowView
- }
- override fun getItem(position: Int): Any {
- return stitem[position]
- }
- override fun getItemId(position: Int): Long {
- return stitem[position]._id.toLong()
- }
- override fun getCount(): Int {
- return stitem.size
- }
- }
- class Counting : AppCompatActivity() {
- internal lateinit var db: DBHelper
- internal var stItem: List<Item> = ArrayList<Item>()
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- setContentView(R.layout.activity_counting)
- val date = findViewById<TextView>(R.id.edt_date).setText(record)
- val location = findViewById<TextView>(R.id.edt_location).setText(rec)
- println(location)
- db = DBHelper(this)
- refreshData()
- edt_id.setOnKeyListener(View.OnKeyListener { v, keyCode, event ->
- if (keyCode == KeyEvent.KEYCODE_ENTER && event.action == KeyEvent.ACTION_UP) {
- //Perform Code
- edt_id.text.toString()
- println(Integer.parseInt(edt_id.text.toString()))
- val item = Item(
- Integer.parseInt(edt_id.text.toString()),
- Integer.parseInt(DBHelper.quantit.toString()),
- edt_location.text.toString(),
- edt_date.text.toString()
- )
- db.addItem(item)
- refreshData()
- edt_id.text = null
- }
- false
- })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement