SlideShare uma empresa Scribd logo
1 de 17
DATABASE IN ANDROID 
FIRST PART
Database Persistence 
If you want the app to remember data between 
sessions, you need to persist the data. 
How? 
You can store your data using a SQLite 
database.
What is SQLite? 
SQLite is a lightweight Database that : 
● Use simple file database to store data. 
● Can use SQL for Database operations. 
● Can manage transactions. 
● Specifications and source code are being published 
● Driver have been developed in many languages.
Start by creating the database 
● Thus, you can create and open database directly inside your app. 
● The best way to get off the ground with a new database is to extend 
a built in abstract base class called SQLiteOpenHelper that provides 
you with all of the basic behavior to manage a database.
Classes provided by Android 
SQLiteHelper Class SQLiteDatabase Class 
Create Database Perform: 
Tables search/ insert/renew/delete 
Create Table 
Perform database connection: Execute the specified SQL- 
(open/close)
The method of Creation DB 
1. Create subclass of SQLiteOpenHelper class.
The method of Creation DB 
2.Execute the constructor of SQLiteOpenHelper class by 
constructor of the class created in 1, create database.
The method of creation DB 
3. Implement 2 methods that are decided by 
SQLiteOpenHelper: 
● onCreate: The method that implements the table creation 
process. 
● onUpgrade: The table that implements the table definition 
renew process.
The method of creation DB 
4. Execute the SQL statement that creates Table at 
SQLiteOpenHelper#onCREATE.
import android.content.Context; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteOpenHelper; 
import android.util.log; 
public class TrySQLiteOpenHelper extends SQLiteOpenHelper{ 
public trySQLiteOpenHelper(Context context){ 
super(context,”SAMPLE_DATABASE”, null, 1); 
} 
Example:
@override 
Example(continue): 
public void onCreate(SQLiteDatabase database){ 
database.exec.SQL(“CREATE_TABLE_SAMPLE_TABLE 
(-idINTEGER, 
nameTT>T);”); 
} 
Note: 
exec.SQL() executes the optional SQL statement to operate data.
@Override 
Example(continue): 
public void onUpgrade 
(SQLite Database, int old version, int new version){ 
} 
}
Search data from DB 
The mechanism is : 
● Need SQLiteDatabase objects to search data from database. 
● By executing SQLiteDatabase#query method, the results can be 
gotten by Cursor class. 
Note: 
Cursor is the class which represents a 2 dimensional table of any 
database.
Search data from DB 
● Get SQliteOpenDatabase objects by SQLiteOpenHelper method. 
And, depend on uses, There’re 2 methods: 
● getReadableDatabase. 
● getWritableDatabase.
SQLiteDatabase#query method 
● The method of search data: 
1. Create the object of SQLiteOpenHelper class. 
2. Use SQLiteOpenHelper#getReadable Database method to get 
SQLiteDatabase objects. 
3. Execute SQLiteDatabase#query method to get the Cursor objects 
of search results. 
4. Execute Activity#startManagingCursor to transmit Cursor 
management to Activity. 
5. Execute SQLiteDatabase#close method to close database.
TrySQLiteOpenHelper database; 
OpenHelper = new TrySQLiteOpenHelper(this); 
SQLiteDatabase database = null; 
Cursor cursor= null; 
try{ 
// get the SQLiteDatabase objects. 
database = database.OpenHelper.getReadableDatabase(); 
//get the cursor object of searching results. 
cursor = database.query(“SAMPLE_TABLE”,null, null, null, null, 
null); 
Example(query method)
// Transfer the Cursor object management to Activity 
startManagingCursor( cursor); 
}finally{ 
if(database)= null){ 
//disconnect with database 
database.close(); 
} 
} 
Example( query method)

Mais conteúdo relacionado

Mais procurados

android content providers
android content providersandroid content providers
android content providersDeepa Rani
 
Introduction to Android Development
Introduction to Android DevelopmentIntroduction to Android Development
Introduction to Android DevelopmentAly Abdelkareem
 
android layouts
android layoutsandroid layouts
android layoutsDeepa Rani
 
Android activities & views
Android activities & viewsAndroid activities & views
Android activities & viewsma-polimi
 
Supporting multiple screens on android
Supporting multiple screens on androidSupporting multiple screens on android
Supporting multiple screens on androidLi SUN
 
Android share preferences
Android share preferencesAndroid share preferences
Android share preferencesAjay Panchal
 
Android application structure
Android application structureAndroid application structure
Android application structureAlexey Ustenko
 
Introduction to android
Introduction to androidIntroduction to android
Introduction to androidzeelpatel0504
 
Introduction to Android development - Presentation
Introduction to Android development - PresentationIntroduction to Android development - Presentation
Introduction to Android development - PresentationAtul Panjwani
 

Mais procurados (20)

android content providers
android content providersandroid content providers
android content providers
 
Fragment
Fragment Fragment
Fragment
 
Introduction to Android Development
Introduction to Android DevelopmentIntroduction to Android Development
Introduction to Android Development
 
android layouts
android layoutsandroid layouts
android layouts
 
Android Basic Components
Android Basic ComponentsAndroid Basic Components
Android Basic Components
 
Android Components
Android ComponentsAndroid Components
Android Components
 
Android studio ppt
Android studio pptAndroid studio ppt
Android studio ppt
 
Android activities & views
Android activities & viewsAndroid activities & views
Android activities & views
 
AndroidManifest
AndroidManifestAndroidManifest
AndroidManifest
 
Android User Interface
Android User InterfaceAndroid User Interface
Android User Interface
 
Android Services
Android ServicesAndroid Services
Android Services
 
Supporting multiple screens on android
Supporting multiple screens on androidSupporting multiple screens on android
Supporting multiple screens on android
 
jQuery for beginners
jQuery for beginnersjQuery for beginners
jQuery for beginners
 
ADO.NET
ADO.NETADO.NET
ADO.NET
 
Activity lifecycle
Activity lifecycleActivity lifecycle
Activity lifecycle
 
Android share preferences
Android share preferencesAndroid share preferences
Android share preferences
 
Android application structure
Android application structureAndroid application structure
Android application structure
 
JQuery introduction
JQuery introductionJQuery introduction
JQuery introduction
 
Introduction to android
Introduction to androidIntroduction to android
Introduction to android
 
Introduction to Android development - Presentation
Introduction to Android development - PresentationIntroduction to Android development - Presentation
Introduction to Android development - Presentation
 

Destaque

Data Handning with Sqlite for Android
Data Handning with Sqlite for AndroidData Handning with Sqlite for Android
Data Handning with Sqlite for AndroidJakir Hossain
 
Android Protips: Advanced Topics for Expert Android App Developers
Android Protips: Advanced Topics for Expert Android App DevelopersAndroid Protips: Advanced Topics for Expert Android App Developers
Android Protips: Advanced Topics for Expert Android App DevelopersReto Meier
 
Android Training (Storing data using SQLite)
Android Training (Storing data using SQLite)Android Training (Storing data using SQLite)
Android Training (Storing data using SQLite)Khaled Anaqwa
 
09.1. Android - Local Database (Sqlite)
09.1. Android - Local Database (Sqlite)09.1. Android - Local Database (Sqlite)
09.1. Android - Local Database (Sqlite)Oum Saokosal
 
Android database tutorial
Android database tutorialAndroid database tutorial
Android database tutorialinfo_zybotech
 

Destaque (9)

ANDROID USING SQLITE DATABASE ADMINISTRATORS ~HMFTJ
ANDROID USING SQLITE DATABASE ADMINISTRATORS ~HMFTJANDROID USING SQLITE DATABASE ADMINISTRATORS ~HMFTJ
ANDROID USING SQLITE DATABASE ADMINISTRATORS ~HMFTJ
 
Android Database
Android DatabaseAndroid Database
Android Database
 
Android vs iOS
Android vs iOSAndroid vs iOS
Android vs iOS
 
Data Handning with Sqlite for Android
Data Handning with Sqlite for AndroidData Handning with Sqlite for Android
Data Handning with Sqlite for Android
 
Android Protips: Advanced Topics for Expert Android App Developers
Android Protips: Advanced Topics for Expert Android App DevelopersAndroid Protips: Advanced Topics for Expert Android App Developers
Android Protips: Advanced Topics for Expert Android App Developers
 
Android Training (Storing data using SQLite)
Android Training (Storing data using SQLite)Android Training (Storing data using SQLite)
Android Training (Storing data using SQLite)
 
09.1. Android - Local Database (Sqlite)
09.1. Android - Local Database (Sqlite)09.1. Android - Local Database (Sqlite)
09.1. Android - Local Database (Sqlite)
 
Design and Concepts of Android Graphics
Design and Concepts of Android GraphicsDesign and Concepts of Android Graphics
Design and Concepts of Android Graphics
 
Android database tutorial
Android database tutorialAndroid database tutorial
Android database tutorial
 

Semelhante a Database in Android

Databases in Android Application
Databases in Android ApplicationDatabases in Android Application
Databases in Android ApplicationMark Lester Navarro
 
Create an android app for database creation using.pptx
Create an android app for database creation using.pptxCreate an android app for database creation using.pptx
Create an android app for database creation using.pptxvishal choudhary
 
Android App Development 05 : Saving Data
Android App Development 05 : Saving DataAndroid App Development 05 : Saving Data
Android App Development 05 : Saving DataAnuchit Chalothorn
 
Chapter 3.pptx Oracle SQL or local Android database setup SQL, SQL-Lite, codi...
Chapter 3.pptx Oracle SQL or local Android database setup SQL, SQL-Lite, codi...Chapter 3.pptx Oracle SQL or local Android database setup SQL, SQL-Lite, codi...
Chapter 3.pptx Oracle SQL or local Android database setup SQL, SQL-Lite, codi...TAISEEREISA
 
Sql server 2012 tutorials writing transact-sql statements
Sql server 2012 tutorials   writing transact-sql statementsSql server 2012 tutorials   writing transact-sql statements
Sql server 2012 tutorials writing transact-sql statementsSteve Xu
 
How to use sq lite with java using net beans
How to use sq lite with java using net beansHow to use sq lite with java using net beans
How to use sq lite with java using net beansAravindharamanan S
 
Migrating from PHP 4 to PHP 5
Migrating from PHP 4 to PHP 5Migrating from PHP 4 to PHP 5
Migrating from PHP 4 to PHP 5John Coggeshall
 
12_Data_Storage_Part_2.pptx
12_Data_Storage_Part_2.pptx12_Data_Storage_Part_2.pptx
12_Data_Storage_Part_2.pptxFaezNasir
 
I am looking for some assistance with SQLite database. I have tried se.pdf
I am looking for some assistance with SQLite database. I have tried se.pdfI am looking for some assistance with SQLite database. I have tried se.pdf
I am looking for some assistance with SQLite database. I have tried se.pdfConint29
 
Accessing data with android cursors
Accessing data with android cursorsAccessing data with android cursors
Accessing data with android cursorsinfo_zybotech
 
Accessing data with android cursors
Accessing data with android cursorsAccessing data with android cursors
Accessing data with android cursorsinfo_zybotech
 
SQLite in Flutter.pptx
SQLite in Flutter.pptxSQLite in Flutter.pptx
SQLite in Flutter.pptxNabin Dhakal
 
Asp .Net Database Connectivity Presentation.pptx
Asp .Net Database Connectivity Presentation.pptxAsp .Net Database Connectivity Presentation.pptx
Asp .Net Database Connectivity Presentation.pptxsridharu1981
 

Semelhante a Database in Android (20)

Databases in Android Application
Databases in Android ApplicationDatabases in Android Application
Databases in Android Application
 
Create an android app for database creation using.pptx
Create an android app for database creation using.pptxCreate an android app for database creation using.pptx
Create an android app for database creation using.pptx
 
Android App Development 05 : Saving Data
Android App Development 05 : Saving DataAndroid App Development 05 : Saving Data
Android App Development 05 : Saving Data
 
Android sq lite-chapter 22
Android sq lite-chapter 22Android sq lite-chapter 22
Android sq lite-chapter 22
 
Sql lite android
Sql lite androidSql lite android
Sql lite android
 
Chapter 3.pptx Oracle SQL or local Android database setup SQL, SQL-Lite, codi...
Chapter 3.pptx Oracle SQL or local Android database setup SQL, SQL-Lite, codi...Chapter 3.pptx Oracle SQL or local Android database setup SQL, SQL-Lite, codi...
Chapter 3.pptx Oracle SQL or local Android database setup SQL, SQL-Lite, codi...
 
Notepad tutorial
Notepad tutorialNotepad tutorial
Notepad tutorial
 
Sql server 2012 tutorials writing transact-sql statements
Sql server 2012 tutorials   writing transact-sql statementsSql server 2012 tutorials   writing transact-sql statements
Sql server 2012 tutorials writing transact-sql statements
 
Android sql examples
Android sql examplesAndroid sql examples
Android sql examples
 
How to use sq lite with java using net beans
How to use sq lite with java using net beansHow to use sq lite with java using net beans
How to use sq lite with java using net beans
 
Chapter -7.pptx
Chapter -7.pptxChapter -7.pptx
Chapter -7.pptx
 
Sq lite
Sq liteSq lite
Sq lite
 
9 content-providers
9 content-providers9 content-providers
9 content-providers
 
Migrating from PHP 4 to PHP 5
Migrating from PHP 4 to PHP 5Migrating from PHP 4 to PHP 5
Migrating from PHP 4 to PHP 5
 
12_Data_Storage_Part_2.pptx
12_Data_Storage_Part_2.pptx12_Data_Storage_Part_2.pptx
12_Data_Storage_Part_2.pptx
 
I am looking for some assistance with SQLite database. I have tried se.pdf
I am looking for some assistance with SQLite database. I have tried se.pdfI am looking for some assistance with SQLite database. I have tried se.pdf
I am looking for some assistance with SQLite database. I have tried se.pdf
 
Accessing data with android cursors
Accessing data with android cursorsAccessing data with android cursors
Accessing data with android cursors
 
Accessing data with android cursors
Accessing data with android cursorsAccessing data with android cursors
Accessing data with android cursors
 
SQLite in Flutter.pptx
SQLite in Flutter.pptxSQLite in Flutter.pptx
SQLite in Flutter.pptx
 
Asp .Net Database Connectivity Presentation.pptx
Asp .Net Database Connectivity Presentation.pptxAsp .Net Database Connectivity Presentation.pptx
Asp .Net Database Connectivity Presentation.pptx
 

Mais de MaryadelMar85

gvSIG visualización y navegación
gvSIG visualización y navegacióngvSIG visualización y navegación
gvSIG visualización y navegaciónMaryadelMar85
 
gvSIG: Proyectos y Documentos
gvSIG: Proyectos y DocumentosgvSIG: Proyectos y Documentos
gvSIG: Proyectos y DocumentosMaryadelMar85
 
Conceptos Básicos en gvSIG
Conceptos Básicos en gvSIGConceptos Básicos en gvSIG
Conceptos Básicos en gvSIGMaryadelMar85
 
Definición e Instalación de gvSIG
Definición e Instalación de gvSIGDefinición e Instalación de gvSIG
Definición e Instalación de gvSIGMaryadelMar85
 
android:versionName and android:versionCode
android:versionName and  android:versionCodeandroid:versionName and  android:versionCode
android:versionName and android:versionCodeMaryadelMar85
 
Eclipse in Android. 3 common issues.
Eclipse in Android. 3 common issues.Eclipse in Android. 3 common issues.
Eclipse in Android. 3 common issues.MaryadelMar85
 
Intents and PendingIntents in Android application development
Intents and PendingIntents in Android application developmentIntents and PendingIntents in Android application development
Intents and PendingIntents in Android application developmentMaryadelMar85
 

Mais de MaryadelMar85 (7)

gvSIG visualización y navegación
gvSIG visualización y navegacióngvSIG visualización y navegación
gvSIG visualización y navegación
 
gvSIG: Proyectos y Documentos
gvSIG: Proyectos y DocumentosgvSIG: Proyectos y Documentos
gvSIG: Proyectos y Documentos
 
Conceptos Básicos en gvSIG
Conceptos Básicos en gvSIGConceptos Básicos en gvSIG
Conceptos Básicos en gvSIG
 
Definición e Instalación de gvSIG
Definición e Instalación de gvSIGDefinición e Instalación de gvSIG
Definición e Instalación de gvSIG
 
android:versionName and android:versionCode
android:versionName and  android:versionCodeandroid:versionName and  android:versionCode
android:versionName and android:versionCode
 
Eclipse in Android. 3 common issues.
Eclipse in Android. 3 common issues.Eclipse in Android. 3 common issues.
Eclipse in Android. 3 common issues.
 
Intents and PendingIntents in Android application development
Intents and PendingIntents in Android application developmentIntents and PendingIntents in Android application development
Intents and PendingIntents in Android application development
 

Último

How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesBoston Institute of Analytics
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 

Último (20)

How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 

Database in Android

  • 1. DATABASE IN ANDROID FIRST PART
  • 2. Database Persistence If you want the app to remember data between sessions, you need to persist the data. How? You can store your data using a SQLite database.
  • 3. What is SQLite? SQLite is a lightweight Database that : ● Use simple file database to store data. ● Can use SQL for Database operations. ● Can manage transactions. ● Specifications and source code are being published ● Driver have been developed in many languages.
  • 4. Start by creating the database ● Thus, you can create and open database directly inside your app. ● The best way to get off the ground with a new database is to extend a built in abstract base class called SQLiteOpenHelper that provides you with all of the basic behavior to manage a database.
  • 5. Classes provided by Android SQLiteHelper Class SQLiteDatabase Class Create Database Perform: Tables search/ insert/renew/delete Create Table Perform database connection: Execute the specified SQL- (open/close)
  • 6. The method of Creation DB 1. Create subclass of SQLiteOpenHelper class.
  • 7. The method of Creation DB 2.Execute the constructor of SQLiteOpenHelper class by constructor of the class created in 1, create database.
  • 8. The method of creation DB 3. Implement 2 methods that are decided by SQLiteOpenHelper: ● onCreate: The method that implements the table creation process. ● onUpgrade: The table that implements the table definition renew process.
  • 9. The method of creation DB 4. Execute the SQL statement that creates Table at SQLiteOpenHelper#onCREATE.
  • 10. import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.util.log; public class TrySQLiteOpenHelper extends SQLiteOpenHelper{ public trySQLiteOpenHelper(Context context){ super(context,”SAMPLE_DATABASE”, null, 1); } Example:
  • 11. @override Example(continue): public void onCreate(SQLiteDatabase database){ database.exec.SQL(“CREATE_TABLE_SAMPLE_TABLE (-idINTEGER, nameTT>T);”); } Note: exec.SQL() executes the optional SQL statement to operate data.
  • 12. @Override Example(continue): public void onUpgrade (SQLite Database, int old version, int new version){ } }
  • 13. Search data from DB The mechanism is : ● Need SQLiteDatabase objects to search data from database. ● By executing SQLiteDatabase#query method, the results can be gotten by Cursor class. Note: Cursor is the class which represents a 2 dimensional table of any database.
  • 14. Search data from DB ● Get SQliteOpenDatabase objects by SQLiteOpenHelper method. And, depend on uses, There’re 2 methods: ● getReadableDatabase. ● getWritableDatabase.
  • 15. SQLiteDatabase#query method ● The method of search data: 1. Create the object of SQLiteOpenHelper class. 2. Use SQLiteOpenHelper#getReadable Database method to get SQLiteDatabase objects. 3. Execute SQLiteDatabase#query method to get the Cursor objects of search results. 4. Execute Activity#startManagingCursor to transmit Cursor management to Activity. 5. Execute SQLiteDatabase#close method to close database.
  • 16. TrySQLiteOpenHelper database; OpenHelper = new TrySQLiteOpenHelper(this); SQLiteDatabase database = null; Cursor cursor= null; try{ // get the SQLiteDatabase objects. database = database.OpenHelper.getReadableDatabase(); //get the cursor object of searching results. cursor = database.query(“SAMPLE_TABLE”,null, null, null, null, null); Example(query method)
  • 17. // Transfer the Cursor object management to Activity startManagingCursor( cursor); }finally{ if(database)= null){ //disconnect with database database.close(); } } Example( query method)