Win a copy of liveProject: Protecting User Data with Spring Security and OAuth2 this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • paul wheaton
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • Mikalai Zaikin
  • Himai Minh

how to pass data to a sqlite delete method

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i am trying to set up a delete button to delete a customer from a table, the customers are displayed in card views using a recycler view so each customer will have their own delete button, the customer information are displayed using textviews. i need to pass the customer id to the delete method i order to search the table for that specific id to delete. how do i pass the data succesfully?

this is what iver currently got to try and pass the data i need

passing data to method


delete method
 
Saloon Keeper
Posts: 23510
161
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One this that is very important to note here. Your code sample is for ANDROID, not for "regular" Java.

In standard Java, the preferred thing to do would be to create a PreparedStatement and bind the ID of the row to be deleted to it.

However, Android has its own API for SQLite and the syntax for deleting on that platform looks like this:


So with the parameters properly plugged in you get:


I have doubts about you having a class named "GlobalVars", but that's a different matter.
 
adrian rowlands
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
adjusted the delete method but still not working

i get this error in the log cat

 
Tim Holloway
Saloon Keeper
Posts: 23510
161
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The way I read that is that you didn't create the database itself.
 
Rancher
Posts: 582
9
Android Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
YES, this should ask in Android forum.

From the error, it seems like the delete query is wrong.

You may try this too
 
Tim Holloway
Saloon Keeper
Posts: 23510
161
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Randy Tong wrote:YES, this should ask in Android forum.

From the error, it seems like the delete query is wrong.

You may try this too



I wouldn't. It smells too much of SQL Injection attack. Plus it has more overhead.
 
Marshal
Posts: 3489
494
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:It smells too much of SQL Injection attack ...



Use whereArgs (placeholders) to prevent SQL injection:
 
adrian rowlands
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tried this still get same issue
 
Marshal
Posts: 26519
81
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:The way I read that is that you didn't create the database itself.



I agree, it looks like the problem is to do with accessing the database rather than with processing the query. Notice that there's a method named getReadableDatabase near the top of the stack trace, higher than deleteCustomer. And the error appears to be related to finding the location of the database ("getDatabasePath").

If my theory is correct then executing any query should have the same result, or maybe only any query which wants to update the database.
 
adrian rowlands
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:

Tim Holloway wrote:The way I read that is that you didn't create the database itself.



I agree, it looks like the problem is to do with accessing the database rather than with processing the query. Notice that there's a method named getReadableDatabase near the top of the stack trace, higher than deleteCustomer. And the error appears to be related to finding the location of the database ("getDatabasePath").

If my theory is correct then executing any query should have the same result, or maybe only any query which wants to update the database.



I can add to the data base fine, read it and display the items in the database so.

Any suggestions where I should look for the the issue?
Or maybe just start from scratch?
 
Tim Holloway
Saloon Keeper
Posts: 23510
161
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I expected that you would say that.

But all that means to me is that either you've accidentally created more than one instance of your SQLiteDBOpenHelper class and haven't initialized the one you're currently using, or you've managed to nuke/re-initialize the one you were using.
 
adrian rowlands
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:I expected that you would say that.

But all that means to me is that either you've accidentally created more than one instance of your SQLiteDBOpenHelper class and haven't initialized the one you're currently using, or you've managed to nuke/re-initialize the one you were using.



well ive started again and followed a youtube tutorial to create a basicapp to add to database and update, and ive gone through it exactly and the update doesnt actually work so i give up
 
Tim Holloway
Saloon Keeper
Posts: 23510
161
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't despair. It's not that hard and it does work reliably. There's probably some simple mistake you made that, when fixed, magically makes the whole thing work. That's usually how it is for me. Although it usually takes 3-4 days of swearing and finally getting a junior programmer to point out I'm missing something trivial.

Being that this is Android, probably one of the trickiest things to know is what methods get called when, since Android does much of the calling, not your code. Adding some tracing statements to the log (or IDE breakpoints) can help with that.

If you're new to Java, though, learning Java AND Android at the same time is going to be harder than learning just Java.
 
adrian rowlands
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:Don't despair. It's not that hard and it does work reliably. There's probably some simple mistake you made that, when fixed, magically makes the whole thing work. That's usually how it is for me. Although it usually takes 3-4 days of swearing and finally getting a junior programmer to point out I'm missing something trivial.

Being that this is Android, probably one of the trickiest things to know is what methods get called when, since Android does much of the calling, not your code. Adding some tracing statements to the log (or IDE breakpoints) can help with that.

If you're new to Java, though, learning Java AND Android at the same time is going to be harder than learning just Java.



I'll take a look in the later/morning what are tracing statements?
 
Tim Holloway
Saloon Keeper
Posts: 23510
161
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

adrian rowlands wrote:
I'll take a look in the later/morning what are tracing statements?



Use the android.util.Log class to write messages to the application log. Like so:


This is logging at level "i" (Informational), using a log tag of "ShoppingDB" to print out a message that includes my current data value. The log tag is just an identifier to help make related messages easier to find in the log.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic