我的Databasehelper.class
public class DatabaseHelper extends sqliteOpenHelper { public static final String DBname = "db3000.sqlite"; public static final String DBLOCATION = "/data/data/com.gnirt69.sqlitefromassetexample/databases/"; private Context mContext; private sqliteDatabase mDatabase; public DatabaseHelper(Context context) { super(context, DBname, null, 1); this.mContext = context; } @OverrIDe public voID onCreate(sqliteDatabase db) { } @OverrIDe public voID onUpgrade(sqliteDatabase db, int oldVersion, int newVersion) { } public voID openDatabase() { String dbPath = mContext.getDatabasePath(DBname).getPath(); if(mDatabase != null && mDatabase.isopen()) { return; } mDatabase = sqliteDatabase.openDatabase(dbPath, null, sqliteDatabase.OPEN_READWRITE); } public voID closeDatabase() { if(mDatabase!=null) { mDatabase.close(); } }public ArrayList<word> getListWord() { word product = null; ArrayList<word> productList = new ArrayList<>(); openDatabase(); Cursor cursor = mDatabase.rawquery("SELECT * FROM word", null); cursor.movetoFirst(); while (!cursor.isAfterLast()) { product = new word(cursor.getInt(0), cursor.getString(1), cursor.getString(2), cursor.getString(3),cursor.getString(4),cursor.getInt(5)); productList.add(product); cursor.movetoNext(); } cursor.close(); closeDatabase(); return productList; }}
我的framgnet.class
@OverrIDe public voID onVIEwCreated(VIEw vIEw, Bundle savedInstanceState) { super.onVIEwCreated(vIEw, savedInstanceState); mDBHelper = new DatabaseHelper(getActivity()); //Check exists database file database = getActivity().getDatabasePath(DatabaseHelper.DBname); if(false == database.exists()) { mDBHelper.getReadableDatabase(); //copy db if(copyDatabase(getActivity())) { Toast.makeText(getActivity(), "copy database succes", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(getActivity(), "copy data error", Toast.LENGTH_SHORT).show(); return; } }// Get product List in db when db exists mProductList = mDBHelper.getListWord(); // getcategoryFromDataBase(); }private boolean copyDatabase(Context context) { try { inputStream inputStream = context.getAssets().open(DatabaseHelper.DBname); String outfilename = DatabaseHelper.DBLOCATION + DatabaseHelper.DBname; OutputStream outputStream = new fileOutputStream(outfilename); byte[]buff = new byte[1024]; int length = 0; while ((length = inputStream.read(buff)) > 0) { outputStream.write(buff, 0, length); } outputStream.flush(); outputStream.close(); Log.w("MainActivity","DB copIEd"); return true; }catch (Exception e) { e.printstacktrace(); return false; } }
最初,我尝试在Activity.class中调用读取数据;它工作得很好,但是当我在片段中尝试它时它不会运行和显示
java.io.fileNotFoundException: /data/data/com.gnirt69.sqlitefromassetexample/databases/db3000.sqlite: open Failed: EACCES (Permission denIEd)
这里发生了什么?
请帮我.
解决方法:
public class category extends Fragment {Context con;public DBHelper db; @Suppresslint("NewAPI") @OverrIDe public VIEw onCreateVIEw(LayoutInflater inflater, VIEwGroup container, Bundle savedInstanceState) { con=getActivity(); VIEw rootVIEw = inflater.inflate(R.layout.activity_category, container, false); db=new DBHelper(con); try { db.createDataBase(); db.exportDataBase(); } catch (IOException e) { // Todo auto-generated catch block e.printstacktrace(); } //insert data in table db.dml("insert into tablename(c_ID,c_name,c_subcat_ID,c_icon)values('"+t1+"','"+t2+"','"+t3+"','"+t4+"')"); //delete data in table db.dml("delete from tablename"); //update table db.dml("update tablename set colum=value where ID=1"); filldata(); return rootVIEw; } public voID filldata() { try { Cursor c=db.getData("select * from tablename"); while(c.movetoNext()) { ID=c.getString(0); name=c.getString(1); city=c.getString(2); } } catch(Exception e) { e.printstacktrace(); } }}/* DBHelper class */public class DBHelper extends sqliteOpenHelper { private static String DB_name = "your database name"; // Read sqlite from assets folder in sqlite database private sqliteDatabase db; private final Context context; private String DB_PATH; String outfilename=""; SharedPreferences.Editor spEdit; public DBHelper(Context context) { super(context, DB_name, null, 1); this.context = context; DB_PATH = "/data/data/" + context.getPackagename() + "/" + "databases/"; } public voID exportDataBase() throws IOException { Calendar c = Calendar.getInstance(); SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy"); String formattedDate = df.format(c.getTime()); fileinputStream dbfile = new fileinputStream(DB_PATH+""+DB_name); String outfilename = Environment.getExternalStorageDirectory()+"/"+formattedDate+""+DB_name+".db"; String outfilename1 = Environment.getExternalStorageDirectory()+"/"; file yourfile = new file(outfilename1); if(!yourfile.exists()) { yourfile.mkdir(); } OutputStream myOutput = new fileOutputStream(outfilename); byte[] buffer = new byte[1024]; int length; while ((length = dbfile.read(buffer)) > 0) { myOutput.write(buffer, 0, length); } // Close the streams myOutput.flush(); myOutput.close(); }public voID createDataBase() throws IOException { boolean dbExist = checkDataBase(); //------------------------------------------------------------ PackageInfo pinfo = null; if(!dbExist){ getReadableDatabase(); copyDataBase(); } } private boolean checkDataBase() { file dbfile = new file(DB_PATH + DB_name); return dbfile.exists(); } private voID copyDataBase() throws IOException { inputStream myinput = context.getAssets().open(DB_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); } // Close the streams myOutput.flush(); myOutput.close(); myinput.close(); } public Cursor getData(String query) { String myPath = DB_PATH + DB_name; db = sqliteDatabase.openDatabase(myPath, null, sqliteDatabase.OPEN_READWRITE); try{ Cursor c = db.rawquery(query, null); return c; }catch(Exception e){ return null; } } //UPDATE temp_dquot SET age='20',name1='--',rdt='11/08/2014',basic_sa='100000',plno='814',pterm='20',mterm='20',mat_date='11/08/2034',mode='YLY',dab_sa='100000',tr_sa='0',cir_sa='',bonus_rate='42',prem='5276',basic_prem='5118',dab_prem='100.0',step_rate='for life',loyal_rate='0',bonus_rate='42',act_mat='1,88,000',mly_b_pr='448',qly_b_pr='1345',hly_b_pr='2664',yly_b_pr='5276' WHERE uniqID=1 public voID dml(String query) { String myPath = DB_PATH + DB_name; if(db==null) db = sqliteDatabase.openDatabase(myPath, null, sqliteDatabase.OPEN_READWRITE); try{ db.execsql(query); }catch(Exception e){ Log.e("Error",e.toString()); } }@OverrIDepublic voID onCreate(sqliteDatabase db) { // Todo auto-generated method stub}@OverrIDepublic voID onUpgrade(sqliteDatabase db, int oldVersion, int newVersion) { // Todo auto-generated method stub} } /* add permission in manifest */<uses-permission androID:name="androID.permission.WRITE_EXTERNAL_STORAGE" /><uses-permission androID:name="androID.permission.READ_EXTERNAL_STORAGE" />
总结 以上是内存溢出为你收集整理的android – 从assets文件夹中读取sqlite错误全部内容,希望文章能够帮你解决android – 从assets文件夹中读取sqlite错误所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)