2011-05-10 13 views
4

如何使用Asset文件夾中的SQLite連接。如何使用Asset文件夾中的SQLite連接

+0

你能指出一下你想做什麼嗎? – Hein 2011-05-10 04:36:26

+0

我已經使用從資產文件夾在Android中的SQLite連接如何使用它你能告訴我這個? – 2011-05-10 04:40:01

+0

檢查此[網站](http://www.anotherandroidblog.com/2010/08/04/android-database-tutorial/) – Hein 2011-05-10 05:14:37

回答

6

這就是所謂的DataBaseHelper.java文件

public class DataBaseHelper extends SQLiteOpenHelper 
{ 
private static String TAG = "TAG"; 
private static String DB_PATH = "/data/data/YOUR_PACKAGE/databases/"; 
private static String DB_NAME = "ServiceInfo.sqlite"; 
private SQLiteDatabase mDataBase; 
private final Context mContext; 

public DataBaseHelper(Context context) 
{ 
    super(context, DB_NAME, null, 1); 
    DB_PATH = "/data/data/" + context.getPackageName() + "/databases/"; 
    this.mContext = context; 
} 

public void createDataBase() throws IOException 
{ 
    boolean mDataBaseExist = checkDataBase(); 
    if(!mDataBaseExist) 
    { 
     this.getReadableDatabase(); 
     try 
     { 
      copyDataBase(); 
     } 
     catch (IOException mIOException) 
     { 
      throw new Error("ErrorCopyingDataBase"); 
     } 
    } 
} 

private boolean checkDataBase() 
{ 
    SQLiteDatabase mCheckDataBase = null; 
    try 
    { 
     String myPath = DB_PATH + DB_NAME; 
     mCheckDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.NO_LOCALIZED_COLLATORS); 
    } 
    catch(SQLiteException mSQLiteException) 
    { 
     Log.e(TAG, "DatabaseNotFound " + mSQLiteException.toString()); 
    } 

    if(mCheckDataBase != null) 
    { 
     mCheckDataBase.close(); 
    } 
    return mCheckDataBase != null; 
} 

private void copyDataBase() throws IOException 
{ 
    InputStream mInput = mContext.getAssets().open(DB_NAME); 
    String outFileName = DB_PATH + DB_NAME; 
    OutputStream mOutput = new FileOutputStream(outFileName); 
    byte[] mBuffer = new byte[1024]; 
    int mLength; 
    while ((mLength = mInput.read(mBuffer))>0) 
    { 
     mOutput.write(mBuffer, 0, mLength); 
    } 
    mOutput.flush(); 
    mOutput.close(); 
    mInput.close(); 
} 

public boolean openDataBase() throws SQLException 
{ 
    String mPath = DB_PATH + DB_NAME; 
    mDataBase = SQLiteDatabase.openDatabase(mPath, null, SQLiteDatabase.NO_LOCALIZED_COLLATORS); 
    return mDataBase != null; 
} 

@Override 
public synchronized void close() 
{ 
    if(mDataBase != null) 
     mDataBase.close(); 
    super.close(); 
} 

@Override 
public void onCreate(SQLiteDatabase db) 
{ } 

@Override 
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
{ 
    Log.v(TAG, "UpgradingDatabase, This will drop current database and will recreate it"); 
} 
} 

現在AdapetrClass.java

public class KamaDBAdapter 
{ 
protected static final String TAG = "TAG"; 

private final Context mContext; 
private SQLiteDatabase mDb; 
private DataBaseHelper mDbHelper; 

private static String ACCOUNT_TABLE = "account"; 
public static String ACCOUNT_EXTRADATA = "extraData"; 
public static String ACCOUNT_ID = "ID"; 
public static String ACCOUNT_ADDITIONALDATA = "additionalData"; 
public static String ACCOUNT_DATA = "data"; 

public KamaDBAdapter(Context context) 
{ 
    this.mContext = context; 
    mDbHelper = new DataBaseHelper(mContext); 
} 

public KamaDBAdapter createDatabase() throws SQLException 
{ 
    try 
    { 
     mDbHelper.createDataBase(); 
    } 
    catch (IOException mIOException) 
    { 
     Log.e(TAG, mIOException.toString() + " UnableToCreateDatabase"); 
     throw new Error("UnableToCreateDatabase"); 
    } 
    return this; 
} 

public KamaDBAdapter open() throws SQLException 
{ 
    try 
    { 
     mDbHelper.openDataBase(); 
     mDbHelper.close(); 
     mDb = mDbHelper.getReadableDatabase(); 
    } 
    catch (SQLException mSQLException) 
    { 
     Log.e(TAG, mSQLException.toString()); 
     throw mSQLException; 
    } 
    return this; 
} 

public void close() 
{ 
    mDbHelper.close(); 
} 

public int countAccountData() 
{ 
    Cursor mCoursor = mDb.query(ACCOUNT_TABLE, new String[] {}, null, null, null, null, null); 
    int mReturnedCount = mCoursor.getCount(); 
    mCoursor.close(); 
    return mReturnedCount; 
} 

public long insertData(String mExtra, String mAdditionalData, String mData) 
{ 
    ContentValues initialValues = new ContentValues(); 
    initialValues.put(ACCOUNT_EXTRADATA, mExtra); 
    initialValues.put(ACCOUNT_ADDITIONALDATA, mAdditionalData); 
    initialValues.put(ACCOUNT_DATA, mData); 
    return mDb.insert(ACCOUNT_TABLE, null, initialValues); 
} 

public boolean updateData(int mPosition, String mExtra, String mAdditionalData, String mData) 
{ 
    ContentValues initialValues = new ContentValues(); 
    initialValues.put(ACCOUNT_EXTRADATA, mExtra); 
    initialValues.put(ACCOUNT_ADDITIONALDATA, mAdditionalData); 
    initialValues.put(ACCOUNT_DATA, mData); 
    return mDb.update(ACCOUNT_TABLE, initialValues, "ID=" + mPosition, null) > 0; 
} 

public String retriveData(int mPosition) 
{ 
    Cursor mCursor = mDb.query(ACCOUNT_TABLE, new String[] {ACCOUNT_DATA}, "ID=" + mPosition, null, null, null, null); 
    mCursor.moveToFirst(); 
    String mReturn = mCursor.getString(mCursor.getColumnIndex(ACCOUNT_DATA)); 
    mCursor.close(); 
    return mReturn; 
} 

public String retriveAdditionalData(int mPosition) 
{ 
    Cursor mCursor = mDb.query(ACCOUNT_TABLE, new String[] {ACCOUNT_ADDITIONALDATA}, "ID=" + mPosition, null, null, null, null); 
    mCursor.moveToFirst(); 
    String mReturn = mCursor.getString(mCursor.getColumnIndex(ACCOUNT_ADDITIONALDATA)); 
    mCursor.close(); 
    return mReturn; 
} 

public boolean deleteAccount(int mPosition) 
{ 
    return mDb.delete(ACCOUNT_TABLE, ACCOUNT_ID + "=" + mPosition, null) > 0; 
} 
} 

現在,在你的主類調用這個類:事情是這樣的:

private static KamaDBAdapter mDbHelper; 
mDbHelper = new KamaDBAdapter(Usage.this); 
mDbHelper.createDatabase(); 

現在你的數據庫複製到設備。您可以從本地設備訪問您放置在Asset文件夾中的相同數據庫。

希望它能幫助你。

+0

謝謝。這是我正在尋找的。真正接受。 – 2011-05-10 13:42:04

+0

不客氣。 – 2011-05-10 13:42:34

相關問題