2015-04-12 153 views
0

我已經創建了存儲在SD卡中的數據庫。但似乎發生了一些錯誤。 這裏是代碼的CreateDatabase存儲在SD卡中的Android數據

public void createDatabase(){ 
    String path = Environment.getExternalStorageDirectory().getAbsolutePath()+ "/smsgarbage.db"; 
    sqlite = openOrCreateDatabase(path, MODE_PRIVATE, null); 
    try { 
     String query = "CREATE TABLE tblGarbage(id text PRIMARY KEY, prefixNum text)"; 
     sqlite.execSQL(query); 
     Toast.makeText(MainActivity.this, "Oncreate dabase was created", Toast.LENGTH_LONG).show(); 
    } catch (SQLException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
} 

清單文件,我已經填寫:

<uses-permission android:name="android.permission.RECEIVE_SMS"/> 
<uses-permission android:name="android.permission.WRITE_SMS"/> 
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 

人誰是主人對這個問題,請花時間重新檢查出來支持我。我很欣賞這一點。

+2

'有些事情是錯誤發生了......發佈錯誤日誌。那麼只有問題可以找到。 –

+0

發生了什麼問題? –

回答

0

嗨,如果你想通過SD卡創建數據庫正確的方式來實現看下面的方式讓我知道如果你有任何疑問,如果你沒有得到我在任何點。

第1步。擴展SQLiteOpenHelper用完整的SD卡路徑替換數據庫名稱。這將在定義位置創建數據庫。

import android.content.Context; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteDatabase.CursorFactory; 
import android.database.sqlite.SQLiteOpenHelper; 


import com.adb.test.sql.DAO.FoodMarchantDAO; 


public class SQLite extends SQLiteOpenHelper { 

    public static int DATABASE_VERSION = 1; 

    public static String DATABASE_NAME = "smsgarbage.db"; 

    private static final String DATABASE_PATH_NAME = getDBAbsolutePath(); 

    /** 
    * Create a helper object to create, open, and/or manage a database. This method always returns very quickly. 
    * The database is not actually created or opened until one of getWritableDatabase() 
    * or getReadableDatabase() is called. 
    * @param context 
    * @param name 
    * @param factory 
    * @param version 
    */ 
    public SQLite(Context context) 
    { 
     super(context, DATABASE_PATH_NAME, null, version); 
    } 


    @Override 
    public void onCreate(SQLiteDatabase db) { 
     // when we call SQLiteDatabase getWritableDatabase() this would called 
     // In fist time application install if no database then it would called once but that time Application 
     // database might be null we have to used here db 
    } 


    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
    } 

    @Override 
    public void onOpen(SQLiteDatabase db) { 
     super.onOpen(db); 
    } 

    private static String getDBAbsolutePath() { 
     return new StringBuilder().append(appHomeDirectory()).append(DATABASE_NAME).toString(); 
    } 

    public static String appHomeDirectory() { 
     return new StringBuilder().append(getExternalStoragePath()).append("foodbazer").append("/").toString(); 
    } 
} 

第2步。在應用程序級別如何訪問SQLite幫助類,請參閱下面的內容。

import com.adb.test.sql.data.SQLite; 
import android.app.Application; 
import android.database.sqlite.SQLiteDatabase; 


public class FoodBazerApplication extends Application{ 

    private static FoodBazerApplication mFoodBazerApplication; 

    public SQLiteDatabase mAppLevelDB; 

    public static FoodBazerApplication getInstance() 
    { 
     return mFoodBazerApplication; 
    } 

    @Override 
    public void onCreate() { 
     super.onCreate(); 
     mFoodBazerApplication = this; 
     SQLite tmpSQL= new SQLite(getApplicationContext()); 
     // it return null if first time database create onCreate() called, 
     // once database create it will open the database so openDataBase() called 
     mAppLevelDB = tmpSQL.getWritableDatabase(); 
    } 
}