· 8 years ago · Aug 17, 2018, 11:18 PM
1public class AddBooks extends BaseActivity implements View.OnClickListener {
2
3private EditText mBookTitle, mBookAuthor, mBookISBN, mBookDate;
4private Button upload;
5private MySQLiteHelper db = new MySQLiteHelper(this);
6private String title, author, isbn, date;
7
8@Override
9protected void onCreate(@Nullable Bundle savedInstanceState) {
10 super.onCreate(savedInstanceState);
11 setContentView(R.layout.add_books);
12
13
14 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
15 if (getSupportActionBar() != null)
16 (getSupportActionBar()).setDisplayHomeAsUpEnabled(true);
17 }
18
19 mBookTitle = findViewById(R.id.book_title_edit);
20 mBookAuthor = findViewById(R.id.book_author_edit);
21 mBookISBN = findViewById(R.id.book_isbn_edit);
22 mBookDate = findViewById(R.id.book_date_edit);
23 mBookDate.setOnClickListener(this);
24
25 upload = findViewById(R.id.book_upload);
26 upload.setOnClickListener(this);
27
28}
29
30private boolean checkField() {
31 //get text from the editText fields
32 boolean valid = true;
33 View focusView = null;
34
35 title = mBookTitle.getText().toString().trim();
36 author = mBookAuthor.getText().toString().trim();
37 isbn = mBookISBN.getText().toString().trim();
38 date = mBookDate.getText().toString();
39
40 //gives error report if the editText field is empty
41 if (TextUtils.isEmpty(title)) {
42 mBookTitle.setError(getString(R.string.empty_title));
43 focusView = mBookTitle;
44 valid = false;
45 }
46 if (TextUtils.isEmpty(author)) {
47 mBookAuthor.setError(getString(R.string.empty_author));
48 focusView = mBookAuthor;
49 valid = false;
50 }
51 if (!valid) {
52 focusView.requestFocus();
53 }
54 return valid;
55}
56
57@Override
58public void onClick(View v) {
59 //dialog popup for date selection on editText click
60 if (v == mBookDate) {
61 final Calendar calendar = Calendar.getInstance();
62 int mYear = calendar.get(Calendar.YEAR);
63 int mMonth = calendar.get(Calendar.MONTH);
64 int mDay = calendar.get(Calendar.DAY_OF_MONTH);
65
66 DatePickerDialog datePickerDialog = new DatePickerDialog(this,
67 new DatePickerDialog.OnDateSetListener() {
68 @Override
69 public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
70 mBookDate.setText(dayOfMonth + "-" + (month + 1) + "-" + year);
71 }
72 }, mYear, mMonth, mDay);
73 datePickerDialog.show();
74 }
75
76 if (v == upload) {
77 if (checkField()) {
78 Book book = new Book(author, title, isbn, date);
79
80 //add books to the database
81 db.addBook(book);
82
83 Toast.makeText(this, getString(R.string.success_upload), Toast.LENGTH_SHORT).show();
84 mBookDate.setText("");
85 mBookTitle.setText("");
86 mBookISBN.setText("");
87 mBookAuthor.setText("");
88 }
89 }
90}
91
92/**
93 * Take care of popping the fragment back stack or finishing the activity
94 * as appropriate.
95 */
96@Override
97public void onBackPressed() {
98 super.onBackPressed();
99}
100
101public class MySQLiteHelper extends SQLiteOpenHelper {
102
103
104// Database Name - BookDB
105private static final String DATABASE_NAME = "BookDB";
106
107// Table Name - books and users
108private static final String TABLE_BOOKS = "books";
109
110
111// Columns Names of books Table
112private static final String KEY_ID = "id";
113private static final String KEY_TITLE = "title";
114private static final String KEY_AUTHOR = "author";
115private static final String KEY_ISBN = "isbn";
116private static final String KEY_DATE_PUB = "date";
117
118
119// Database Version
120private static final int DATABASE_VERSION = 1;
121
122// Log TAG for debugging purpose
123private static final String TAG = "SQLiteDebugLog";
124
125// Constructor
126public MySQLiteHelper(Context context) {
127 super(context, DATABASE_NAME, null, DATABASE_VERSION);
128}
129
130
131@Override
132public void onCreate(SQLiteDatabase db) {
133String CREATE_BOOK_TABLE = "CREATE TABLE books ( " + "id INTERGER PRIMARY KEY AUTOINCREMENT, " +
134 "author TEXT, " + "title TEXT, " + "isbn TEXT, " + "date TEXT NOT NULL)";
135
136
137 // execute an SQL statement to create the table
138 db.execSQL(CREATE_BOOK_TABLE);
139}
140
141// onUpdate() is invoked when you upgrade the database scheme.
142
143@Override
144public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
145 // Drop older books table if existed
146 db.execSQL("DROP TABLE IF EXISTS books");
147
148 this.onCreate(db);
149}
150//******************************METHODS FOR BOOK*******************************************
151
152public void addBook(Book book){
153 Log.d(TAG, "addBook() - " + book.toString());
154 // get reference to writable DB
155 SQLiteDatabase db = this.getWritableDatabase();
156
157 // create ContentValues to add key "column"/value
158 ContentValues values = new ContentValues();
159 values.put(KEY_AUTHOR, book.getAuthor()); // get author
160 values.put(KEY_TITLE, book.getTitle()); // get title
161 values.put(KEY_ISBN, book.getIsbn()); // get isbn
162 values.put(KEY_DATE_PUB, book.getDatePublish());
163
164 //print the book details
165 System.out.println("set title: " + book.getTitle());
166 System.out.println("set author: " + book.getAuthor());
167 System.out.println("set isbn: "+ book.getIsbn());
168 System.out.println("set date: " + book.getDatePublish());
169 // insert
170 db.insert(TABLE_BOOKS, // table
171 null, //nullColumnHack
172 values); // key/value -> keys = column names/ values = column values
173
174 // 4. close - release the reference of writable DB
175 db.close();
176}
177
178D/SQLiteDebugLog: addBook() - com.android.nsuklib.data.Book@1c8cf0f
179I/System.out: set title: cool things
180 set author: Jamse
181I/System.out: set isbn: REISUEY
182 set date: 17-8-2018
183E/SQLiteLog: (1) table books has no column named date
184E/SQLiteDatabase: Error inserting date=17-8-2018 title=cool things author=Jamse isbn=REISUEY
185 android.database.sqlite.SQLiteException: table books has no column named date (code 1): , while compiling: INSERT INTO books(date,title,author,isbn) VALUES (?,?,?,?)
186 at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
187 at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
188 at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
189 at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
190 at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
191 at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
192 at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1472)
193 at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1343)
194 at com.android.nsuklib.data.MySQLiteHelper.addBook(MySQLiteHelper.java:116)
195 at com.android.nsuklib.data.AddBooks.onClick(AddBooks.java:99)
196 at android.view.View.performClick(View.java:6256)
197 at android.view.View$PerformClick.run(View.java:24697)
198 at android.os.Handler.handleCallback(Handler.java:789)
199 at android.os.Handler.dispatchMessage(Handler.java:98)
200 at android.os.Looper.loop(Looper.java:164)
201 at android.app.ActivityThread.main(ActivityThread.java:6541)
202 at java.lang.reflect.Method.invoke(Native Method)
203 at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
204 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
205E/EGL_emulation: tid 10741: eglSurfaceAttrib(1210): error 0x3009 (EGL_BAD_MATCH)