Top Banner
Database Intro w/SQLite How to put an embedded database on your phone and integrate with your application. [email protected] Thursday, February 28, 13
21

Database Intro w/SQLite · 2013-03-01 · Database Intro w/SQLite How to put an embedded database on your phone and integrate with your application. [email protected] Thursday,

Mar 28, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Database Intro w/SQLite · 2013-03-01 · Database Intro w/SQLite How to put an embedded database on your phone and integrate with your application. deano@webenefitall.com Thursday,

Database Intro w/SQLite

How to put an embedded database on your phone and integrate with your application.

[email protected], February 28, 13

Page 2: Database Intro w/SQLite · 2013-03-01 · Database Intro w/SQLite How to put an embedded database on your phone and integrate with your application. deano@webenefitall.com Thursday,

Things you will need:

SQLite software to create the database comes standard on intel macs

free needs to be downloaded for windows

SQLite browser/editor software

used to create/edit databases and tables

free, just needs to be downloaded and set up

Create a simple database(s) and table(s)

Create Android Database sample application

Put database into Android project

Test app (fingers crossed, hope all goes well) :)

Thursday, February 28, 13

Page 3: Database Intro w/SQLite · 2013-03-01 · Database Intro w/SQLite How to put an embedded database on your phone and integrate with your application. deano@webenefitall.com Thursday,

Download & Setup SQLite software

For Mac

It is include in Mac OS X.

It is located in /usr/bin directory and called sqlite3

Thursday, February 28, 13

Page 4: Database Intro w/SQLite · 2013-03-01 · Database Intro w/SQLite How to put an embedded database on your phone and integrate with your application. deano@webenefitall.com Thursday,

Download & Setup SQLite software

For Windows

Download Precompiled Windows Binaries from the SQLite webpage.

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

Extract the binaries by double clicking on the downloaded zip files. You can extract the files to your C drive (folder name C:\sqlite\ ) or to any location which suits your needs.

Thursday, February 28, 13

Page 5: Database Intro w/SQLite · 2013-03-01 · Database Intro w/SQLite How to put an embedded database on your phone and integrate with your application. deano@webenefitall.com Thursday,

Download & Setup SQLite Browser (same software for Win/Mac)

Download zip file from

http://sourceforge.net/projects/sqlitebrowser/

Click on the Download icon

Extract this to some location on your PC/Mac

Double click on SQLite Database Browser application to run SQLite DB Browser

Win & Mac Screen shots on next page

Thursday, February 28, 13

Page 6: Database Intro w/SQLite · 2013-03-01 · Database Intro w/SQLite How to put an embedded database on your phone and integrate with your application. deano@webenefitall.com Thursday,

Thursday, February 28, 13

Page 7: Database Intro w/SQLite · 2013-03-01 · Database Intro w/SQLite How to put an embedded database on your phone and integrate with your application. deano@webenefitall.com Thursday,

Create Database and Table

File > New Database

Enter name hit Save (put

Thursday, February 28, 13

Page 8: Database Intro w/SQLite · 2013-03-01 · Database Intro w/SQLite How to put an embedded database on your phone and integrate with your application. deano@webenefitall.com Thursday,

Create Table(s)

Edit > Create Table

enter Table name

Hit ADD to add fields to table

When all fields are added hit Create

Thursday, February 28, 13

Page 9: Database Intro w/SQLite · 2013-03-01 · Database Intro w/SQLite How to put an embedded database on your phone and integrate with your application. deano@webenefitall.com Thursday,

MUST HAVE...

Every Android database must have a table called android_metadata with one field called locale of type TEXT as seen below

Thursday, February 28, 13

Page 10: Database Intro w/SQLite · 2013-03-01 · Database Intro w/SQLite How to put an embedded database on your phone and integrate with your application. deano@webenefitall.com Thursday,

How to do this...Create table android_metadata in your database

Add field called locale of type TEXT

Set the default value of en_US into the locale field in the android_metadata table

See screenshot on next page...

Thursday, February 28, 13

Page 11: Database Intro w/SQLite · 2013-03-01 · Database Intro w/SQLite How to put an embedded database on your phone and integrate with your application. deano@webenefitall.com Thursday,

Enter this SQL string

Hit the Execute query button

Thursday, February 28, 13

Page 12: Database Intro w/SQLite · 2013-03-01 · Database Intro w/SQLite How to put an embedded database on your phone and integrate with your application. deano@webenefitall.com Thursday,

SQLITE Demo...create database using (sqlite browser)

I like to put a .db extension on file

create table

add column

note limited column types

be creative

Thursday, February 28, 13

Page 13: Database Intro w/SQLite · 2013-03-01 · Database Intro w/SQLite How to put an embedded database on your phone and integrate with your application. deano@webenefitall.com Thursday,

Linking TablesKeys are supported, but difficult to enable...

http://www.sqlite.org/foreignkeys.html#fk_enable

For most apps not needed. Just use common field names between tables. “Poor mans linking”

Thursday, February 28, 13

Page 14: Database Intro w/SQLite · 2013-03-01 · Database Intro w/SQLite How to put an embedded database on your phone and integrate with your application. deano@webenefitall.com Thursday,

Managing Dates

SQLite does not have a date/time field type

SQLite does have date time funtions, but stores its values in TEXT fields

YYYYMMDDHHmmSS

HHmmSS is optional

Thursday, February 28, 13

Page 15: Database Intro w/SQLite · 2013-03-01 · Database Intro w/SQLite How to put an embedded database on your phone and integrate with your application. deano@webenefitall.com Thursday,

How to put DB in AppDrag .db file into the assets directory in your project.

Create and run your Sample App

Thursday, February 28, 13

Page 16: Database Intro w/SQLite · 2013-03-01 · Database Intro w/SQLite How to put an embedded database on your phone and integrate with your application. deano@webenefitall.com Thursday,

Where did it go..DP_PATH = /data/data/[PACKAGE]/databases/

DB_NAME = name you gave your DB

Thursday, February 28, 13

Page 17: Database Intro w/SQLite · 2013-03-01 · Database Intro w/SQLite How to put an embedded database on your phone and integrate with your application. deano@webenefitall.com Thursday,

How can you get DB from App

Window > Show View > Other... > Android > File Explorer

Navigate to /data/data/[packageName]/databases

Then use the icon to copy db to computer

Thursday, February 28, 13

Page 18: Database Intro w/SQLite · 2013-03-01 · Database Intro w/SQLite How to put an embedded database on your phone and integrate with your application. deano@webenefitall.com Thursday,

Add Class to DB...Enter a Class Name

Enter a Class Number

Click Submit to insertdata into database

Thursday, February 28, 13

Page 19: Database Intro w/SQLite · 2013-03-01 · Database Intro w/SQLite How to put an embedded database on your phone and integrate with your application. deano@webenefitall.com Thursday,

Code Segments...!I will make the sample project available on MeetUp (somehow)

Thursday, February 28, 13

Page 20: Database Intro w/SQLite · 2013-03-01 · Database Intro w/SQLite How to put an embedded database on your phone and integrate with your application. deano@webenefitall.com Thursday,

Sample Application

Grade Book Application

Paper Prototype

Here is an example of a SQLite database with some tables and fields

Thursday, February 28, 13

Page 21: Database Intro w/SQLite · 2013-03-01 · Database Intro w/SQLite How to put an embedded database on your phone and integrate with your application. deano@webenefitall.com Thursday,

Questions...

Thursday, February 28, 13