· 5 years ago · Jun 06, 2020, 06:08 PM
1package com.example.myapplication;
2
3import android.content.ContentValues;
4import android.content.Context;
5import android.database.Cursor;
6import android.database.SQLException;
7import android.database.sqlite.SQLiteDatabase;
8import android.database.sqlite.SQLiteOpenHelper;
9import android.util.Log;
10//https://www.journaldev.com/9438/android-sqlite-database-example-tutorial#opening-the-android-sqlite-database-file
11public class DBAdapter2 {
12 static final String KEY_ROWID = "_id";
13 static final String KEY_PLATE = "plate";
14 static final String KEY_OWNER = "owner";
15 static final String KEY_PAY = "pay";
16 static final String KEY_YEAR = "year";
17 static final String TAG = "DBAdapter";
18 static final String DATABASE_NAME = "MyDB";
19 static final String DATABASE_TABLE = "cars";
20 static final int DATABASE_VERSION = 1;
21 static final String DATABASE_CREATE =
22 "create table cars (_id integer primary key autoincrement, "
23 + "plate text not null, owner text not null, pay text not null, year text not null);";
24 private Context context;
25 DatabaseHelper DBHelper;
26 SQLiteDatabase db;
27 public DBAdapter2(Context ctx)
28 {
29 this.context = ctx;
30 DBHelper = new DatabaseHelper(context);
31 }
32 private static class DatabaseHelper extends SQLiteOpenHelper
33 {
34 DatabaseHelper(Context context)
35 {
36 super(context, DATABASE_NAME, null, DATABASE_VERSION);
37 }
38 @Override
39 public void onCreate(SQLiteDatabase db)
40 {
41 try {
42 db.execSQL(DATABASE_CREATE);
43 } catch (SQLException e) {
44 e.printStackTrace();
45 }
46 }
47 @Override
48 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
49 {
50 Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
51 + newVersion + ", which will destroy all old data");
52 db.execSQL("DROP TABLE IF EXISTS cars");
53 onCreate(db);
54 }
55 }
56 //---opens the database---
57 public DBAdapter2 open() throws SQLException
58 {
59 db = DBHelper.getWritableDatabase();
60 return this;
61 }
62 //---closes the database---
63 public void close()
64 {
65 DBHelper.close();
66 }
67 //---insert a contact into the database---
68 public long insertContact(String plate, String owner, String pay,String year)
69 {
70 ContentValues initialValues = new ContentValues();
71 initialValues.put(KEY_PLATE, plate);
72 initialValues.put(KEY_OWNER, owner);
73 initialValues.put(KEY_PAY, pay);
74 initialValues.put(KEY_YEAR, year);
75 return db.insert(DATABASE_TABLE, null, initialValues);
76 }
77 //---deletes a particular contact---
78 public boolean deleteContact(long rowId)
79 {
80 return db.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
81 }
82 public boolean deleteall()
83 {
84 return db.delete(DATABASE_TABLE,null,null ) > 0;
85 }
86 //---retrieves all the contacts---
87 public Cursor getAllContacts()
88 {
89 return db.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_PLATE,
90 KEY_OWNER,KEY_PAY,KEY_YEAR}, null, null, null, null, null);
91 }
92 //---retrieves a particular contact---
93 public Cursor getContact(long rowId) throws SQLException
94 {
95 Cursor mCursor =
96 db.query(true, DATABASE_TABLE, new String[] {KEY_ROWID,
97 KEY_PLATE, KEY_OWNER,KEY_PAY,KEY_YEAR},
98 KEY_ROWID + "=" + rowId, null,
99 null, null, null, null);
100 if (mCursor != null) {
101 mCursor.moveToFirst();
102 }
103 return mCursor;
104 }
105 //---updates a contact---
106 public boolean updateContact(long rowId, String plate, String owner,String pay,String year)
107 {
108 ContentValues args = new ContentValues();
109 args.put(KEY_PLATE, plate);
110 args.put(KEY_OWNER, owner);
111 args.put(KEY_PAY, pay);
112 args.put(KEY_YEAR, year);
113 return db.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) >
114 0;
115 }
116}