Postingan lainnya
Jika ID=001 data baru akan insert ke table dan jika ID=001 sudah maka update qty bertambah
disini saya punya masalah bagaimana caranya jika terdapat data baru makan akan insert ke table dan jika data sudah ada maka akan upadate
contoh tblscan (ID,nmbrg,QTY)
jika data baru ID=001, nmbrg= A, QTY=1 maka insert ke tblscan
dan jika data ID=001 sudah ada dan saya input QTY jadi 2, maka akan update QTY nya saja berubah jadi 3.
dan jika saya input ID=002 maka akan jadi data baru insert lagi
terimakasih sblmnya
berikut yang sudah saya kerjakan
qty.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View view, int keycode, KeyEvent keyEvent) {
if ((keyEvent.getAction() == KeyEvent.ACTION_DOWN) && (keycode == KeyEvent.KEYCODE_ENTER)) {
SQLiteDatabase db = controller.getWritableDatabase();
cursor = db.rawQuery("select * from tblscan", null);
if (cursor != null && cursor.getCount() > 0) {
IDupdate = ID.getText().toString();
qtyupdate = qty.getText().toString();
db.execSQL("update tblscandata set QTY = QTY + '" + qty.getText().toString() + "' where ID = '" + IDupdate + "'");
kosongdata();
}
else {
IDinsert = ID.getText().toString();
desinsert = desc.getText().toString();
qtyinsert = qty.getText().toString();
query_insert = "INSERT INTO " + DBController.tableName1 + " values ( '" + IDinsert +"', '" + desinsert + "','" + qtyinsert + "')";
db.execSQL(query_insert);
Toast.makeText(ScanData.this,"Data Inserted Successfully", Toast.LENGTH_LONG).show();
kosongdata();
}
}
return false;
}
});
2 Jawaban:
<div>Di baris kode ini:</div><pre><strong>cursor </strong>= db.rawQuery(<strong>"select * from tblscan"</strong>, <strong>null</strong>);</pre><div>Diganti dengan:</div><pre>nomor<strong>ID </strong>= <strong>ID</strong>.getText().toString(); <strong>cursor </strong>= db.rawQuery(<strong>"select * from tblscan where ID='"+nomorID+"'"</strong>, <strong>null</strong>);</pre><div>Jadi ID dari awal sudah dicari, bukan dibedakan IDupdate dan IDinsert. des dan qty jg harusnya gak perlu dibedain variabelnya, karena kan sumbernya sama2 dari TextField, malah bikin boros memori.<br><br>Di baris kode ini:</div><pre>db.execSQL(<strong>"update tblscandata set QTY = QTY + '" </strong>+ <strong>qty</strong>.getText().toString() + <strong>"' where ID = '" </strong>+ ID<strong>update </strong>+ <strong>"'"</strong>);</pre><div>Kenapa qty nya jadi string? Tapi di coba dulu aja, kalo error atau gak mau update, mungkin petiknya perlu dihilangin.</div><div><br><br>Kunjungi juga <a href="https://warkop-it.000webhostapp.com">https://warkop-it.000webhostapp.com</a></div>
Tanggapan
Alhamdulillah berhasil..terimakasih tapi saya punya masalah baru..apakah bisa dibantu.. jadi saya punya 2 table (tblmaster dan tblscandata)
dan mempunyai form sprti ini..(terdapat automatic dan manual) untuk yg manual sudah aman
<div>Alhamdulillah berhasil..<br>tapi saya punya masalah baru..apakah bisa dibantu..<br>jadi saya punya 2 table (tblmaster dan tblscandata)<br><br>dan mempunyai form sprti ini..(terdapat automatic dan manual) untuk yg manual sudah aman<br><figure data-trix-attachment="{"contentType":"image/jpeg","filename":"scandata.jpg","filesize":59313,"height":1280,"url":"https://i.ibb.co/MGWmhSy/scandata.jpg","width":960}" data-trix-content-type="image/jpeg" data-trix-attributes="{"presentation":"gallery"}" class="attachment attachment--preview attachment--jpg"><img src="https://i.ibb.co/MGWmhSy/scandata.jpg" width="960" height="1280"><figcaption class="attachment__caption"><span class="attachment__name">scandata.jpg</span></figcaption></figure><br><br><br>ketika saya klik automatic dan saya input upc dan enter muncul semua data desc,price,sku,soh (ambil dr tblmaster) dan langsung insert(ke tblscandata) sebagai data baru..<br>tetapi bgtu data upc saya input dan enter akan cek data (di tblscandata) klo data nya sudah ada, maka qty nya aja yg akan bertambah 1 (default qty ketika automatic)<br><br>berikut source yg saya kerjakan<br><br></div><pre><strong>upc</strong>.setOnKeyListener(<strong>new </strong>View.OnKeyListener() { @Override <strong>public boolean </strong>onKey(View view, <strong>int </strong>keycode, KeyEvent keyEvent) { <strong>int </strong>id = <strong>Pilihan</strong>.getCheckedRadioButtonId(); <strong>switch </strong>(id) { <strong>case </strong>R.id.<strong><em>rb_auto</em></strong>: <strong>if </strong>((keyEvent.getAction() == KeyEvent.<strong><em>ACTION_DOWN</em></strong>) && (keycode == KeyEvent.<strong><em>KEYCODE_ENTER</em></strong>)) { SQLiteDatabase db = <strong>controller</strong>.getWritableDatabase(); <strong>cursor </strong>= db.rawQuery(<strong>"select * from tblmaster where upc ='" </strong>+ <strong>upc</strong>.getText().toString() + <strong>"'"</strong>, <strong>null</strong>); <strong>daftar </strong>= <strong>new </strong>String[<strong>cursor</strong>.getCount()]; <strong>if </strong>(<strong>upc</strong>.getText().toString().length() == 0) { Toast.<em>makeText</em>(getApplicationContext(), <strong>"UPC HARUS DI ISI"</strong>, Toast.<strong><em>LENGTH_LONG</em></strong>).show(); <strong>upc</strong>.requestFocus(); <strong>break</strong>; } <strong>else if </strong>(<strong>cursor</strong>.getCount() == 0) { Toast.<em>makeText</em>(getApplicationContext(), <strong>"DATA TIDAK DITEMUKAN"</strong>, Toast.<strong><em>LENGTH_LONG</em></strong>).show(); <strong>upc</strong>.setFocusableInTouchMode(<strong>true</strong>); <strong>upc</strong>.setText(<strong>""</strong>); <strong>upc</strong>.requestFocus(); <strong>break</strong>; } <strong>else </strong>{ <strong>cursor</strong>.moveToPosition(0); <strong>desc</strong>.setText(<strong>cursor</strong>.getString(1).toString()); <strong>price</strong>.setText(<strong>cursor</strong>.getString(2).toString()); <strong>sku</strong>.setText(<strong>cursor</strong>.getString(3).toString()); <strong>soh</strong>.setText(<strong>cursor</strong>.getString(4).toString()); <strong>upchilang</strong>.setText(<strong>cursor</strong>.getString(0).toString()); } <strong>cursor1 </strong>= db.rawQuery(<strong>"select * from tblscandata where upc='" </strong>+ <strong>UPC_A </strong>+ <strong>"'"</strong>, <strong>null</strong>); <strong>if </strong>(<strong>cursor1 </strong>!= <strong>null </strong>&& <strong>cursor1</strong>.getCount() > 0) { <strong>UPC_A </strong>= <strong>upc</strong>.getText().toString(); <strong>DESC_A </strong>= <strong>desc</strong>.getText().toString(); <strong>PRICE_A </strong>= <strong>price</strong>.getText().toString(); <strong>SKU_A </strong>= <strong>sku</strong>.getText().toString(); <strong>SOH_A </strong>= <strong>soh</strong>.getText().toString(); <strong>QTY_A </strong>= <strong>qty</strong>.getText().toString(); db.execSQL(<strong>"update tblscandata set QTY = QTY + '" </strong>+ <strong>qty</strong>.getText().toString() + <strong>"' where upc = '" </strong>+ <strong>UPC_A </strong>+ <strong>"'"</strong>); <strong>upc</strong>.requestFocus(); kosongdata(); <strong>break</strong>; } <strong>else </strong>{ <strong>query </strong>= <strong>"INSERT INTO " </strong>+ DBController.<em>tableName1 </em>+ <strong>" values ( '" </strong>+ <strong>UPC_A </strong>+ <strong>"', '" </strong>+ <strong>DESC_A </strong>+ <strong>"','" </strong>+ <strong>PRICE_A </strong>+ <strong>"','" </strong>+ <strong>SKU_A </strong>+ <strong>"','" </strong>+ <strong>SOH_A </strong>+ <strong>"','" </strong>+ <strong>QTY_A </strong>+ <strong>"')"</strong>; db.execSQL(<strong>query</strong>); Toast.<em>makeText</em>(ScanData.<strong>this</strong>, <strong>"Data Inserted Successfully"</strong>, Toast.<strong><em>LENGTH_LONG</em></strong>).show(); <strong>upc</strong>.requestFocus(); kosongdata(); }
}
<strong>break</strong>;
<strong>case </strong>R.id.<strong><em>rb_manual</em></strong>:
<strong>if </strong>((keyEvent.getAction() == KeyEvent.<strong><em>ACTION_DOWN</em></strong>) &amp;&amp;
(keycode == KeyEvent.<strong><em>KEYCODE_ENTER</em></strong>)) {
<strong>upc</strong>.requestFocus();
SQLiteDatabase db = <strong>controller</strong>.getWritableDatabase();
<strong>cursor </strong>= db.rawQuery(<strong>"select * from tblmaster where upc ='" </strong>+
<strong>upc</strong>.getText().toString() + <strong>"'"</strong>, <strong>null</strong>);
<strong>daftar </strong>= <strong>new </strong>String[<strong>cursor</strong>.getCount()];
<strong>if </strong>(<strong>upc</strong>.getText().toString().length() == 0) {
Toast.<em>makeText</em>(getApplicationContext(), <strong>"UPC HARUS DI ISI"</strong>, Toast.<strong><em>LENGTH_LONG</em></strong>).show();
<strong>upc</strong>.requestFocus();
<strong>break</strong>;
} <strong>else if </strong>(<strong>cursor</strong>.getCount() == 0) {
Toast.<em>makeText</em>(getApplicationContext(), <strong>"DATA TIDAK DITEMUKAN"</strong>, Toast.<strong><em>LENGTH_LONG</em></strong>).show();
<strong>upc</strong>.setText(<strong>""</strong>);
<strong>upc</strong>.requestFocus();
} <strong>else </strong>{
<strong>cursor</strong>.moveToPosition(0);
<strong>desc</strong>.setText(<strong>cursor</strong>.getString(1).toString());
<strong>price</strong>.setText(<strong>cursor</strong>.getString(2).toString());
<strong>sku</strong>.setText(<strong>cursor</strong>.getString(3).toString());
<strong>soh</strong>.setText(<strong>cursor</strong>.getString(4).toString());
<strong>upchilang</strong>.setText(<strong>cursor</strong>.getString(0).toString());
<strong>qty</strong>.requestFocus();
}
}
<strong>break</strong>;
}
<strong>return false</strong>;
}
}); <strong>qty</strong>.setOnKeyListener(<strong>new </strong>View.OnKeyListener() { @Override <strong>public boolean </strong>onKey(View view, <strong>int </strong>keycode, KeyEvent keyEvent) { <strong>if </strong>((keyEvent.getAction() == KeyEvent.<strong><em>ACTION_DOWN</em></strong>) && (keycode == KeyEvent.<strong><em>KEYCODE_ENTER</em></strong>)) { SQLiteDatabase db = <strong>controller</strong>.getWritableDatabase(); <strong>UPC </strong>= <strong>upc</strong>.getText().toString(); <strong>DESC </strong>= <strong>desc</strong>.getText().toString(); <strong>PRICE </strong>= <strong>price</strong>.getText().toString(); <strong>SKU </strong>= <strong>sku</strong>.getText().toString(); <strong>SOH </strong>= <strong>soh</strong>.getText().toString(); <strong>QTY </strong>= <strong>qty</strong>.getText().toString(); <strong>cursor </strong>= db.rawQuery(<strong>"select * from tblscandata where upc='" </strong>+ <strong>UPC </strong>+ <strong>"'"</strong>, <strong>null</strong>); <strong>if </strong>(<strong>cursor </strong>!= <strong>null </strong>&& <strong>cursor</strong>.getCount() > 0) { db.execSQL(<strong>"update tblscandata set QTY = QTY + '" </strong>+ <strong>qty</strong>.getText().toString() + <strong>"' where upc = '" </strong>+ <strong>UPC </strong>+ <strong>"'"</strong>); <strong>upc</strong>.requestFocus(); kosongdata(); } <strong>else </strong>{ <strong>query </strong>= <strong>"INSERT INTO " </strong>+ DBController.<em>tableName1 </em>+ <strong>" values ( '" </strong>+ <strong>UPC </strong>+ <strong>"', '" </strong>+ <strong>DESC </strong>+ <strong>"','" </strong>+ <strong>PRICE </strong>+ <strong>"','" </strong>+ <strong>SKU </strong>+ <strong>"','" </strong>+ <strong>SOH </strong>+ <strong>"','" </strong>+ <strong>QTY </strong>+ <strong>"')"</strong>; db.execSQL(<strong>query</strong>); Toast.<em>makeText</em>(ScanData.<strong>this</strong>, <strong>"Data Inserted Successfully"</strong>, Toast.<strong><em>LENGTH_LONG</em></strong>).show(); <strong>upc</strong>.requestFocus(); kosongdata(); } } <strong>return false</strong>; }
});</pre>