Android 數據庫打包隨APK發佈

有些時候我們的軟件用到SQLite數據庫,這個時候怎麼把一個做好的數據庫打包進我們的APK呢,其實很簡單,就是把我們的數據庫文件放到我們的手機裏,所以不必侷限在哪個地方寫這個代碼,在第一次創建數據庫的時候可以,我覺得在軟件起動頁裏效果更好一點,首先我們應該把事先寫好的數據庫文件比如test.db放到res文件夾裏的raw文件夾裏,也可以放到assets裏,因爲這兩個文件夾不會在生成APK的時候不會被壓縮 
Android專業開發羣1:150086842
Android專業開發羣2:219277004
標籤: SQLite

代碼片段(1)[全屏查看所有代碼]

1. [代碼]Java代碼     跳至 [1] [全屏預覽]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
 
import com.android.qufu.dinner.MealActivityGroup;
import com.android.qufu.dinner.R;
 
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;
 
public class Loggin extends Activity {
    public static String dbName="dinner.db";//數據庫的名字
    private static String DATABASE_PATH="/data/data/com.android.qufu.dinner/databases/";//數據庫在手機裏的路徑
     int alpha = 255;
     int b = 0;
     
     public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.login);
            //判斷數據庫是否存在
            boolean dbExist = checkDataBase();
            if(dbExist){
                 
            }else{//不存在就把raw裏的數據庫寫入手機
                try{
                    copyDataBase();
                }catch(IOException e){
                    throw new Error("Error copying database");
                }
            }
             
            new Thread(new Runnable() {
                public void run() {
                    initApp(); //初始化程序
                      
                    while (b < 2) {
                        try {
                            if (b == 0) {
                                Thread.sleep(20);
                                b = 1;
                            } else {
                                Thread.sleep(50);
                            }
                            updateApp();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }).start();
 
            
        }
 
        public void updateApp() {
            alpha -= 5;
            if (alpha <= 0) {
                b = 2;
                if(true){
                try{
                   Intent in = new Intent(Loggin.this,MealActivityGroup.class);
                   Loggin.this.startActivity(in);
                   Loggin.this.finish();
                }catch(Exception e){
                         
                    }
                }
                
            }
            
        }
        /**
         * 判斷數據庫是否存在
         * @return false or true
         */
        public boolean checkDataBase(){
            SQLiteDatabase checkDB = null;
            try{
                String databaseFilename = DATABASE_PATH+dbName;
                checkDB =SQLiteDatabase.openDatabase(databaseFilename, null,
                        SQLiteDatabase.OPEN_READONLY);
            }catch(SQLiteException e){
                 
            }
            if(checkDB!=null){
                checkDB.close();
            }
            return checkDB !=null?true:false;
        }
        /**
         * 複製數據庫到手機指定文件夾下
         * @throws IOException
         */
        public void copyDataBase() throws IOException{
            String databaseFilenames =DATABASE_PATH+dbName;
            File dir = new File(DATABASE_PATH);
            if(!dir.exists())//判斷文件夾是否存在,不存在就新建一個
                dir.mkdir();
            FileOutputStream os = null;
            try{
                os = new FileOutputStream(databaseFilenames);//得到數據庫文件的寫入流
            }catch(FileNotFoundException e){
                e.printStackTrace();
            }
            InputStream is = Loggin.this.getResources().openRawResource(R.raw.test);//得到數據庫文件的數據流
            byte[] buffer = new byte[8192];
            int count = 0;
            try{
                 
                while((count=is.read(buffer))>0){
                    os.write(buffer, 0, count);
                    os.flush();
                }
            }catch(IOException e){
                 
            }
            try{
                is.close();
                os.close();
            }catch(IOException e){
                e.printStackTrace();
            }
        }
        /**
         * 初始化,這裏是起始頁的沒有用
         */
        public void initApp(){         
        }
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章