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 .content .ContentValues ;
23+ import android .content .Context ;
24+ import android .database .Cursor ;
25+ import android .database .DatabaseUtils ;
26+ import android .database .sqlite .SQLiteDatabase ;
27+ import android .database .sqlite .SQLiteOpenHelper ;
28+
29+ import java .util .ArrayList ;
30+
31+ /**
32+ * Created by amitshekhar on 06/02/17.
33+ */
34+
35+ public class CarDBHelper extends SQLiteOpenHelper {
36+
37+ public static final String DATABASE_NAME = "Car.db" ;
38+ public static final String CARS_TABLE_NAME = "cars" ;
39+ public static final String CARS_COLUMN_ID = "id" ;
40+ public static final String CARS_COLUMN_NAME = "name" ;
41+ public static final String CARS_COLUMN_COLOR = "color" ;
42+ public static final String CCARS_COLUMN_MILEAGE = "mileage" ;
43+
44+ public CarDBHelper (Context context ) {
45+ super (context , DATABASE_NAME , null , 1 );
46+ }
47+
48+ @ Override
49+ public void onCreate (SQLiteDatabase db ) {
50+ // TODO Auto-generated method stub
51+ db .execSQL (
52+ "create table cars " +
53+ "(id integer primary key, name text, color text, mileage real)"
54+ );
55+ }
56+
57+ @ Override
58+ public void onUpgrade (SQLiteDatabase db , int oldVersion , int newVersion ) {
59+ // TODO Auto-generated method stub
60+ db .execSQL ("DROP TABLE IF EXISTS cars" );
61+ onCreate (db );
62+ }
63+
64+ public boolean insertCar (String name , String color , float mileage ) {
65+ SQLiteDatabase db = this .getWritableDatabase ();
66+ ContentValues contentValues = new ContentValues ();
67+ contentValues .put ("name" , name );
68+ contentValues .put ("color" , color );
69+ contentValues .put ("mileage" , mileage );
70+ db .insert ("cars" , null , contentValues );
71+ return true ;
72+ }
73+
74+ public Cursor getData (int id ) {
75+ SQLiteDatabase db = this .getReadableDatabase ();
76+ Cursor res = db .rawQuery ("select * from cars where id=" + id + "" , null );
77+ return res ;
78+ }
79+
80+ public int numberOfRows () {
81+ SQLiteDatabase db = this .getReadableDatabase ();
82+ int numRows = (int ) DatabaseUtils .queryNumEntries (db , CARS_TABLE_NAME );
83+ return numRows ;
84+ }
85+
86+ public boolean updateCar (Integer id , String name , String color , float mileage ) {
87+ SQLiteDatabase db = this .getWritableDatabase ();
88+ ContentValues contentValues = new ContentValues ();
89+ contentValues .put ("name" , name );
90+ contentValues .put ("color" , color );
91+ contentValues .put ("mileage" , mileage );
92+ db .update ("cars" , contentValues , "id = ? " , new String []{Integer .toString (id )});
93+ return true ;
94+ }
95+
96+ public Integer deleteCar (Integer id ) {
97+ SQLiteDatabase db = this .getWritableDatabase ();
98+ return db .delete ("cars" ,
99+ "id = ? " ,
100+ new String []{Integer .toString (id )});
101+ }
102+
103+ public ArrayList <String > getAllCars () {
104+ ArrayList <String > arrayList = new ArrayList <>();
105+
106+ //hp = new HashMap();
107+ SQLiteDatabase db = this .getReadableDatabase ();
108+ Cursor res = db .rawQuery ("select * from cars" , null );
109+ res .moveToFirst ();
110+
111+ while (res .isAfterLast () == false ) {
112+ arrayList .add (res .getString (res .getColumnIndex (CARS_COLUMN_NAME )));
113+ res .moveToNext ();
114+ }
115+ return arrayList ;
116+ }
117+
118+ public int count () {
119+ SQLiteDatabase db = getReadableDatabase ();
120+ Cursor cursor = db .rawQuery ("select * from cars" , null );
121+ if (cursor != null && cursor .getCount () > 0 ) {
122+ cursor .moveToFirst ();
123+ return cursor .getInt (0 );
124+ } else {
125+ return 0 ;
126+ }
127+ }
128+ }
0 commit comments