· 7 years ago · Oct 07, 2018, 06:08 PM
1package com.android.example.wordlistsqlsearchable;
2
3import android.database.sqlite.SQLiteOpenHelper;
4
5import switchDatabase;
6import mysqlConnection;
7
8
9
10public class WordListAdapter extends SQLiteOpenHelper {
11
12mysqlConnection.switchDatabase("your_db_name",new
13
14 IConnectionInterface() {
15 @Override public void actionCompleted () {
16 //Database switched successfully
17 }
18
19 @Override public void handleInvalidSQLPacketException (InvalidSQLPacketException e){
20 errorHandler.handleInvalidSQLPacketException(e);
21 }
22
23 @Override public void handleMySQLException (MySQLException e){
24 errorHandler.handleMySQLException(e);
25 }
26
27 @Override public void handleIOException (IOException e){
28 errorHandler.handleIOException(e);
29 }
30
31 @Override public void handleMySQLConnException (MySQLConnException e){
32 errorHandler.handleMySQLConnException(e);
33 }
34
35 @Override public void handleException (Exception e){
36 errorHandler.handleGeneralException(e);
37 }
38 });
39
40
41 Statement statement = mysqlConnection.createStatement();
42 statement.execute("CREATE DATABASE my_new_db",new
43
44 IConnectionInterface() {
45 @Override public void actionCompleted () {
46 //action completed successfully
47 }
48
49 @Override public void handleInvalidSQLPacketException (InvalidSQLPacketException e)
50 {
51 errorHandler.handleInvalidSQLPacketException(e);
52 }
53
54 @Override public void handleMySQLException (MySQLException e)
55 {
56 errorHandler.handleMySQLException(e);
57 }
58
59 @Override public void handleIOException (IOException e)
60 {
61 errorHandler.handleIOException(e);
62 }
63
64 @Override public void handleMySQLConnException (MySQLConnException e)
65 {
66 errorHandler.handleMySQLConnException(e);
67 }
68
69 @Override public void handleException (Exception e)
70 {
71 errorHandler.handleGeneralException(e);
72 }
73 });
74
75
76 Statement statement = mysqlConnection.createStatement();
77 statement.executeQuery("SELECT * FROM my_table",new
78
79 IResultInterface() {
80 @Override public void executionComplete (ResultSet resultSet)
81 {
82 addHistoryRecord(new QueryHistory(QueryHistory.Status.OK, txtQuery.getText().toString(), "Fetched " + resultSet.getNumRows() + " Row(s)", statement.getQueryTimeInMilliseconds()));
83 processResultset(resultSet);
84 }
85
86 @Override public void handleInvalidSQLPacketException (InvalidSQLPacketException e)
87 {
88 Log.e("DBViewFragment", e.toString());
89 addHistoryRecord(new QueryHistory(QueryHistory.Status.ERROR, txtQuery.getText().toString(), e.toString(), statement.getQueryTimeInMilliseconds()));
90 errorHandler.handleInvalidSQLPacketException(e);
91
92 }
93
94 @Override public void handleMySQLException (MySQLException e)
95 {
96 Log.e("DBViewFragement", e.toString());
97 addHistoryRecord(new QueryHistory(QueryHistory.Status.ERROR, txtQuery.getText().toString(), e.toString(), statement.getQueryTimeInMilliseconds()));
98 errorHandler.handleMySQLException(e);
99 }
100
101 @Override public void handleIOException (IOException e)
102 {
103 Log.e("DBViewFragement", e.toString());
104 addHistoryRecord(new QueryHistory(QueryHistory.Status.ERROR, txtQuery.getText().toString(), e.toString(), statement.getQueryTimeInMilliseconds()));
105 errorHandler.handleIOException(e);
106 }
107
108 @Override public void handleMySQLConnException (MySQLConnException e)
109 {
110 Log.e("DBViewFragement", e.toString());
111 addHistoryRecord(new QueryHistory(QueryHistory.Status.ERROR, txtQuery.getText().toString(), e.toString(), statement.getQueryTimeInMilliseconds()));
112 errorHandler.handleMySQLConnException(e);
113 }
114
115 @Override public void handleException (Exception e)
116 {
117 Log.e("DBViewFragement", e.toString());
118 addHistoryRecord(new QueryHistory(QueryHistory.Status.ERROR, txtQuery.getText().toString(), e.toString(), statement.getQueryTimeInMilliseconds()));
119 errorHandler.handleGeneralException(e);
120 }
121 });
122}
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138//package com.android.example.wordlistsqlsearchable;
139//
140//import android.content.ContentValues;
141//import android.content.Context;
142//import android.database.Cursor;
143//import android.database.DatabaseUtils;
144//import android.database.sqlite.SQLiteDatabase;
145//import android.database.sqlite.SQLiteOpenHelper;
146//import android.util.Log;
147//
148//import com.BoardiesITSolutions.AndroidMySQLConnector.Connection;
149//import com.BoardiesITSolutions.AndroidMySQLConnector.Exceptions.InvalidSQLPacketException;
150//import com.BoardiesITSolutions.AndroidMySQLConnector.Exceptions.MySQLConnException;
151//import com.BoardiesITSolutions.AndroidMySQLConnector.Exceptions.MySQLException;
152//import com.BoardiesITSolutions.AndroidMySQLConnector.Statement;
153//
154//import java.io.IOException;
155//
156///**
157// * Open helper for the list of words database.
158// */
159//public class WordListOpenHelper /* extends SQLiteOpenHelper */ {
160//
161// private static final String TAG = WordListOpenHelper.class.getSimpleName();
162//
163// // Declaring all these as constants makes code a lot more readable and looking like SQL.
164//
165// // Version has to be 1 first time or app will crash.
166// private static final int DATABASE_VERSION = 1;
167// private static final String WORD_LIST_TABLE = "NotesTable";
168// private static final String DATABASE_NAME = "notesdb";
169//
170// // Column names...
171// public static final String KEY_ID = "id";
172// public static final String KEY_TITLE = "title";
173//
174// // ... and a string array of columns.
175// private static final String[] COLUMNS =
176// {KEY_ID, KEY_TITLE};
177//
178// // Build the SQL query that creates the table.
179// private static final String WORD_LIST_TABLE_CREATE =
180// "CREATE TABLE " + WORD_LIST_TABLE + " (" +
181// KEY_ID + " INTEGER PRIMARY KEY, " + // will auto-increment if no value passed
182// KEY_TITLE + " TEXT );";
183//
184//// next for local variant
185//// private SQLiteDatabase mWritableDB;
186//// private SQLiteDatabase mReadableDB;
187// private Connection mWriteableDB, mReadableDB;
188//
189// public WordListOpenHelper(Context context) {
190// // super(context, DATABASE_NAME, null, DATABASE_VERSION);
191// Log.d(TAG, "Construct WordListOpenHelper");
192// mWriteableDB= new Connection("192.168.0.12", "boot", "sed123sed", 3307, "notesdb", new MainActivity.MyConnectionHandler());
193// mReadableDB = new Connection("192.168.0.12", "boot", "sed123sed", 3307, "notesdb", new MainActivity.MyConnectionHandler());
194// }
195//
196// // @Override
197// public void onCreate(SQLiteDatabase db) {
198// db.execSQL(WORD_LIST_TABLE_CREATE);
199// fillDatabaseWithData(db);
200// }
201//
202// /**
203// * Adds the initial data set to the database.
204// * According to the docs, onCreate for the open helper does not run on the UI thread.
205// *
206// * @param db Database to fill with data since the member variables are not initialized yet.
207// */
208// public void fillDatabaseWithData(SQLiteDatabase db) {
209//
210// String[] words = {"Android", "Adapter", "ListView", "AsyncTask", "Android Studio",
211// "SQLiteDatabase", "SQLOpenHelper", "Data model", "ViewHolder",
212// "Android Performance", "OnClickListener"};
213//
214// // Create a container for the data.
215// ContentValues values = new ContentValues();
216//
217// for (int i=0; i < words.length; i++) {
218// // Put column/value pairs for current row into the container.
219// values.put(KEY_TITLE, words[i]); // put() overrides existing values.
220// // Insert the row.
221// db.insert(WORD_LIST_TABLE, null, values);
222// }
223// }
224//
225// Connection getReadableDatabase() {
226// return mReadableDB;
227// }
228// Connection getWritableDatabase() {
229// return mWriteableDB;
230// }
231// public Cursor search(String searchString) {
232// String[] columns = new String[]{KEY_TITLE};
233// String where = KEY_TITLE + " LIKE ?";
234// searchString = "%" + searchString + "%";
235// String[] whereArgs = new String[]{searchString};
236//
237// Cursor cursor = null;
238// try {
239// if (mReadableDB == null) {
240// mReadableDB = getReadableDatabase();
241// }
242// Statement s = new Statement();
243// s.execute("SELECT * FROM NotesTable", );
244// cursor = mReadableDB.query(WORD_LIST_TABLE, columns, where, whereArgs, null, null, null);
245// } catch (Exception e) {
246// Log.d(TAG, "SEARCH EXCEPTION! " + e); // Just log the exception
247// }
248// return cursor;
249// }
250//
251//
252// /**
253// * Queries the database for an entry at a given position.
254// *
255// * @param position The Nth row in the table.
256// * @return a WordItem with the requested database entry.
257// */
258// public WordItem query(int position) {
259// String query = "SELECT * FROM " + WORD_LIST_TABLE +
260// " ORDER BY " + TITLE + " ASC " +
261// "LIMIT " + position + ",1";
262//
263// Cursor cursor = null;
264// WordItem entry = new WordItem();
265//
266// try {
267// if (mReadableDB == null) {
268// mReadableDB = getReadableDatabase();
269// }
270// cursor = mReadableDB.rawQuery(query, null);
271// cursor.moveToFirst();
272// entry.setId(cursor.getInt(cursor.getColumnIndex(KEY_ID)));
273// entry.setWord(cursor.getString(cursor.getColumnIndex(KEY_TITLE)));
274// } catch (Exception e) {
275// Log.d(TAG, "QUERY EXCEPTION! " + e); // Just log the exception
276// } finally {
277// // Must close cursor and db now that we are done with it.
278// cursor.close();
279// return entry;
280// }
281// }
282//
283// /**
284// * Gets the number of rows in the word list table.
285// *
286// * @return The number of entries in WORD_LIST_TABLE.
287// */
288// public long count() {
289// if (mReadableDB == null) {
290// mReadableDB = getReadableDatabase();
291// }
292// return DatabaseUtils.queryNumEntries(mReadableDB, WORD_LIST_TABLE);
293// }
294//
295// /**
296// * Adds a single word row/entry to the database.
297// *
298// * @param word New word.
299// * @return The id of the inserted word.
300// */
301// public long insert(String word) {
302// long newId = 0;
303// ContentValues values = new ContentValues();
304// values.put(KEY_TITLE, word);
305// try {
306// if (mWritableDB == null) {
307// mWritableDB = getWritableDatabase();
308// }
309// newId = mWritableDB.insert(WORD_LIST_TABLE, null, values);
310// } catch (Exception e) {
311// Log.d(TAG, "INSERT EXCEPTION! " + e);
312// }
313// return newId;
314// }
315//
316// /**
317// * Updates the word with the supplied id to the supplied value.
318// *
319// * @param id Id of the word to update.
320// * @param word The new value of the word.
321// * @return the number of rows affected or -1 of nothing was updated.
322// */
323// public int update(int id, String word) {
324// int mNumberOfRowsUpdated = -1;
325// try {
326// if (mWritableDB == null) {
327// mWritableDB = getWritableDatabase();
328// }
329// ContentValues values = new ContentValues();
330// values.put(KEY_TITLE, word);
331//
332// mNumberOfRowsUpdated = mWritableDB.update(WORD_LIST_TABLE, //table to change
333// values, // new values to insert
334// KEY_ID + " = ?", // selection criteria for row (in this case, the _id column)
335// new String[]{String.valueOf(id)}); //selection args; the actual value of the id
336//
337// } catch (Exception e) {
338// Log.d (TAG, "UPDATE EXCEPTION! " + e);
339// }
340// return mNumberOfRowsUpdated;
341// }
342//
343// /**
344// * Deletes one entry identified by its id.
345// *
346// * @param id ID of the entry to delete.
347// * @return The number of rows deleted. Since we are deleting by id, this should be 0 or 1.
348// */
349// public int delete(int id) {
350// int deleted = 0;
351// try {
352// if (mWritableDB == null) {
353// mWritableDB = getWritableDatabase();
354// }
355// deleted = mWritableDB.delete(WORD_LIST_TABLE, //table name
356// KEY_ID + " =? ", new String[]{String.valueOf(id)});
357// } catch (Exception e) {
358// Log.d (TAG, "DELETE EXCEPTION! " + e); }
359// return deleted;
360// }
361//
362// /**
363// * Called when a database needs to be upgraded. The most basic version of this method drops
364// * the tables, and then recreates them. All data is lost, which is why for a production app,
365// * you want to back up your data first. If this method fails, changes are rolled back.
366// *
367// * @param db
368// * @param oldVersion
369// * @param newVersion
370// */
371// @Override
372// public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
373// Log.w(WordListOpenHelper.class.getName(),
374// "Upgrading database from version " + oldVersion + " to "
375// + newVersion + ", which will destroy all old data");
376// db.execSQL("DROP TABLE IF EXISTS " + WORD_LIST_TABLE);
377// onCreate(db);
378//
379//
380// }
381//
382// class MyConnectionHandler implements com.BoardiesITSolutions.AndroidMySQLConnector.IConnectionInterface
383// {
384// public void actionCompleted() {
385// Log.i(TAG, "action completed");
386// }
387// public void handleInvalidSQLPacketException(InvalidSQLPacketException ex)
388// {
389// Log.e(TAG, "EX_1");
390// }
391// public void handleMySQLException(MySQLException ex) {
392// Log.e(TAG, "EX_2");
393// }
394// public void handleIOException(IOException ex) {
395// Log.e(TAG, "EX_3");
396// }
397// public void handleMySQLConnException(MySQLConnException ex) {
398// Log.e(TAG, "EX_4");
399// }
400// public void handleException(Exception exception) {
401// Log.e(TAG, "EX_5");
402// }
403// }
404//}
405//