由于工具的不同,在androidstudio中导入外部数据库也是有所不同,下面教大家怎么在as中导入db,注意往下看
1.切换project视图 src->main->新建assets
至于为什么,往下看:
2.创建DBMain继承自
SQLiteOpenHelper
public class DBMain extends SQLiteOpenHelper { private static String DB_PATH= "data/data/com.safephone.lwp.importdatabase/databases/"; private static String DB_NAME = "ii"; private SQLiteDatabase dbObj; private final Context context; public DBMain(Context context) { super(context, DB_NAME , null, 3); this. context = context; } public void createDB() throws IOException { this.getReadableDatabase(); try { copyDB(); } catch (IOException e) { throw new Error("Error copying database"); } } private boolean checkDB(){ SQLiteDatabase checkDB = null; try{ String path = DB_PATH + DB_NAME; checkDB = SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.OPEN_READONLY); if (checkDB!=null) { Cursor c= checkDB.rawQuery("SELECT * FROM bank", null); Log.i("Cursor.......",c.getString(0)); c.moveToFirst(); String contents[]=new String[80]; int flag=0; while(! c.isAfterLast()) { String temp=""; String s2=c.getString(0); String s3=c.getString(1); String s4=c.getString(2); temp=temp+"\n Id:"+s2+"\tType:"+s3+"\tBal:"+s4; contents[flag]=temp; flag=flag+1; Log.i("DB values.........",temp); c.moveToNext(); } } else { return false; } }catch(SQLiteException e){ e.printStackTrace(); } if(checkDB != null){ checkDB.close(); } return checkDB != null ? true : false; } public void copyDB() throws IOException{ try { InputStream ip = context.getAssets().open(DB_NAME+".db"); Log.i("Input Stream....",ip+""); String op= DB_PATH + DB_NAME ; OutputStream output = new FileOutputStream( op); byte[] buffer = new byte[1024]; int length; while ((length = ip.read(buffer))>0){ output.write(buffer, 0, length); Log.i("Content.... ",length+""); } output.flush(); output.close(); ip.close(); } catch (IOException e) { Log.v("error", e.toString()); } } public void openDB() throws SQLException { String myPath = DB_PATH + DB_NAME; dbObj = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE); Log.i("open DB......",dbObj.toString()); } @Override public synchronized void close() { if(dbObj != null) dbObj.close(); super.close(); } @Override public void onCreate(SQLiteDatabase db) { } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } }3.Mainactivity的oncreate方法中
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); DBMain db; db = new DBMain(this); try { db.createDB(); } catch (IOException ioe) { throw new Error("Database not created...."); } try { db.openDB(); }catch(SQLException sqle){ throw sqle; } SQLiteDatabase db1; db1=openOrCreateDatabase("ii",SQLiteDatabase.CREATE_IF_NECESSARY,null); Cursor c= db1.rawQuery("SELECT * FROM bank",null); c.moveToFirst(); String temp=""; while(! c.isAfterLast()) { String s2=c.getString(0); String s3=c.getString(1); String s4=c.getString(2); temp=temp+"\n Id:"+s2+"\tType:"+s3+"\tBal:"+s4; c.moveToNext(); } }
ok,大功告成,觉得可以请点赞