There are many ways to store your data, such as in an online database, in a local SQLite database, or even in a text file. It is up to you to decide what is the best solution for your application. This lesson shows you how to create a SQLite virtual table
that can provide robust full-text searching. The table is populated with data from a text file that contains a word and definition pair on each line in the file.http://blog.csdn.net/sergeycao
Create the Virtual Table
A virtual table behaves similarly to a SQLite table, but reads and writes to an object in memory via callbacks, instead of to a database file. To create a virtual table, create a class for the table:
public class DatabaseTable {
private final DatabaseOpenHelper mDatabaseOpenHelper;
public DatabaseTable(Context context) {
mDatabaseOpenHelper = new DatabaseOpenHelper(context);
}
}
Create an inner class in DatabaseTable
that extends SQLiteOpenHelper
. The
SQLiteOpenHelper
class defines abstract methods that you must override so that your database table can be created and upgraded when necessary. For example, here is some code that declares a database table that will contain words
for a dictionary app:
public class DatabaseTable {
private static final String TAG = "DictionaryDatabase";
//The columns we'll include in the dictionary table
public static final String COL_WORD = "WORD";
public static final String COL_DEFINITION = "DEFINITION";
private static final String DATABASE_NAME = "DICTIONARY";
private static final String FTS_VIRTUAL_TABLE = "FTS";
private static final int DATABASE_VERSION = 1;
private final DatabaseOpenHelper mDatabaseOpenHelper;
public DatabaseTable(Context context) {
mDatabaseOpenHelper = new DatabaseOpenHelper(context);
}
private static class DatabaseOpenHelper extends SQLiteOpenHelper {
private final Context mHelperContext;
private SQLiteDatabase mDatabase;
private static final String FTS_TABLE_CREATE =
"CREATE VIRTUAL TABLE " + FTS_VIRTUAL_TABLE +
" USING fts3 (" +
COL_WORD + ", " +
COL_DEFINITION + ")";
DatabaseOpenHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
mHelperContext = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
mDatabase = db;
mDatabase.execSQL(FTS_TABLE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS " + FTS_VIRTUAL_TABLE);
onCreate(db);
}
}
}
Populate the Virtual Table
The table now needs data to store. The following code shows you how to read a text file (located in
res/raw/definitions.txt
) that contains words and their definitions, how to parse that file, and how to insert each line of that file as a row in the virtual table. This is all done in another thread to prevent the UI from locking. Add the following
code to your DatabaseOpenHelper
inner class.
Tip: You also might want to set up a callback to notify your UI activity of this thread's completion.
private void loadDictionary() {
new Thread(new Runnable() {
public void run() {
try {
loadWords();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}).start();
}
private void loadWords() throws IOException {
final Resources resources = mHelperContext.getResources();
InputStream inputStream = resources.openRawResource(R.raw.definitions);
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
try {
String line;
while ((line = reader.readLine()) != null) {
String[] strings = TextUtils.split(line, "-");
if (strings.length < 2) continue;
long id = addWord(strings[0].trim(), strings[1].trim());
if (id < 0) {
Log.e(TAG, "unable to add word: " + strings[0].trim());
}
}
} finally {
reader.close();
}
}
public long addWord(String word, String definition) {
ContentValues initialValues = new ContentValues();
initialValues.put(COL_WORD, word);
initialValues.put(COL_DEFINITION, definition);
return mDatabase.insert(FTS_VIRTUAL_TABLE, null, initialValues);
}
Call the loadDictionary()
method wherever appropriate to populate the table. A good place would be in the
onCreate()
method of the DatabaseOpenHelper
class, right after you create the table:
@Override
public void onCreate(SQLiteDatabase db) {
mDatabase = db;
mDatabase.execSQL(FTS_TABLE_CREATE);
loadDictionary();
}
Search for the Query
When you have the virtual table created and populated, use the query supplied by your
SearchView
to search the data. Add the following methods to the
DatabaseTable
class to build a SQL statement that searches for the query:
public Cursor getWordMatches(String query, String[] columns) {
String selection = COL_WORD + " MATCH ?";
String[] selectionArgs = new String[] {query+"*"};
return query(selection, selectionArgs, columns);
}
private Cursor query(String selection, String[] selectionArgs, String[] columns) {
SQLiteQueryBuilder builder = new SQLiteQueryBuilder();
builder.setTables(FTS_VIRTUAL_TABLE);
Cursor cursor = builder.query(mDatabaseOpenHelper.getReadableDatabase(),
columns, selection, selectionArgs, null, null, null);
if (cursor == null) {
return null;
} else if (!cursor.moveToFirst()) {
cursor.close();
return null;
}
return cursor;
}
Search for a query by calling getWordMatches()
. Any matching results are returned in a
Cursor
that you can iterate through or use to build a
ListView
. This example calls getWordMatches()
in the
handleIntent()
method of the searchable activity. Remember that the searchable activity receives the query inside of the
ACTION_SEARCH
intent as an extra, because of the intent filter that you previously created:
DatabaseTable db = new DatabaseTable(this);
...
private void handleIntent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
Cursor c = db.getWordMatches(query, null);
//process Cursor and display results
}
}
分享到:
相关推荐
Network Storage Tools and Technologies for Storing Your Company's Data
Network Storage Tools and Technologies for Storing Your Company's Data 英文无水印pdf pdf所有页面使用FoxitReader和PDF-XChangeViewer测试都可以打开 本资源转载自网络,如有侵权,请联系上传者或csdn删除 ...
Digital data is heavily used when generating, storing, and transmitting information, and special codes are used to represent the data and to control its size, reliability, and security. Data coding is...
History and Overview of R, Getting Started with R, R Nuts and Bolts, Getting Data In and Out of R, Using Textual and Binary Formats for Storing Data, Interfaces to the Outside World ...
used for storing and managing Big Data. Analyzing Big Data is a challenging task as it involves large distributed file systems which should be fault tolerant, flexible and scalable. Map Reduce is ...
Chapter 21: Storing, Managing, and Analyzing Big Satellite Data : Experiences and Lessons Learned from a Real-World Application Chapter 22: Barriers to the Adoption of Big Data Applications in the ...
Chapter 3, Storing Efficiently, looks at how in Rust, knowing where values are stored is not only important for performance, but also important for understanding error messages and the language in ...
collecting, storing, processing, and analyzing big data. The main focus of the paper is on unstructured data analysis. Unstructured data refers to information that either does not have a pre-defined ...
storing, and searching data, as well as using modern tools and Pythonic libraries to create data APIs and cloud services. You may also be using Docker and Amazon Web Services to package and deploy a ...
Searching the Big Data: Practices and Experiences in Efficiently Querying Knowledge Bases 1 Introduction 2 Background 3 The Framework of Cache-Based Knowledge Base Querying 4 Similar Queries ...
Organizations invest incredible amounts of time and money obtaining and then storing big data in data stores called data lakes. But how many of these organizations can actually get the data back out ...
Swift library for making storing data in a SQLite database easy as pie.zip,Swift library for making storing data in a SQLite database simple and magic-free
The R language is widely used by statisticians for data analysis, and the popularity of R programming has therefore increased substantially in recent years. The emerging Internet of Things (IoT) ...
2: STORING DATA IN LISTS AND LINKED LISTS 3: CONSTRUCTING STACKS AND QUEUES 4: ARRANGING DATA ELEMENTS USING A SORTING ALGORITHM 5: FINDING OUT AN ELEMENT USING SEARCHING ALGORITHMS 6: DEALING WITH ...
At the heart of this book lies the coverage of pandas, an open source, BSD-licensed library providing high-performance, easy-to-use data structures and data analysis tools for the Python programming ...
More and more companies are amassing larger and larger amounts of data, and storing them in bigger and bigger data bases. Consequently, successful applications of data-driven decision making are ...
You'll see a unified architecture and embedded workflow to automate various analytics steps such as data preprocessing, model creation, and storing final model output to tables. You'll take a deep ...
In addition to explaining the fundamentals of data warehouse systems, the book covers new topics such as methods for handling unstructured data in a data warehouse and storing data across multiple ...
Ch 10: Using a Relational Database Server for Sharing and Storing Information Ch 11: JavaScript Templating Ch 12: Maps, Location, and Multimedia Databases Ch 13: Cross-Platform and Native App ...
data transformation is where the data is transformed for storing in the proper format or structure for the purposes of querying and analysis; data loading where the data is loaded into the final ...