现在的位置: 首页 > 综合 > 正文

数据库保存到SDcard

2013年10月04日 ⁄ 综合 ⁄ 共 2970字 ⁄ 字号 评论关闭

 

我们知道数据库创建的时候默认保存到系统data/data/项目名
下面了,有没有一种方法保存到自己指定的SDCard上的文件夹里面呢?答案是可以的。我们只需要找到SQLiteOpenHelper这个类,了解一下
就不难发现其保存的路径是固定了的,那么我们只需要改动一下getWritableDatabase(),即写入的时候的路径:

01 public
synchronized
SQLiteDatabase getWritableDatabase() {
02 if
(mDatabase !=
null
&& mDatabase.isOpen() && !mDatabase.isReadOnly()) {
03 return
mDatabase;  
// The database is already open for business
04 }
05  
06 if
(mIsInitializing) {
07 throw
new
IllegalStateException(
"getWritableDatabase called recursively"
);
08 }
09  
10 // If we have a read-only database open, someone could be using it
11 // (though they shouldn't), which would cause a lock to be held on
12 // the file, and our attempts to open the database read-write would
13 // fail waiting for the file lock.  To prevent that, we acquire the
14 // lock on the read-only database, which shuts out other users.
15  
16 boolean
success =
false
;
17 SQLiteDatabase db =
null
;
18 if
(mDatabase !=
null
) mDatabase.lock();
19 try
{
20 mIsInitializing =
true
;
21 if
(mName ==
null
) {
22 db = SQLiteDatabase.create(
null
);
23 }
else
{
24 <span style=
"color: red;"
> db = mContext.openOrCreateDatabase(mName,
0
, mFactory);</span>
25 }
26  
27 int
version = db.getVersion();
28 if
(version != mNewVersion) {
29 db.beginTransaction();
30 try
{
31 if
(version ==
0
) {
32 onCreate(db);
33 }
else
{
34 onUpgrade(db, version, mNewVersion);
35 }
36 db.setVersion(mNewVersion);
37 db.setTransactionSuccessful();
38 }
finally
{
39 db.endTransaction();
40 }
41 }
42  
43 onOpen(db);
44 success =
true
;
45 return
db;
46 }
finally
{
47 mIsInitializing =
false
;
48 if
(success) {
49 if
(mDatabase !=
null
) {
50 try
{ mDatabase.close(); }
catch
(Exception e) { }
51 mDatabase.unlock();
52 }
53 mDatabase = db;
54 }
else
{
55 if
(mDatabase !=
null
) mDatabase.unlock();
56 if
(db !=
null
) db.close();
57 }
58 }
59 }

注意标志的红色的部分,这是创建数据库的地方,我们再看看openOrCreateDatabase()

这个方法,openOrCreateDatabase()
这个方法存在于package android.content;下面的public abstract class Context{}类里面,

1 public
abstract
SQLiteDatabase openOrCreateDatabase(String name,
int
mode, CursorFactory factory);

我们发现在package android.database.sqlite;包下的public class SQLiteDatabase extends SQLiteClosable {}里面也有一个

1 <span style=
"color: red;"
>
public
static
SQLiteDatabase openOrCreateDatabase(String path, CursorFactory factory) {</span>
2 <span style=
"color: red;"
>
return
openDatabase(path, factory, CREATE_IF_NECESSARY);</span>
3 <span style=
"color: red;"
> }</span>

相同的方法,只是参数不同,这个可以传一个路径进去,我们就调用这个方法。然后我们还要有一个我们指定文件夹的路径的方法:

01 public
File getDatabasePath(String name) {
02 String EXTERN_PATH =
null
;
03 //判断是否有SDcard
04 if
(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)==
true
)
05 {
06 //判断是否存在指定的文件夹,如果没有就创建它
07 EXTERN_PATH = android.os.Environment.getExternalStorageDirectory().getAbsolutePath()
08 +
"/database/"
;
09 File f=
new
File(EXTERN_PATH);
10 if
(!f.exists())
11 {
12 f.mkdirs();
13 }
14 }
15 return
new
File(EXTERN_PATH+ name);
16 }

及对SDcard进行写和读的权限:

1 <!-- 在SDCard中创建于删除文件权限 -->
2 <uses-permission android:name=
"android.permission.MOUNT_UNMOUNT_FILESYSTEMS"
/>
3 <!-- 往SDCard写入数据权限 -->
4 <uses-permission android:name=
"android.permission.WRITE_EXTERNAL_STORAGE"
/>

最后当我们创建数据库的时候直接继承我们自己定义的SDSQLiteOpenHelper(),不用系统自带SQLiteOpenHelper()即可。

 

 

转帖:http://bg135.com/save-to-sdcard-database.html

抱歉!评论已关闭.