我应该为SQLite数据库使用什么

时间:2014-10-30 17:36:57

标签: android sqlite

好的,这就是我的困境。我开始在android中使用SQLite database's,我在Android Developer网站上遇到了Notepad Tutorial。现在我觉得这很酷,并且一直在搞乱它直到我注意到Android Studio说某些代码是deprecated

其中一个弃用的代码是startManagingCursor。我决定更多地研究一下,找到一个名为CursorLoader并带有ContentProvider的东西,所以我决定查找它们,似乎CursorLoaderContentProvidertutorials一个更好的选择。问题在于,我找不到一个不会过分复杂的不错的例子。就像我说的那样,我刚刚开始学习这个,我不想坐下来试着找出别人的代码做了3个小时。

那么,是否有任何好的examples或{{1}}不会使这个主题复杂化,以便我有机会学习它?

1 个答案:

答案 0 :(得分:-1)

示例代码

package com.prgguru.example;
 
import java.util.ArrayList;
 
import android.app.ListActivity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Toast;
 
public class SQLiteCrudExample  extends ListActivity {
    //DB name
    private final String dbName = "Android";
    //Table name
    private final String tableName = "Versions";
    //String array has list Android versions which will be populated in the list
    private final String[] versionNames= new String[]{"Cupcake", "Donut", "Eclair", "Froyo", "Gingerbread", "Honeycomb", "Ice Cream Sandwich", "Jelly Bean", "Kitkat"};
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ArrayList<String> results = new ArrayList<String>();
        //Declare SQLiteDatabase object
        SQLiteDatabase sampleDB = null;
         
        try {
            //Instantiate sampleDB object
            sampleDB =  this.openOrCreateDatabase(dbName, MODE_PRIVATE, null);
            //Create table using execSQL
            sampleDB.execSQL("CREATE TABLE IF NOT EXISTS " + tableName +    " (versionname VARCHAR);");
            //Insert Android versions into table created
            for(String ver: versionNames){
                sampleDB.execSQL("INSERT INTO " + tableName + " Values ('"+ver+"');");
            }
             
            //Create Cursor object to read versions from the table
            Cursor c = sampleDB.rawQuery("SELECT versionname FROM " + tableName, null);
            //If Cursor is valid
            if (c != null ) {
                //Move cursor to first row
                if  (c.moveToFirst()) {
                    do {
                        //Get version from Cursor
                        String firstName = c.getString(c.getColumnIndex("versionname"));
                        //Add the version to Arraylist 'results'
                        results.add(firstName);
                    }while (c.moveToNext()); //Move to next row
                } 
            }
             
            //Set the ararylist to Android UI List
            this.setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,results));
             
        } catch (SQLiteException se ) {
            Toast.makeText(getApplicationContext(), "Couldn't create or open the database", Toast.LENGTH_LONG).show();
        } finally {
            if (sampleDB != null) {
                sampleDB.execSQL("DELETE FROM " + tableName);
                sampleDB.close();
            }
        }
    }
}

===

有一个示例源代码here可供下载

也可以试试这个链接:

http://www.androidhive.info/2013/09/android-sqlite-database-with-multiple-tables/ http://developer.android.com/training/basics/data-storage/databases.html http://www.vogella.com/tutorials/AndroidSQLite/article.html http://www.tutorialspoint.com/android/android_sqlite_database.htm