blm401 mobil cihazlar için android İletim sistemi sqlite

23
BLM401 Mobil Cihazlar için ANDROID İşletim Sistemi SQLite Veritabanı BLM401 1 Dr.Refik SAMET

Upload: others

Post on 28-Oct-2021

12 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: BLM401 Mobil Cihazlar için ANDROID İletim Sistemi SQLite

BLM401 Mobil Cihazlar için

ANDROID İşletim Sistemi

SQLite Veritabanı

BLM401 1 Dr.Refik SAMET

Page 2: BLM401 Mobil Cihazlar için ANDROID İletim Sistemi SQLite

GİRİŞ (1/4)

SQLite

• açık kaynak kodlu;

• sunucu gerektirmeyen;

• konfigürasyon ayarları gerektirmeyen;

• platformdan bağımsız;

• işlemsel (transactional);

• ilişkisel (relational);

• gömülü

veritabanı metodudur. BLM401 2 Dr.Refik SAMET

Page 3: BLM401 Mobil Cihazlar için ANDROID İletim Sistemi SQLite

GİRİŞ (2/4)

SQLite

• sadece bir dosyadan ibarettir;

• diskte ve hafızada çok az yer kaplar;

• Android içinde hazır gelmektedir.

• Android uygulamalarında saklanması istenen

veriler SQLite veritabanı oluşturarak saklanabilir;

• Android’ de oluşturulan veritabanları /data/data/

<paket adı>/databases klasöründe durmaktadır;

BLM401 3 Dr.Refik SAMET

Page 4: BLM401 Mobil Cihazlar için ANDROID İletim Sistemi SQLite

GİRİŞ (3/4)

• Android, SQL cümlecikleri kurarak

veritabanındaki verilere ulaşılmasını sağlayacak

kütüphaneler sunmaktadır;

SQLite üzerinde

• veritabanı oluşturma;

• veritabanına kayıt ekleme ve sorgulama;

• kayıt güncelleme;

• silme gibi işlemler yapılabilir.

BLM401 4 Dr.Refik SAMET

Page 5: BLM401 Mobil Cihazlar için ANDROID İletim Sistemi SQLite

GİRİŞ (4/4)

• Yukarıda sıralanan işlemler bir örnek proje

üzerinde anlatılacaktır;

• Projenin ismi merhabaSQLite olup bir

kütüphane uygulamasıdır;

• Bu uygulamayı anlamak için sayfalar 140-158

dikkatle okunmalı ve kodları yazılarak

çalıştırılmalıdır.

BLM401 5 Dr.Refik SAMET

Page 6: BLM401 Mobil Cihazlar için ANDROID İletim Sistemi SQLite

SQLite

• Embedded RDBMS

• 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

BLM401 Dr.Refik SAMET 6

Page 7: BLM401 Mobil Cihazlar için ANDROID İletim Sistemi SQLite

SQLite

Datastore – single, cross platform file (like

an MS Access DB)

• Definitions

• Tables

• Indicies

• Data

BLM401 Dr.Refik SAMET 7

Page 8: BLM401 Mobil Cihazlar için ANDROID İletim Sistemi SQLite

SQLite Data Types

This is quite different than the normal SQL

data types so please read:

http://www.sqlite.org/datatype3.html

BLM401 Dr.Refik SAMET 8

Page 9: BLM401 Mobil Cihazlar için ANDROID İletim Sistemi SQLite

SQLite 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.

BLM401 Dr.Refik SAMET 9

Page 10: BLM401 Mobil Cihazlar için ANDROID İletim Sistemi SQLite

android.database.sqlite

Contains the SQLite database management

classes that an application would use to

manage its own private database.

BLM401 Dr.Refik SAMET 10

Page 11: BLM401 Mobil Cihazlar için ANDROID İletim Sistemi SQLite

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.

BLM401 Dr.Refik SAMET 11

Page 12: BLM401 Mobil Cihazlar için ANDROID İletim Sistemi SQLite

android.database.sqlite -

Classes • 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.

BLM401 Dr.Refik SAMET 12

Page 13: BLM401 Mobil Cihazlar için ANDROID İletim Sistemi SQLite

android.database.sqlite -

Classes • 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.

BLM401 Dr.Refik SAMET 13

Page 14: BLM401 Mobil Cihazlar için ANDROID İletim Sistemi SQLite

android.database.sqlite.SQLite

Database

• Contains the methods for: creating,

opening, closing, inserting, updating,

deleting and quering an SQLite database

BLM401 Dr.Refik SAMET 14

Page 15: BLM401 Mobil Cihazlar için ANDROID İletim Sistemi SQLite

openOrCreateDatabase( )

• This method will open an existing

database or create one in the application

data area

BLM401 Dr.Refik SAMET 15

import android.database.sqlite.SQLiteDatabase;

SQLiteDatabase myDatabase;

myDatabase = openOrCreateDatabase ("my_sqlite_database.db" ,

SQLiteDatabase.CREATE_IF_NECESSARY , null);

Page 16: BLM401 Mobil Cihazlar için ANDROID İletim Sistemi SQLite

Creating Tables

• Create a static string containing the

SQLite CREATE statement, use the

execSQL( ) method to execute it.

BLM401 Dr.Refik SAMET 16

String createAuthor = "CREAT TABLE authors (

id INTEGER PRIMARY KEY

AUTOINCREMENT,

fname TEXT,

lname TEXT);

myDatabase.execSQL(createAuthor);

Page 17: BLM401 Mobil Cihazlar için ANDROID İletim Sistemi SQLite

insert( )

• long insert(String table, String

nullColumnHack, ContentValues values)

BLM401 Dr.Refik SAMET 17

import android.content.ContentValues;

ContentValues values = new ContentValues( );

values.put("firstname" , "J.K.");

values.put("lastname" , "Rowling");

long newAuthorID = myDatabase.insert("tbl_authors" , "" ,

values);

Page 18: BLM401 Mobil Cihazlar için ANDROID İletim Sistemi SQLite

update( )

• int update(String table, ContentValues

values, String whereClause, String[ ]

whereArgs)

BLM401 Dr.Refik SAMET 18

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() }

);

}

Page 19: BLM401 Mobil Cihazlar için ANDROID İletim Sistemi SQLite

delete( )

• int delete(String table, String

whereClause, String[] whereArgs)

BLM401 Dr.Refik SAMET 19

public void deleteBook(Integer bookId) {

myDatabase.delete("tbl_books" , "id=?" ,

new String[ ] { bookId.toString( ) } ) ;

}

Page 20: BLM401 Mobil Cihazlar için ANDROID İletim Sistemi SQLite

android.database

• http://developer.android.com/reference/androi

d/database/package-summary.html

• Contains classes and interfaces to explore

data returned through a content provider.

• The main thing you are going to use here is

the Cursor interface to get the data from the

resultset that is returned by a query

http://developer.android.com/reference/androi

d/database/Cursor.html

BLM401 Dr.Refik SAMET 20

Page 21: BLM401 Mobil Cihazlar için ANDROID İletim Sistemi SQLite

Queries

BLM401 Dr.Refik SAMET 21

• Method of SQLiteDatabase class and performs queries

on the DB and returns the results in a Cursor object

• Cursor c = mdb.query(p1,p2,p3,p4,p5,p6,p7)

– p1 ; Table name (String)

– p2 ; Columns to return (String array)

– p3 ; WHERE clause (use null for all, ?s for selection args)

– p4 ; selection arg values for ?s of WHERE clause

– p5 ; GROUP BY ( null for none) (String)

– p6 ; HAVING (null unless GROUP BY requires one) (String)

– p7 ; ORDER BY (null for default ordering)(String)

– p8 ; LIMIT (null for no limit) (String)

Page 22: BLM401 Mobil Cihazlar için ANDROID İletim Sistemi SQLite

Tutorial

• http://www.screaming-

penguin.com/node/7742

BLM401 Dr.Refik SAMET 22

Page 23: BLM401 Mobil Cihazlar için ANDROID İletim Sistemi SQLite

(son)

BAŞARILAR …

BLM401 Dr.Refik SAMET 23