0% found this document useful (0 votes)
35 views40 pages

SQLite GoogleMaps UNIT5

The document provides instructions on how to build a basic map application using Google Maps in Android. It covers downloading the Google API, adding required permissions and libraries to the manifest, retrieving an API key, and implementing a MapView in the layout with the API key.

Uploaded by

Ranjini K
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views40 pages

SQLite GoogleMaps UNIT5

The document provides instructions on how to build a basic map application using Google Maps in Android. It covers downloading the Google API, adding required permissions and libraries to the manifest, retrieving an API key, and implementing a MapView in the layout with the API key.

Uploaded by

Ranjini K
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 40

Histor

y is an open source embedded database. The


• SQlite
original implementation was designed by D. Richard
Hipp.
Why Sqlite
Only????
• In android we will use Sqlite database only . Because it is
in built DB in Android SDK more over it is lite weighted
relation DB suitable for Mobile Devices.
• We need not to load any drivers and we need not to
install the Sqlite separately.
• The queries also simple to understand and easy to
implement.
Embedded RDBMS (Relational Database Management
System)
 ACID Compliant (Atomicity, Consistency, Isolation,
Durability)
 Size – about 257 Kbytes
 Not a client/server architecture
 Accessed via function calls from the application
 Writing (insert, update, delete) locks the database, queries
can be done in parallel
Feature of
SQLite
• Application file format – Transactions guarantee ACID [Atomicity,
Consistency , Isolation, Durability] even after system crashes and
power failures.
triggers provide undo/redo feature.
• Temporary data analysis – Command line client, import CSV files
and use sql to analyze & generate reports .
• Embedded devices – Applicable to small, reliable and
portable like mobiles.
• Portable - uses only ANSI-standard C and VFS, file format is cross
platform (little vs. big endian, 32 vs. 64 bit)
Feature of
SQLite
• Reliable – has 100% test coverage, open source code
and bug database, transactions are ACID even if power
fails.
• Small – 300 kb library, runs in 16kb stack and 100kb
heap.
• Single Database File – An SQLite database is a single
ordinary disk file that can be located anywhere in the
directory hierarchy.
• Readable source code – The source code to SQLite is
designed to be readable and accessible to the average
Unique Features.
• No configuration. Just drop in the C library and go.
• No server process to administer or user accounts to
manage.
• Easy to backup and transmit database (just copy the file)
• Dynamic typing for column values, variable lengths for
column records
• Query can reference multiple database files
• A few non-standard SQL extensions (mostly for conflict
resolution)
Disadvantage
s
• High concurrency – reader/writer locks on the entire file.

• Huge datasets – DB file can’t exceed file system limit or


2TB.

• Access control – we don’t have any user interface


to operate Sqlite database objects as in MYSQL / SQL
Server
/Oracle. All the objects are virtual. However there are
few third party UI are available in the market.
Examples To
Do

Step:1 Importing package


“android.database.sqlite.SQLiteDatabase”
. Step:2 Creating object
SQLiteDatabase object name=null;
Examples To
Do Let us do some
coding
EX:mydb=openOrCreateDatabase("DatabaseName5",
MODE_PRIVATE,null);
//mydb is sqlite object name .
//DatabaseName5 is nothing but database name
//MODE_PRIVATE is permissions of a table accessing
Examples To
Do Let us do some
coding
mydb.execSQL("CREATE TABLE IF NOT EXISTS

+TableName+" (ColumnName DataType);");
Examples To
Do Let us do some
coding
Create:
mydb.execSQL("CREATE TABLE IF NOT EXISTS “
+TableName+" (ColumnName DataType);");
Alter:
ALTER TABLE TableName RENAME TO new-table-name
Drop:
DROP TABLE TableName
(View Source)
Examples To
Do Let us do some
coding
Select:
Cursor c=mydb.rawQuery("SELECT * FROM "+TableName+"
where Name='"+city+"'",null);
Insert:
mydb.execSQL("INSERT INTO "+TableName+“ (Name, Area)“
+
"VALUES ('RedFort','40.8 acres‘);");
Delete:
mydb.execSQL(“Delete"+TableName);
(View Source)
openOrCreateDatabase( )
 This method will open an existing database or create one
in the application data area

import android.database.sqlite.SQLiteDatabase;
SQLiteDatabase myDatabase;
myDatabase = openOrCreateDatabase
("my_sqlite_database.db" ,
SQLiteDatabase.CREATE_IF_NECESSARY , null);
Creating Tables
 Create a static string containing the SQLite CREATE
statement, use the execSQL( ) method to execute it.

String createAuthor = "CREAT TABLE authors (


id INTEGER PRIMARY KEY AUTOINCREMENT,
fname TEXT,
lname TEXT);
myDatabase.execSQL(createAuthor);
insert( )
long insert(String table, String nullColumnHack,
ContentValues values)

import android.content.ContentValues;
ContentValues values = new ContentValues( );
values.put("firstname" , "J.K.");
values.put("lastname" , "Rowling");
long newAuthorID = myDatabase.insert("tbl_authors" , "" ,
values);
update( )
int update(String table, ContentValues values, String
whereClause, String[ ]
whereArgs)

public void updateBookTitle(Integer bookId, String newTitle) {


ContentValues values = new ContentValues();
values.put("title" , newTitle);
myDatabase.update("tbl_books" , values ,
"id=?" , new String[ ] {bookId.toString() } );
}
delete( )

int delete(String table, String whereClause, String[]


whereArgs)

public void deleteBook(Integer bookId) {


myDatabase.delete("tbl_books" , "id=?" ,
new String[ ] { bookId.toString( ) } ) ;
SQLite
•OpenHelper
SQLite OpenHelper is a class to manage database
creation and version management.
• This class take care of opening the database if it
exists, creating it if it does not, and upgrading it as
necessary.
• This is for creating db “onCreate(SQLiteDataBase)”.
• when the database needs to be upgraded
“onUpgrade (SQLiteDataBase db, int oldVersion,
int newVersion)”.
• when the database has been opened
“onOpen (SQLiteDataBase db)”.
android.database.sqlite

 Contains the SQLite database management classes that


an application would use to manage its own private
database.
android.database.sqlite - Classes

 SQLiteCloseable - An object created from a SQLiteDatabase that can


be closed.
 SQLiteCursor - A Cursor implementation that exposes results from a
query on a SQLiteDatabase.
SQLiteDatabase - Exposes methods to manage a SQLite database.
 SQLiteOpenHelper - A helper class to manage database creation and
version management.
 SQLiteProgram - A base class for compiled SQLite programs.
 SQLiteQuery - A SQLite program that represents a query that reads the
resulting rows into a CursorWindow.
SQLiteQueryBuilder - a convenience class that helps build SQL queries
to be sent to SQLiteDatabase objects.
SQLiteStatement - A pre-compiled statement against a SQLiteDatabase
that can be reused.
Storage classes
 NULL – null value
 INTEGER - signed integer, stored in 1, 2, 3, 4, 6, or 8
bytes depending on the magnitude of the value
 REAL - a floating point value, 8-byte IEEE floating
point number.
 TEXT - text string, stored using the database encoding
(UTF-8, UTF-16BE or UTF-16LE).
 BLOB -The value is a blob of data, stored exactly as it
was input
Map
• Google owns Android, so it is obvious
that the map you're going to use in
Android app must be Google Map.
• However to use Google Map in your
app is not as easy as it should be.
Instruction how to make Map app:
1. Go to window menu -> Android SDK
Manager
-> Download Google API (2.3.3 API 10)
2. Create AVD Targeting Google API:
For this map app, you need to create an AVD
targeting Google API. In this case, please
choose API level 10 for 2.3.3.
3. Android Manifest: Add Google Maps
Library
<application …>
<uses- l i b r ary andr o id:name="com. google.andr oid.maps"
/>
<activity…>
...
</application>

4. Android Manifest: Add Internet Permission


<manifest …>
<uses-sdk android:minSdkVersion="10" / >
<uses-permission
android:name="android.permission.INTERNET"/>
...
...
</manifest>
AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.kosalab"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="10" / >
<uses-permission
android:name="android.permission
.INTERNET"/>

<application
android:icon="@drawable/
ic_launcher"
android:label="@string/
app_name" >
<uses-library
android:name="com.google.an
droid.maps" / >
<activity
android:label="@string/app_name"
android:name=".GoogleMapActivity" >
<intent-filter >
<action
android:name="android.intent.action.
5. main.xml: replace with this
codes
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.maps.MapView

xmlns:android="
http://schemas.android.com/apk/res/android"
android:id="@+id/mymap"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
android:apiKey="Enter Your API Key"
/>
6. Getting API Key:
1. Go to a website,
http://code.google.com/android/add-
ons/google-apis
2. Click on Maps API Key Signup
6.3. Enter your MD5 fingerprint. (Scroll down a
bit)
6.4. To get MD5
fingerprint
6.4.1. In Eclipse, go to Window -> Preferences -> Android -
> Build -> Default debug keystore -> Copy the path
6.4.2. In Windows Explorer, go to Java installed folder, e.g. c:\
Program Files (x86)\Java -> jre6 -> bin -> keystore.exe
• 6.4.3. After you found out keystore.exe, open cmd, and
then go to folder where keystore located.
keytool - l i s t - a l i a s androiddebugkey -keystore "C:\
User s \ ko s a l\ .a ndroi d \ debug.k eystore" - st orepass
android -keypass android

-> Copy the


• 6.4.5. Paste the PrivateKey into MD5 Fingerprint in the website
• -> Click "Generate API Key"
6.4.6. Finally, you will get your API
key:
7. Enter the API key into
main.xml.

<?xml version="1.0" encoding="utf-8"?>


<com.google.android.maps.MapView

xmlns:android="
http://schemas.android.com/apk/res/android"
android:id="@+id/mymap"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
android:apiKey="eNtEr_y0uR_Ap1_kEy_HeRe"
/>
8. In Java, you must extends MapActivity instead of Activity:

import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;

import android.os.Bundle;

public class GoogleMapActivity extends


MapActivity {
/ * * Called when the a c t i v i t y i s f i r s t created. * /
@Override
public void onCreate(Bundle savedInstanceState)
{ super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MapView mapView = (MapView) findViewById(R.id.mymap);
mapView.setBuiltInZoomControls(true);
}

@Override
protected boolean isRouteDisplayed() {
/ / TODO Auto-generated method stub
return f a l s e ;
}
}
• Run the App with the AVD targeting Google
API

You might also like