一、使用raw文件夹下的数据库【自己导入的】
1.新建raw文件夹
res鼠标右键->new->Android Resource Directory->下拉选择raw.
然后写读写代码:
1 package com.example.wxy.kcsj2; 2 3 import android.content.Context; 4 import android.database.sqlite.SQLiteDatabase; 5 import android.os.Environment; 6 import android.util.Log; 7 8 import java.io.File; 9 import java.io.FileNotFoundException; 10 import java.io.FileOutputStream; 11 import java.io.IOException; 12 import java.io.InputStream; 13 14 public class DataBaseHelper { 15 private final int BUFFER_SIZE = 400000; 16 public static final String DB_NAME = "student.db3"; // 保存的数据库文件名 17 public static final String PACKAGE_NAME = "com.example.wxy.kcsj2";// 应用的包名 18 public static final String DB_PATH = "/data" + Environment.getDataDirectory().getAbsolutePath() + "/" 19 + PACKAGE_NAME + "/databases/"; // 在手机里存放数据库的位置 20 //sdcard的路径(在android 4.4中不好使,文件成功创建是在手机的) 21 //public static final String DB_PATH = Environment.getExternalStorageDirectory().getAbsolutePath()+"/student"; 22 private SQLiteDatabase database; 23 private Context context; 24 25 public SQLiteDatabase getDatabase() { 26 return database; 27 } 28 29 public void setDatabase(SQLiteDatabase database) { 30 this.database = database; 31 } 32 33 public DataBaseHelper(Context context) { 34 this.context = context; 35 } 36 37 public void openDatabase() { 38 System.out.println(DB_PATH + "/" + DB_NAME); 39 System.out.println("打开数据库"); 40 this.database = this.openDatabase(DB_PATH + "/" + DB_NAME); 41 } 42 43 public SQLiteDatabase openDatabase(String dbfile) { 44 try { 45 File myDataPath = new File(DB_PATH); 46 if (!myDataPath.exists()) { 47 myDataPath.mkdirs();// 如果没有这个目录,则创建 48 System.out.println("目录,创建"); 49 } 50 dbfile = myDataPath + "/" + DB_NAME; 51 if (!(new File(dbfile).exists())) {// 判断数据库文件是否存在,若不存在则执行导入,否则直接打开数据库 52 InputStream is = context.getResources().openRawResource(R.raw.student); // 欲导入的数据库 53 FileOutputStream fos = new FileOutputStream(dbfile); 54 byte[] buffer = new byte[BUFFER_SIZE]; 55 int count = 0; 56 while ((count = is.read(buffer)) > 0) { 57 fos.write(buffer, 0, count); 58 } 59 fos.close(); 60 is.close(); 61 } 62 SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbfile, 63 null); 64 return db; 65 } catch (FileNotFoundException e) { 66 Log.e("Database", "File not found"); 67 System.out.println("文件啊没找到"); 68 e.printStackTrace(); 69 } catch (IOException e) { 70 Log.e("Database", "IO exception"); 71 System.out.println("io输入输出错误"); 72 e.printStackTrace(); 73 } 74 return null; 75 } 76 77 public void closeDatabase() { 78 this.database.close(); 79 System.out.println("关闭数据库"); 80 } 81 }
参考很多前辈的代码,但是,忘记链接了就没写,多谢这些前辈了
复制db3数据库文件进去
然后就是,复制DataBaseHelper.java,里面的包名数据库名需要修改成你自己的。
然后再MainActivity类加
private DataBaseHelper dataBaseHelper;
在onCreate方法加
sqLiteDBOperator = new SQLiteDBOperator(this);
dataBaseHelper = new DataBaseHelper(this);
dataBaseHelper.openDatabase();
然后
在SQLiteDBOperator.java数据库操作类添加打开数据库的语句:
private DataBaseHelper dataBaseHelper;
public SQLiteDBOperator(Context context) {
//数据库名
dataBaseHelper = new DataBaseHelper(context);
db = SQLiteDatabase.openOrCreateDatabase(DataBaseHelper.DB_PATH + "/" + DataBaseHelper.DB_NAME, null);
System.out.print("打开数据库" + DataBaseHelper.DB_PATH + DataBaseHelper.DB_NAME);}
基本就好了。
2020/5/24