最近的一个项目,需要用到很多数据,所以想的是用数据库的形式进行存储, 虽然安卓中自带的有创建数据库,表的操作,但是因为要插入的数据太多, 太麻烦,所以还是希望用可视化的软件进行制作,然后再导入到项目中. 因此我就用了SQliteStudio进行操作的.接下来我就讲讲如何解决我在这之中遇到的坑.
首先用SQliteStudio写好数据库之后,直接去你创建的文件夹去拷贝你创建的数据库,切记千万不要用软件导出来(因为导出来会选择一个格式,但是并没有发现db,所以导出来后就不是数据库了), 因为我就是导出来发现再用软件打开时打开不了,最好就直接拷贝出来. 这是我遇到的第一个坑.
接下来我拷贝的数据库,拷贝到资源目录(assets)下,然后就把数据库写到文件中(这里我写到了sd卡中,也可以写到/data/data/包名/databases/),我在网上搜了很多,但发现有很多都用不了,最后就自己取他们的有用的代码拼接加上自己写了一些.接下来我来贴下我自己的代码:
首先是SQLiteOpenHelper类
public class BookSqliteOpenHelper extends SQLiteOpenHelper { public BookSqliteOpenHelper(Context context) { super(context, "book.db", null, 1); this.myContext = context; } private Context myContext; //The Android's default system path of your application database. private String DB_PATH = android.os.Environment.getExternalStorageDirectory().getAbsolutePath() + "/booksql/"; private static String DB_NAME = "book.db"; private static String ASSETS_NAME = "book.db"; private SQLiteDatabase myDataBase = null; public void createDataBase() throws IOException { boolean dbExist = checkDataBase(); if (!dbExist) { try { File dir = new File(DB_PATH); if (!dir.exists()) { dir.mkdir(); } File dbf = new File(DB_PATH + DB_NAME); if (dbf.exists()) { dbf.delete(); } SQLiteDatabase.openOrCreateDatabase(dbf, null); copyDataBase(); } catch (IOException e) { throw new Error("数据库创建失败"); } } } private void copyDataBase() throws IOException { InputStream myInput = null; // try { myInput = myContext.getAssets().open(ASSETS_NAME); String outFileName = DB_PATH + DB_NAME; OutputStream myOutput = new FileOutputStream(outFileName); byte[] buffer = new byte[1024]; int length; while ((length = myInput.read(buffer)) > 0) { myOutput.write(buffer, 0, length); } myOutput.flush(); myOutput.close(); myInput.close(); // } catch (IOException e) { // e.printStackTrace(); // } } private boolean checkDataBase() { SQLiteDatabase checkDB = null; String myPath = DB_PATH + DB_NAME; try { checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY); } catch (SQLiteException e) { //database does't exist yet. } if (checkDB != null) { checkDB.close(); } return checkDB != null ? true : false; } @Override public synchronized void close() { if (myDataBase != null) { myDataBase.close(); } super.close(); } @Override public void onCreate(SQLiteDatabase db) { } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } }
然后是Activity中的代码,因为这只是一个demo所以没有放在子线程中进行,希望大家注意. 这里第二个坑就是在获取SQLiteDatabase 的对象时必须要用SQLiteDatabase.openDatabase()或者SQLiteDatabase中其它几个方法,这样才可以对自己写出的文件进行查询.
public class MainActivity extends AppCompatActivity { private String DB_PATH = android.os.Environment.getExternalStorageDirectory().getAbsolutePath() + "/booksql/"; private static String DB_NAME = "book.db"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); BookSqliteOpenHelper helper = new BookSqliteOpenHelper(this); try { helper.createDataBase(); } catch (IOException e) { e.printStackTrace(); } SQLiteDatabase database = SQLiteDatabase.openDatabase(DB_PATH+DB_NAME,null,SQLiteDatabase.OPEN_READWRITE); Cursor cursor = database.query("book", null, null, null, null, null, null, null); while (cursor.moveToNext()){ String bookname = cursor.getString(cursor.getColumnIndex("bookname")); Log.i("11111111111111111",bookname); } } }
最后一个就是权限问题了
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
根据下面网友提供的建议 在进行检查数据库存在时还可以用下面方法进行检测
private boolean checkDataBase() { String myPath = DB_PATH + DB_NAME; File file=new File(myPath); return file.exists(); }