0

So I've been reading the Android Developer docs and have spent the last few hours reading tutorials online and have downloaded a few samples. Some of them compile, some don't.

The ones that compile use the SQLLiteDatabase in different ways than other samples I downloaded. I'm now totally confused. I was following along with the Training docs on http://developer.android.com/guide/topics/data/data-storage.html#db until I got half way down the page where it just stopped, and said that we can checkout the Note Pad sample. But the Notepad sample uses different code than what I had just read in the docs.

My scenario:

  • Display ListView that pulls data from SQLiteDatabase in MainActivity.xml
  • If user presses 'Add entry' button in AddEntryActivity.xml, add the entry to the database
  • If user presses 'Delete entry' button in DeleteEntryActivity.xml, delete the entry in the database.

So it's pretty simple stuff which I could do in just a couple minutes in C# but I'm new to android and I have absolutely no idea where to start. I started with the documentation, but all it does it go about half way and then refers you to the sample which is just more confusing since it uses different code.

How can I achieve this?

4
  • androidtuts4u.blogspot.in/2012/11/… this tutorial might be helpful.Do a quick google search, tons of this kind example is available. Commented Sep 24, 2013 at 9:37
  • Yeah, I've downloaded heaps of samples but maybe 1 out of every 10 I download will actually compile and run :-/ I'm checking out the link now, thanks Commented Sep 24, 2013 at 9:38
  • ughhh, this example is bad in so many ways ... use Notepad sample ... Commented Sep 24, 2013 at 9:39
  • The Notepad sample is massive. I mean, it's an extremely basic app, yet the code used to create it seems so overly complex? Commented Sep 24, 2013 at 9:43

1 Answer 1

1

Use SQLiteDatabase class for this

     SQLiteDatabase db = openOrCreateDatabase("Database_Name", MODE_PRIVATE, null);

     //Create Table 
   String  strsql = "CREATE TABLE IF NOT EXISTS TableName(Name  VARCHAR(30),id    INT(15)  unique) ";
db.execSQL(strsql);       

you can fire query as you do in MYSQL(INSERT,UPDATE,DELETE) using db.execSQL()

To Retrive data you can use this

   strsql = "SELECT * FROM TableName ";

   Cursor c = db.rawQuery(strsql, null);

        int count = c.getCount();
        c.moveToFirst();

        while (k >= 0) {
                  ListviewContent.add(c.getString(c.getColumnIndex("Name")));
            setListAdapter(new ListViewAdapter(this));
            c.moveToNext();
            k--;

        }

   //you can set retrived data to list view as shown above.
Sign up to request clarification or add additional context in comments.

2 Comments

thank you very much for your help and time. I am so confused right now, this code is so simple, so how come the Notepad sample has so many different classes just to insert update and delete?
Actually I didn't worked over them so I don't know about them.This works for me with less efforts.All the best !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.