2014-10-30 96 views
0

好吧,所以這裏是我的困境。我開始在Android中使用SQLite database's,我在Android Developer網站上遇到Notepad Tutorial。現在我認爲這很酷,並且一直在搞亂它,直到我注意到Android Studio說某些代碼是deprecated什麼應該用於SQLite數據庫

其中一個不推薦使用的代碼是startManagingCursor。我決定進一步研究它,發現一個名爲CursorLoaderContentProvider,所以我決定查找它們,似乎CursorLoaderContentProvider是一個更好的選擇。問題是,我找不到一個體面的例子,不會出現過分的複雜性。就像我剛纔說的,我剛開始學習這個,我不想坐下來嘗試弄清楚別人的代碼在3個小時內做了些什麼。

那麼有沒有什麼好的tutorialsexamples不會讓這個話題複雜化,讓我有機會學習它呢?

+0

不,如果你想要一個數據庫,你仍然需要SQLiteDatabase。不要在這個階段做ContentProvider和Loader,做數據庫,按照記事本教程,第一件事。 – 2014-10-30 17:39:43

回答

-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

+0

鏈接唯一的答案,格式不好,沒有解釋。這種「答案」在這裏不受歡迎。請投入更多的時間,並解釋... – WarrenFaith 2014-10-30 17:45:27

+0

我喜歡vogella鏈接 – danny117 2014-10-30 17:45:47

+0

對不起傢伙:);我編輯了我的答案;) – 2014-10-30 18:09:42

相關問題