Jon Snow
- Forum posts: 4
Nov 14, 2011, 3:03:37 AM via Website
Nov 14, 2011 3:03:37 AM via Website
Hi All, I'm hoping I can get some help/feedback on this, any contributions are appreciated.
I am currently trying to develop an app that will essentially take some text that a user has entered in 'SMS speak' and then search my SQLite3 database and return the same txt in proper English simply by matching and replacing any word for word entries that can be located. If there aren't any matches for certain words then the original SMS wording can remain untouched in the result.
I am developing this in eclipse with the Android SDK plugin and I have set my emulator to the latest version 4.0. I have an SQLite3 database of about 1500-2000 words and I have the adapter class in place (as shown below with template that I will be using as I believe this will suit my purpose when I have entered my own methods?)...
This is my first attempt at creating an Android App and I have been learning largely from youtube videos etc. I have formatted the SQLite database in the correct style for an Android App and it is stored in the assets folder correctly but I am stuck on how to actually implement the method for taking the users entry text and translating it in the way I have previously described. The GUI etc doesn't need to be very sophisticated for now I can deal with that when I have actually got the basics working.
My SQLite file looks like this
_id SMS English
1 *$ Starbucks
2 121 One to One
3 4eva Forever
Any help would be great, thanks!
I am currently trying to develop an app that will essentially take some text that a user has entered in 'SMS speak' and then search my SQLite3 database and return the same txt in proper English simply by matching and replacing any word for word entries that can be located. If there aren't any matches for certain words then the original SMS wording can remain untouched in the result.
I am developing this in eclipse with the Android SDK plugin and I have set my emulator to the latest version 4.0. I have an SQLite3 database of about 1500-2000 words and I have the adapter class in place (as shown below with template that I will be using as I believe this will suit my purpose when I have entered my own methods?)...
This is my first attempt at creating an Android App and I have been learning largely from youtube videos etc. I have formatted the SQLite database in the correct style for an Android App and it is stored in the assets folder correctly but I am stuck on how to actually implement the method for taking the users entry text and translating it in the way I have previously described. The GUI etc doesn't need to be very sophisticated for now I can deal with that when I have actually got the basics working.
My SQLite file looks like this
_id SMS English
1 *$ Starbucks
2 121 One to One
3 4eva Forever
Any help would be great, thanks!
1package com.edu4java.android;
2
3import java.io.FileOutputStream;
4import java.io.IOException;
5import java.io.InputStream;
6import java.io.OutputStream;
7import java.util.UUID;
8
9import android.content.Context;
10import android.database.Cursor;
11import android.database.SQLException;
12import android.database.sqlite.SQLiteDatabase;
13import android.database.sqlite.SQLiteException;
14import android.database.sqlite.SQLiteOpenHelper;
15import android.util.Log;
16
17public class AnyDBAdapter {
18
19 private static final String TAG = "AnyDBAdapter";
20 private DatabaseHelper mDbHelper;
21 private static SQLiteDatabase mDb;
22
23 //make sure this matches the
24 //package com.MyPackage;
25 //at the top of this file
26 private static String DB_PATH = "/data/data/com.edu4java.android/databases/";
27
28 //make sure this matches your database name in your assets folder
29 // my database file does not have an extension on it
30 // if yours does
31 // add the extention
32 private static final String DATABASE_NAME = "smsenglish.sqlite";
33
34 //Im using an sqlite3 database, I have no clue if this makes a difference or not
35 private static final int DATABASE_VERSION = 3;
36
37 private final Context adapterContext;
38
39 public AnyDBAdapter(Context context) {
40 this.adapterContext = context;
41 }
42
43 public AnyDBAdapter open() throws SQLException {
44 mDbHelper = new DatabaseHelper(adapterContext);
45
46 try {
47 mDbHelper.createDataBase();
48 } catch (IOException ioe) {
49 throw new Error("Unable to create database");
50 }
51
52 try {
53 mDbHelper.openDataBase();
54 } catch (SQLException sqle) {
55 throw sqle;
56 }
57 return this;
58 }
59 //Usage from outside
60 // AnyDBAdapter dba = new AnyDBAdapter(contextObject); //in my case contextObject is a Map
61 // dba.open();
62 // Cursor c = dba.ExampleSelect("Rawr!");
63 // contextObject.startManagingCursor(c);
64 // String s1 = "", s2 = "";
65 // if(c.moveToFirst())
66 // do {
67 // s1 = c.getString(0);
68 // s2 = c.getString(1);
69 // } while (c.moveToNext());
70 // dba.close();
71 public Cursor ExampleSelect(String myVariable)
72 {
73 String query = "SELECT locale, ? FROM android_metadata";
74 return mDb.rawQuery(query, new String[]{myVariable});
75 }
76
77 //Usage
78 // AnyDBAdatper dba = new AnyDBAdapter(contextObjecT);
79 // dba.open();
80 // dba.ExampleCommand("en-CA", "en-GB");
81 // dba.close();
82 public void ExampleCommand(String myVariable1, String myVariable2)
83 {
84 String command = "INSERT INTO android_metadata (locale) SELECT ? UNION ALL SELECT ?";
85 mDb.execSQL(command, new String[]{ myVariable1, myVariable2});
86 }
87
88 public void close() {
89 mDbHelper.close();
90 }
91
92 private static class DatabaseHelper extends SQLiteOpenHelper {
93
94 Context helperContext;
95
96 DatabaseHelper(Context context) {
97 super(context, DATABASE_NAME, null, DATABASE_VERSION);
98 helperContext = context;
99 }
100
101 @Override
102 public void onCreate(SQLiteDatabase db) {
103 }
104
105 @Override
106 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
107 Log.w(TAG, "Upgrading database!!!!!");
108 //db.execSQL("");
109 onCreate(db);
110 }
111
112 public void createDataBase() throws IOException {
113 boolean dbExist = checkDataBase();
114 if (dbExist) {
115 } else {
116
117 //make sure your database has this table already created in it
118 //this does not actually work here
119 /*
120 * db.execSQL("CREATE TABLE IF NOT EXISTS \"android_metadata\" (\"locale\" TEXT DEFAULT 'en_US')"
121 * );
122 * db.execSQL("INSERT INTO \"android_metadata\" VALUES ('en_US')"
123 * );
124 */
125 this.getReadableDatabase();
126 try {
127 copyDataBase();
128 } catch (IOException e) {
129 throw new Error("Error copying database");
130 }
131 }
132 }
133
134 public SQLiteDatabase getDatabase() {
135 String myPath = DB_PATH + DATABASE_NAME;
136 return SQLiteDatabase.openDatabase(myPath, null,
137 SQLiteDatabase.OPEN_READONLY);
138 }
139
140 private boolean checkDataBase() {
141 SQLiteDatabase checkDB = null;
142 try {
143 String myPath = DB_PATH + DATABASE_NAME;
144 checkDB = SQLiteDatabase.openDatabase(myPath, null,
145 SQLiteDatabase.OPEN_READONLY);
146 } catch (SQLiteException e) {
147 }
148 if (checkDB != null) {
149 checkDB.close();
150 }
151 return checkDB != null ? true : false;
152 }
153
154 private void copyDataBase() throws IOException {
155
156 // Open your local db as the input stream
157 InputStream myInput = helperContext.getAssets().open(DATABASE_NAME);
158
159 // Path to the just created empty db
160 String outFileName = DB_PATH + DATABASE_NAME;
161
162 // Open the empty db as the output stream
163 OutputStream myOutput = new FileOutputStream(outFileName);
164
165 // transfer bytes from the inputfile to the outputfile
166 byte[] buffer = new byte[1024];
167 int length;
168 while ((length = myInput.read(buffer)) > 0) {
169 myOutput.write(buffer, 0, length);
170 }
171
172 // Close the streams
173 myOutput.flush();
174 myOutput.close();
175 myInput.close();
176 }
177
178 public void openDataBase() throws SQLException {
179 // Open the database
180 String myPath = DB_PATH + DATABASE_NAME;
181 mDb = SQLiteDatabase.openDatabase(myPath, null,
182 SQLiteDatabase.OPEN_READWRITE);
183 }
184
185 @Override
186 public synchronized void close() {
187
188 if (mDb != null)
189 mDb.close();
190
191 super.close();
192
193 }
194 }
195
196}
2
3import java.io.FileOutputStream;
4import java.io.IOException;
5import java.io.InputStream;
6import java.io.OutputStream;
7import java.util.UUID;
8
9import android.content.Context;
10import android.database.Cursor;
11import android.database.SQLException;
12import android.database.sqlite.SQLiteDatabase;
13import android.database.sqlite.SQLiteException;
14import android.database.sqlite.SQLiteOpenHelper;
15import android.util.Log;
16
17public class AnyDBAdapter {
18
19 private static final String TAG = "AnyDBAdapter";
20 private DatabaseHelper mDbHelper;
21 private static SQLiteDatabase mDb;
22
23 //make sure this matches the
24 //package com.MyPackage;
25 //at the top of this file
26 private static String DB_PATH = "/data/data/com.edu4java.android/databases/";
27
28 //make sure this matches your database name in your assets folder
29 // my database file does not have an extension on it
30 // if yours does
31 // add the extention
32 private static final String DATABASE_NAME = "smsenglish.sqlite";
33
34 //Im using an sqlite3 database, I have no clue if this makes a difference or not
35 private static final int DATABASE_VERSION = 3;
36
37 private final Context adapterContext;
38
39 public AnyDBAdapter(Context context) {
40 this.adapterContext = context;
41 }
42
43 public AnyDBAdapter open() throws SQLException {
44 mDbHelper = new DatabaseHelper(adapterContext);
45
46 try {
47 mDbHelper.createDataBase();
48 } catch (IOException ioe) {
49 throw new Error("Unable to create database");
50 }
51
52 try {
53 mDbHelper.openDataBase();
54 } catch (SQLException sqle) {
55 throw sqle;
56 }
57 return this;
58 }
59 //Usage from outside
60 // AnyDBAdapter dba = new AnyDBAdapter(contextObject); //in my case contextObject is a Map
61 // dba.open();
62 // Cursor c = dba.ExampleSelect("Rawr!");
63 // contextObject.startManagingCursor(c);
64 // String s1 = "", s2 = "";
65 // if(c.moveToFirst())
66 // do {
67 // s1 = c.getString(0);
68 // s2 = c.getString(1);
69 // } while (c.moveToNext());
70 // dba.close();
71 public Cursor ExampleSelect(String myVariable)
72 {
73 String query = "SELECT locale, ? FROM android_metadata";
74 return mDb.rawQuery(query, new String[]{myVariable});
75 }
76
77 //Usage
78 // AnyDBAdatper dba = new AnyDBAdapter(contextObjecT);
79 // dba.open();
80 // dba.ExampleCommand("en-CA", "en-GB");
81 // dba.close();
82 public void ExampleCommand(String myVariable1, String myVariable2)
83 {
84 String command = "INSERT INTO android_metadata (locale) SELECT ? UNION ALL SELECT ?";
85 mDb.execSQL(command, new String[]{ myVariable1, myVariable2});
86 }
87
88 public void close() {
89 mDbHelper.close();
90 }
91
92 private static class DatabaseHelper extends SQLiteOpenHelper {
93
94 Context helperContext;
95
96 DatabaseHelper(Context context) {
97 super(context, DATABASE_NAME, null, DATABASE_VERSION);
98 helperContext = context;
99 }
100
101 @Override
102 public void onCreate(SQLiteDatabase db) {
103 }
104
105 @Override
106 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
107 Log.w(TAG, "Upgrading database!!!!!");
108 //db.execSQL("");
109 onCreate(db);
110 }
111
112 public void createDataBase() throws IOException {
113 boolean dbExist = checkDataBase();
114 if (dbExist) {
115 } else {
116
117 //make sure your database has this table already created in it
118 //this does not actually work here
119 /*
120 * db.execSQL("CREATE TABLE IF NOT EXISTS \"android_metadata\" (\"locale\" TEXT DEFAULT 'en_US')"
121 * );
122 * db.execSQL("INSERT INTO \"android_metadata\" VALUES ('en_US')"
123 * );
124 */
125 this.getReadableDatabase();
126 try {
127 copyDataBase();
128 } catch (IOException e) {
129 throw new Error("Error copying database");
130 }
131 }
132 }
133
134 public SQLiteDatabase getDatabase() {
135 String myPath = DB_PATH + DATABASE_NAME;
136 return SQLiteDatabase.openDatabase(myPath, null,
137 SQLiteDatabase.OPEN_READONLY);
138 }
139
140 private boolean checkDataBase() {
141 SQLiteDatabase checkDB = null;
142 try {
143 String myPath = DB_PATH + DATABASE_NAME;
144 checkDB = SQLiteDatabase.openDatabase(myPath, null,
145 SQLiteDatabase.OPEN_READONLY);
146 } catch (SQLiteException e) {
147 }
148 if (checkDB != null) {
149 checkDB.close();
150 }
151 return checkDB != null ? true : false;
152 }
153
154 private void copyDataBase() throws IOException {
155
156 // Open your local db as the input stream
157 InputStream myInput = helperContext.getAssets().open(DATABASE_NAME);
158
159 // Path to the just created empty db
160 String outFileName = DB_PATH + DATABASE_NAME;
161
162 // Open the empty db as the output stream
163 OutputStream myOutput = new FileOutputStream(outFileName);
164
165 // transfer bytes from the inputfile to the outputfile
166 byte[] buffer = new byte[1024];
167 int length;
168 while ((length = myInput.read(buffer)) > 0) {
169 myOutput.write(buffer, 0, length);
170 }
171
172 // Close the streams
173 myOutput.flush();
174 myOutput.close();
175 myInput.close();
176 }
177
178 public void openDataBase() throws SQLException {
179 // Open the database
180 String myPath = DB_PATH + DATABASE_NAME;
181 mDb = SQLiteDatabase.openDatabase(myPath, null,
182 SQLiteDatabase.OPEN_READWRITE);
183 }
184
185 @Override
186 public synchronized void close() {
187
188 if (mDb != null)
189 mDb.close();
190
191 super.close();
192
193 }
194 }
195
196}
Recommended editorial content
With your consent, external content is loaded here.
By clicking on the button above, you agree that external content may be displayed to you. Personal data may be transmitted to third-party providers in the process. You can find more information about this in our Privacy Policy.