1. Introduction:
Databases:
Databasesare an organized collectionof logicallyrelated data, whichcanbe easilyaccessedand
manipulatedusingadatabase systemmanager.There existseveral modelsof databases,most
notablyrelational databases.
Relational databases:
Relational databasesare databasesbasedonrelational model.Virtuallyall relational database
systemsuse SQL(StructuredQueryLanguage) formaintainingdatabases.
Relational model:
Relational model organisesdataintoa one or more tables,a table ismade of columnsand rows.
Each row is calledarecord andis identifiedbyaunique key.Columnsare calledattributes.Inmost
cases,a table representsone entitytype,forexample:Client,whererowsare the instancesof that
type for example:Ali.
DBMS (Database management system):
DBMS iscomputersoftware thatinteractswithend-users(anenduserissomeone whousesa
productor intendsto,opposite tothose whomaintainorsupportit.) Theyare basedondatabase
modelsbutall of themsupportrelational databases since1980. There existmanyDBMS but the most
popularsystemsare Oracle,MySQL,MicrosoftSQL server.
SQL (Structured Query Language):
SQL is domain-specificlanguage (alanguage that’susedinaspecificapplication,contrarytoa
general-purpose programminglanguages) usedinprogrammingandin relational databases
managementsystems.
SQL was originallybasedonrelationalalgebraand tuple relational calculus.SQLis divided intothree
mainelements(some people classifythemassublanguages)
Data Query Language (DQL): isusedto make queriesindatabase.
Data DefinitionLanguage (DDL): isusedto create and delete ormodifydatabasesandtables
Data control language (DCL): isusedto control access(permissions) todatastoredindatabases.
Data manipulationlanguage (DML):Is usedto insert,deleteandmodifying(updating) dataina
database
2. MySQL:
MySQL is an opensource Relational Database ManagementSystem,itwasownedandsponsoredby
MySQL AB, and nowisownedbyOracle.Several paid versionsexistwhich offeradditional
functionalities. ItiswidelyusedinmanybigwebsitessuchasGoogle,Facebook,Twitterand
YouTube.
MySQL supported types:
TINYINT.
SMALLINT.
MEDIUMINT.
INT or INTEGER.
BIGINT.
FLOAT.
DOUBLE, DOUBLE PRECISION,REAL: DECIMAL, NUMERIC.
DATE.
DATETIME.
TIMESTAMP.
TIME.
YEAR.
CHAR.
VARCHAR.
TINYBLOB,TINYTEXT.
BLOB, TEXT.
MEDIUMBLOB, MEDIUMTEXT.
LONGBLOB,LONGTEXT.
ENUM.
SET.
Advantages of MySQL:
Easy to workwith:
o MySQL can be installedveryeasily.
Feature rich:
o MySQL supportsa lot of SQL functionalities
Data security:
o It isknownto be most secure andreliable database managementsystem.
Scalability:
o MySQL can handle alotof data efficiently.
Speed:
o MySQL is knownforitsspeedandefficientwork.
Disadvantage of MySQL:
MySQL lack some featuresthatsome applicationmayrequire( itwasnot intendedtodo
everything)
3. Stagnateddevelopment: MySQLisstill anopen-source,buthasnotmuch improvedsince its
acquisition.
When touse MYSQL:
Highsecurity: whenyouneedhighsecuritytoprotectyourdata bases,mysql can offeravery
reliable protectionfordata-accessinasimple way.
When not to use MySQL:
Concurrency: AlthoughMySQLperformreallywell withreadoperations,concurrentread-write
operationscanbe problematic.
Lack of features: Since MySQL doesn’timplementfullSQLstandards,itlackssome feature thatyour
applicationmayneed.
SQLite:
A verypowerful embedded Relational DatabasesManagementSystem.ContrarytootherRDBMS,
SQLite isnot a client-serverDatabase engine. Butisinstead,embeddedintothe endapplication.
SQLite isverypopularchoice as an embedded-database adatabase whichisintegrateddirectlyinto
an applicationthatrequiresaccesstostoreddata,and ishiddenfromthe applicationuser) for
embedded systemssuchasmobile phones,SQLite isknowntobe efficientandfastdue to its
integrationdirectlyinthe application,itoffersavaried setof toolstohandle all sortof data with
much lessconstraints andease comparedtohosted,serverbaseddatabases.
SQLite supportedtypes:
NULL.
INTEGER.
REAL.
TEXT.
BLOB.
Advantage of SQLite:
File based:the data consistsof one single file storedonthe disk,whichmakesitextremelyportable
and easyto use.
Lightweight: SQLite isverylightweightdatabase whichcanusedinembeddedsystemslike Mobile
phones,TV,cameras .
Better performance: readingandwritingdata isveryfast inSQLite. Itonlyloadsdata it needsrather
than loadingthe whole file inmemory. Onlypartsthatare changedwill be overwritten.
No installationneeded: youjustneedtodownloadthe libraryandyouare readyto go.
Reliable: itcontinuouslyupdatescontent,notmuchworkislostdue to powerfailure orcrash.
Portable: it’sportable acrossall OS,and differentplatforms. Itcanalsobe usedwith many
programminglanguage withoutanycomputabilityissues.
4. Accessible: SQLite isaccessible throughwide varietyof thirdpartytools.
Reduce cost and complexity:Contentcanbe accessedand updatedeasily usingSQLqueries. Itcan
be easilyextendedjustbyaddingacolumn or two,andalso preserve backwardcompatibility
SQLite disadvantage:
Most databasesare limitedto2GB.
Cannothandle highhttptraffic.
When touse SQLite?
Embeddedapplications: all applicationsthat needportability,anddonotrequire extensionlike
mobile applications
Disk access replacement:applicationsthatwrite/readfilesinto/fromdiskdirectlycanswitchto
SQLite andbenefice fromthe functionalitiesandsimplicitythatSQLoffers.
Testing.
When not to use SQLite?
Workingan applicationwhere manyusersaccessto the same database at the same time,
Applicationsrequiringhighwriting volume:SQLite doesallow onlyone singlewrite operationatthe
same time (file locking).
Case study: Inserting username and password into
database.
Describingthe scheme of table “Login”:
private static final String createTable= "Create table if not exists
"+Clients.TABLE_NAME+" ( "+ Clients.ID + " integer PRIMARY KEY
AUTOINCREMENT,"+Clients.USER_COLUMN+ " text,"
+ Clients.PASS_COLUMN+ " text)";
Usingof static class DataBaseHelper to which is a subclass of SQLiteOpenHelpertohelpus manage
database (creating and upgrading database),then usingan objectof this subclassto insert, delete
and other operations.
private static class DataBaseHelper extends SQLiteOpenHelper{
Context context;
public DataBaseHelper(Context context){
super(context, DBName, null, DBVersion);
this.context = context;
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL(createTable);
}
5. We useda helperclass calledDBManger to manage all SQL queries: UsingmyDataBaseHelper
Object(instance ofDataBaseHelper) to create a WritableDatabase.
public DBManager(Context context){
myDataBaseHelper = new DataBaseHelper(context);
SQLdb = myDataBaseHelper.getWritableDatabase();
}
Performinginsertquery on the database.
public long insert(ContentValues values){
return SQLdb.insert(Clients.TABLE_NAME, "", values);
}
Insert iscalled from the mainActivity :
ContentValues values = new ContentValues();
values.put(DBManager.Clients.USER_COLUMN, usernameString);
values.put(DBManager.Clients.PASS_COLUMN, passwordString);
id = myDBManager.insert(values);