· 8 years ago · Jun 27, 2018, 04:46 PM
1package com.parkingticket;
2
3private static final String PARKEDCARS_TABLE = "ParkedCars";
4private static final String PARKINGMETERS_TABLE = "ParkingMeters";
5private static final String PARKINGTICKETS_TABLE = "ParkingTickets";
6private static final String POLICEOFFICERS_TABLE = "PoliceOfficers";
7
8// The name and column index for each column in PARKEDCARS
9public static final String KEY_CARID = "carID";
10 public static final int CARID_COLUMN = 0;
11public static final String KEY_CARMAKE = "Make";
12 public static final int CARMAKE_COLUMN = 1;
13public static final String KEY_CARMODEL = "Model";
14 public static final int CARMODEL_COLUMN = 2;
15public static final String KEY_CARCOLOR = "Color";
16 public static final int CARCOLOR_COLUMN = 3;
17public static final String KEY_CARLICENSENUMBER = "LicenseNumber";
18 public static final int CARLICENSENUMBER_COLUMN = 4;
19public static final String KEY_CARMINUTESPARKED = "MinutesParked";
20 public static final int CARMINUTESPARKED_COLUMN = 5;
21
22// The name and column index for each column in PARKINGMETERS
23public static final String KEY_METERID = "meterID";
24 public static final int METERID_COLUMN = 0;
25public static final String KEY_MINUTESPURCHASED = "MinutesPurchased";
26 public static final int MINUTESPURCHASED_COLUMN = 1;
27
28// The name and column index for each column in PARKINGTICKETS
29 //TODO create the columns and indexs for parking tickets
30
31// The name and column index for each column in POLICEOFFICERS
32public static final String KEY_OFFICERID = "officerID";
33 public static final int OFFICERID_COLUMN = 0;
34public static final String KEY_OFFICERNAME = "Name";
35 public static final int OFFICERNAME_COLUMN = 1;
36public static final String KEY_OFFICERBADGE = "BadgeNumber";
37 public static final int OFFICERBADE_COLUMN = 2;
38
39
40//Variable to hold the database instance
41private SQLiteDatabase ticketDB;
42
43//Context of the application using the database.
44private final Context context;
45
46//Database open/upgrade helper
47private TicketDBHelper ticketDBHelper;
48
49public TicketDBAdapter(Context _context)
50{
51 context = _context;
52 ticketDBHelper = new TicketDBHelper(context, DATABASE_NAME, null, DATABASE_VERSION);
53}
54
55public void open() throws SQLiteException
56{
57 try
58 {
59 ticketDB = ticketDBHelper.getWritableDatabase();
60 }
61 catch(SQLiteException ex)
62 {
63 ticketDB = ticketDBHelper.getReadableDatabase();
64 }
65}
66
67public void close()
68{
69 ticketDB.close();
70}
71
72 //Insert a new ParkedCar
73public long insertParkedCar(ParkedCar _car)
74{
75 //Create a new row of values to insert
76 ContentValues newParkedCarValues = new ContentValues();
77
78 //Assign values for each row
79 newParkedCarValues.put(KEY_CARMAKE, _car.getMake());
80 newParkedCarValues.put(KEY_CARMODEL, _car.getModel());
81 newParkedCarValues.put(KEY_CARCOLOR, _car.getColor());
82 newParkedCarValues.put(KEY_CARLICENSENUMBER, _car.getLicenseNumber());
83 newParkedCarValues.put(KEY_CARMINUTESPARKED, _car.getMinutesParked());
84
85 //Insert the row
86 return ticketDB.insert(PARKEDCARS_TABLE, null, newParkedCarValues);
87}
88
89//Remove a ParkedCar based on its index
90public boolean removeParkedCar(long _rowIndex)
91{
92 return ticketDB.delete(PARKEDCARS_TABLE, KEY_CARID + "=" + _rowIndex, null)>0;
93}
94
95//Update a ParkedCar's MinutesParked
96//TODO Create an update for ParkedCar's minutesParked.
97
98public Cursor getAllParkedCarsCursor()
99{
100 return ticketDB.query(PARKEDCARS_TABLE, new String[] {KEY_CARID, KEY_CARMAKE, KEY_CARMODEL, KEY_CARCOLOR, KEY_CARLICENSENUMBER, KEY_CARMINUTESPARKED}, null, null, null, null, null);
101}
102
103public Cursor setCursorParkedCar(long _rowIndex) throws SQLException
104{
105 Cursor result = ticketDB.query(true, PARKEDCARS_TABLE, new String []{KEY_CARID}, KEY_CARID + "=" + _rowIndex, null, null, null, null, null);
106
107 if ((result.getCount() == 0) || !result.moveToFirst())
108 {
109 throw new SQLException("No ParkedCar found for row: " + _rowIndex);
110 }
111
112 return result;
113}
114
115public static class TicketDBHelper extends SQLiteOpenHelper
116{
117 public TicketDBHelper(Context context, String name, CursorFactory factory, int version)
118 {
119 super(context, name, factory, version);
120 }
121
122 //SQL Statement to create PARKEDCARS table
123 private static final String PARKEDCARS_CREATE = "create table " + PARKEDCARS_TABLE + " (" + KEY_CARID + " integer primary key autoincrement, " + KEY_CARMAKE + " text not null," + KEY_CARMODEL + " text not null," + KEY_CARCOLOR + " text not null," + KEY_CARLICENSENUMBER + " text not null," + KEY_CARMINUTESPARKED + "int not null);";
124
125 //SQL Statement to create ParkingMeters table
126 private static final String PARKINGMETERS_CREATE = "create table" + PARKINGMETERS_TABLE + " (" + KEY_METERID + " integer primary key autoincrement, " + KEY_MINUTESPURCHASED + " int not null);";
127
128 //SQL Statement to create ParkingTickets table
129 //TODO create the statement for parkingTickets
130
131 //SQL Statement to create PoliceOfficers table
132 private static final String POLICEOFFICERS_CREATE = "create table" + POLICEOFFICERS_TABLE + " (" + KEY_OFFICERID + " integer primary key autoincrement, " + KEY_OFFICERNAME + " text not null," + KEY_OFFICERBADGE + "text not null);";
133
134 //Called when no database exists in disk and the helper class needs to create a new one.
135 @Override
136 public void onCreate(SQLiteDatabase _db)
137 {
138
139//Called when there is a database verion mismatch meaning that the version of the database on disk needs to be upgraded to the current version
140 @Override
141 public void onUpgrade(SQLiteDatabase _db, int _oldVersion, int _newVersion)
142 {
143 //Log the version upgrade.
144 Log.w("TaskDBAdapter", "Upgrading from version " + _oldVersion + " to " + _newVersion + ", which will destroy all old data");
145
146 //Upgrade the existing database to conform to the new version
147 //Multiple previous versions can be handled by comparing _oldVersoin and _newVersion values
148
149 //The simplest case is to drop teh old table and create a new one.
150 _db.execSQL("DROP TABLE IF EXISTS " + PARKEDCARS_TABLE);
151 _db.execSQL("DROP TABLE IF EXISTS " + PARKINGMETERS_TABLE);
152 _db.execSQL("DROP TABLE IF EXISTS " + POLICEOFFICERS_TABLE);
153
154 onCreate(_db);
155 }
156}
157
158public DbAdapter open() throws SQLException {
159 mDbHelper = new DatabaseHelper(mCtx);
160 mDb = mDbHelper.getWritableDatabase();
161 return this;
162}