Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Creating and Using Databases in Android : Page 2

Every application uses data, and Android applications are no exception. Android uses the open-source, stand-alone SQL database, SQLite. Learn how to create and manipulate a SQLite database for your Android app.


advertisement

Using the Database

You are now ready to use the database along with the helper class you've created. In the DatabaseActivity.javafile, create an instance of the DBAdapter class:

package net.learn2develop.Database; import android.app.Activity; import android.os.Bundle; public class DatabaseActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); DBAdapter db = new DBAdapter(this); } }

Adding a Title



To add a title into the titles table, use the insertTitle()method of the DBAdapter class:

@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); DBAdapter db = new DBAdapter(this); //---add 2 titles--- db.open(); long id; id = db.insertTitle( "0470285818", "C# 2008 Programmer's Reference", "Wrox"); id = db.insertTitle( "047017661X", "Professional Windows Vista Gadgets Programming", "Wrox"); db.close(); }

The insertTitle() method returns the ID of the inserted row. If an error occurs during the adding, it returns -1.

If you examine the file system of the Android device/emulator, you can observe that the books database is created under the databases folder (see Figure 3).

Figure 3. The Database Folder: The books database is created in the databases folder.

Retrieving All the Titles

To retrieve all the titles in the titles table, use the DBAdapter class' getAllTitles() method (see Listing 5).

The result is returned as a Cursor object. To display all the titles, you first need to call the Cursor object's moveToFirst() method. If it succeeds (which means there is at least one row available), display the details of the title using the DisplayTitle() method (defined below). To move to the next title, call the Cursor object's moveToNext()method:

public void DisplayTitle(Cursor c) { Toast.makeText(this, "id: " + c.getString(0) + "\n" + "ISBN: " + c.getString(1) + "\n" + "TITLE: " + c.getString(2) + "\n" + "PUBLISHER: " + c.getString(3), Toast.LENGTH_LONG).show(); }

Figure 4shows the Toast class displaying one of the titles retrieved from the database.

Figure 4. The Toast Class: The device displays each individual title using the Toast class.

Retrieving a Single Title

To retrieve a single title using its ID, call the getTitle()method of the DBAdapter class with the ID of the title:

@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); DBAdapter db = new DBAdapter(this); //---get a title--- db.open(); Cursor c = db.getTitle(2); if (c.moveToFirst()) DisplayTitle(c); else Toast.makeText(this, "No title found", Toast.LENGTH_LONG).show(); db.close(); }

The result is returned as a Cursor object. If a row is returned, display the details of the title using the DisplayTitle()method, else display an error message using the Toast class.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap