1+ /*
2+ *
3+ * * Copyright (C) 2016 Amit Shekhar
4+ * * Copyright (C) 2011 Android Open Source Project
5+ * *
6+ * * Licensed under the Apache License, Version 2.0 (the "License");
7+ * * you may not use this file except in compliance with the License.
8+ * * You may obtain a copy of the License at
9+ * *
10+ * * http://www.apache.org/licenses/LICENSE-2.0
11+ * *
12+ * * Unless required by applicable law or agreed to in writing, software
13+ * * distributed under the License is distributed on an "AS IS" BASIS,
14+ * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+ * * See the License for the specific language governing permissions and
16+ * * limitations under the License.
17+ *
18+ */
19+
20+ package com .sample .database ;
21+
22+ import android .app .Application ;
23+ import android .content .ContentValues ;
24+ import android .content .Context ;
25+ import android .database .Cursor ;
26+ import net .sqlcipher .DatabaseUtils ;
27+ import net .sqlcipher .database .SQLiteDatabase ;
28+ import net .sqlcipher .database .SQLiteOpenHelper ;
29+
30+ import java .util .ArrayList ;
31+
32+ public class PersonDBHelper extends SQLiteOpenHelper {
33+
34+ public static final String DATABASE_NAME = "Person.db" ;
35+ public static final String PERSON_TABLE_NAME = "person" ;
36+ public static final String PERSON_COLUMN_ID = "id" ;
37+ public static final String PERSON_COLUMN_FIRST_NAME = "first_name" ;
38+ public static final String PERSON_COLUMN_LAST_NAME = "last_name" ;
39+ public static final String PERSON_COLUMN_ADDRESS = "address" ;
40+ private static final String DB_PASSWORD = "a_password" ;
41+
42+ public PersonDBHelper (Context context ) {
43+
44+ super (context , DATABASE_NAME , null , 1 );
45+ SQLiteDatabase .loadLibs (context );
46+ }
47+
48+ @ Override
49+ public void onCreate (SQLiteDatabase db ) {
50+ db .execSQL (
51+ "create table person " +
52+ "(id integer primary key, first_name text, last_name text, address text)"
53+ );
54+ }
55+
56+ @ Override
57+ public void onUpgrade (SQLiteDatabase db , int oldVersion , int newVersion ) {
58+ db .execSQL ("DROP TABLE IF EXISTS person" );
59+ onCreate (db );
60+ }
61+
62+ public boolean insertPerson (String firstName , String lastName , String address ) {
63+ SQLiteDatabase db = this .getWritableDatabase (DB_PASSWORD );
64+ ContentValues contentValues = new ContentValues ();
65+ contentValues .put ("first_name" , firstName );
66+ contentValues .put ("last_name" , lastName );
67+ contentValues .put ("address" , address );
68+ db .insert ("person" , null , contentValues );
69+ db .close ();
70+ return true ;
71+ }
72+
73+ public Cursor getData (int id ) {
74+ SQLiteDatabase db = this .getReadableDatabase (DB_PASSWORD );
75+ Cursor res = db .rawQuery ("select * from person where id=" + id + "" , null );
76+ return res ;
77+ }
78+
79+ public int numberOfRows () {
80+ SQLiteDatabase db = this .getReadableDatabase (DB_PASSWORD );
81+ int numRows = (int ) DatabaseUtils .queryNumEntries (db , PERSON_TABLE_NAME );
82+ return numRows ;
83+ }
84+
85+ public boolean updatePerson (Integer id , String firstName , String lastName , String address , float mileage ) {
86+ SQLiteDatabase db = this .getWritableDatabase (DB_PASSWORD );
87+ ContentValues contentValues = new ContentValues ();
88+ contentValues .put ("first_name" , firstName );
89+ contentValues .put ("last_name" , lastName );
90+ contentValues .put ("address" , address );
91+ db .update ("person" , contentValues , "id = ? " , new String []{Integer .toString (id )});
92+ db .close ();
93+ return true ;
94+ }
95+
96+ public Integer deletePerson (Integer id ) {
97+ SQLiteDatabase db = this .getWritableDatabase (DB_PASSWORD );
98+ return db .delete ("person" ,
99+ "id = ? " ,
100+ new String []{Integer .toString (id )});
101+ }
102+
103+ public ArrayList <String > getAllPerson () {
104+ ArrayList <String > arrayList = new ArrayList <>();
105+
106+ SQLiteDatabase db = this .getReadableDatabase (DB_PASSWORD );
107+ Cursor res = db .rawQuery ("select * from person" , null );
108+ res .moveToFirst ();
109+
110+ while (!res .isAfterLast ()) {
111+ arrayList .add (
112+ res .getString (res .getColumnIndex (PERSON_COLUMN_FIRST_NAME )) + " " +
113+ res .getString (res .getColumnIndex (PERSON_COLUMN_LAST_NAME )));
114+ res .moveToNext ();
115+ }
116+ res .close ();
117+ db .close ();
118+ return arrayList ;
119+ }
120+
121+ public int count () {
122+ SQLiteDatabase db = getReadableDatabase (DB_PASSWORD );
123+ Cursor cursor = db .rawQuery ("select * from person" , null );
124+ try {
125+ if (cursor != null && cursor .getCount () > 0 ) {
126+ cursor .moveToFirst ();
127+ return cursor .getInt (0 );
128+ } else {
129+ return 0 ;
130+ }
131+ }
132+ finally {
133+ cursor .close ();
134+ db .close ();
135+ }
136+ }
137+ }
0 commit comments