对于保存重复或者结构化的数据数据,使用数据库很好的。在android对于数据库的API在 
android.database.sqlite包中。
创建并操作数据库:
创建:
1. 继承SQLiteOpenHelper
//继承SQLiteOpenHelper类,
public class DictionaryOpenHelper extends SQLiteOpenHelper{2. 定义相关的成员变量和常量
    public static final String DABASENAME = "dictionary";
    private static final int DATABASE_VERSION = 1;
    private SQLiteDatabase mDatabase;
    private Context mContext;
    //定义table相关的内容
    /*
     * 实现BaseColumns接口,内部类中会有一个关键字_ID。android中的一些类希望有_ID, 
     *例如cursor,但它不是必须的,但能够帮助数据库和android框架更好的协调
     */
    public static class DictionaryEntry implements BaseColumns
    {
        public static final String TAB_NAME = "words";      
        public static final String COLUMN_WORD = "word";
        public static final String COLUMN_DEFINATION = "defination";
    }
    //用于创建数据库
    private static final String SQL_CREATE_ENTRIES = "CREATE TABLE " 
            +DictionaryEntry.TAB_NAME+"("+DictionaryEntry._ID 
            +" INTEGER PRIMARY KEY,"+DictionaryEntry.COLUMN_WORD+"," 
            +DictionaryEntry.COLUMN_DEFINATION + ")";
    public DictionaryOpenHelper(Context context) {
        super(context, DABASENAME, null, DATABASE_VERSION);     
    }3. 实现SQLiteOpenHelper的相关的函数
    //当第一创建数据库时会被调用
    @Override
    public void onCreate(SQLiteDatabase db) {
        // 
        db.execSQL(SQL_CREATE_ENTRIES); 
        //读取文件并添加多行
        loadDictionary();
    }
    private void loadDictionary()
    {
        new Thread(new Runnable() {
            @Override
            public void run() {
                // TODO Auto-generated method stub
                loadWords();
            }
        });
    }
    private void loadWords()
    {
        //读取资源
        final Resources resources = mContext.getResources();
        InputStream inputStream = resources.openRawResource(R.raw.definitions);
        Scanner scanner = new Scanner(inputStream);
        while(scanner.hasNextLine())
        {
            String string = scanner.nextLine();
            String[] strings = string.split("-");
            if (strings.length < 2) {
                continue;
            }
            loadWord(strings[0].trim(), strings[1].trim());
        }
    }
    //添加行
    private void loadWord(String word,String defination)
    {
        ContentValues contentValues = new ContentValues();
        contentValues.put(DictionaryEntry.COLUMN_WORD, 
                        DictionaryEntry.COLUMN_DEFINATION);
        mDatabase.insert(DictionaryEntry.TAB_NAME, null, contentValues);
    }
    //当数据库升级的时候会被调用
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
    {
        // TODO Auto-generated method stub
        db.execSQL(SQL_CREATE_ENTRIES);
        onCreate(db);
    }
注意:
- 数据存储在内部存储中,数据是安全的,因为其他的应用程序不能够访问数据库
- 在调用getWritableDatabase() or getReadableDatabase()来操作长时间的操作,
要使用后台线程,例如AsyncTask或者IntentService
操作
    //1. 创建ContentValues对象并添加信息
    ContentValues contentValues = new ContentValues();
    contentValues.put(DictionaryEntry.COLUMN_WORD,     
                    DictionaryEntry.COLUMN_DEFINATION);
    //2. 添加到数据库中
    mDatabase.insert(DictionaryEntry.TAB_NAME, null, contentValues);从数据库中读取数据
使用query()方法来检索数据,并返回一个Cursor对象
   mDatabase.query(
   table,               //table名
   columns,             //返回的关键字
   selection,           //where语句的关键字
   selectionArgs,       //where的值
   groupBy,             //用于声明怎么去组织行
   having,              //用于声明那个行组应该包含到Cursor中
   orderBy);           //怎么哪个关键字排序从数据库中删除数据
从数据库表中删除数据,需要指定selection,用于指定要删除哪一行,
    mDatabase.delete(table,         //table名
                    whereClause,    //用于操作的WHERE子句 
                    whereArgs);     //WHERE子句的参数从数据库中更新数据
当需要修改数据库表中的数据时,使用update()方法
    mDatabase.update(table,         //要更新的table名
                     values,        //ContentValues的对象,用于指定更新的数据
                     whereClause,   //where子句,用于指定哪一个行(记录)需要修改
                     whereArgs);    //where子句的值例子并未添加所有操作。
数据库的调试: 
        Android SDK中包含一个工具是sqlite3,这个工具能够查看数据库表中内容,使用这个工具只需简单的命令行 
       ~$ adb devices
       List of devices attached 
       emulator-5554 device
     ~$ adb -s emulator-5554 shell           
       #sqlite3 /data/data/com.example.mydictionary/databases/dictionary
       sqlite> .table
以后就能根据数据相关的命令查看数据库表中的数据了,
参考资料:
http://developer.android.com/training/basics/data-storage/databases.htmlandroid数据存储_SQL数据库,布布扣,bubuko.com
原文地址:http://blog.csdn.net/wangfei199101/article/details/24851265