Android 为什么;未能使用chmod“;会发生什么?
我将sqlite数据库复制到assets文件夹的代码如下所示。但是出了点问题`私人语境; 私有字符串db_路径Android 为什么;未能使用chmod“;会发生什么?,android,database,sqlite,Android,Database,Sqlite,我将sqlite数据库复制到assets文件夹的代码如下所示。但是出了点问题`私人语境; 私有字符串db_路径 /*public static final String CREATE_TABLE="CREATE TABLE " + TABLE_NAME +" ("+ KEY_ID+" INTEGER PRIMATY KEY AUTOINCREMENT, "+ KEY_USERNAME+" INTEGER NOT NULL, "+ KEY_PAS
/*public static final String CREATE_TABLE="CREATE TABLE " +
TABLE_NAME +" ("+
KEY_ID+" INTEGER PRIMATY KEY AUTOINCREMENT, "+
KEY_USERNAME+" INTEGER NOT NULL, "+
KEY_PASSWORD+" VARCHAR NOT NULL, "+
KEY_HOMEADDRESS+" VARCHAR, "+
KEY_LICENSENO+" VARCHAR );";*/
我的DbHelper类是
public DbHelper(Context context){
super(context,"DATABASE_NAME",null,DATABASE_VERSION);
this.DATABASE_NAME=DATABASE_NAME;
this.context=context;
db_path="/data/data/"+context.getPackageName()+"/databases/";
}
//create an empty database on the system to rewrite
public void crearDataBase() throws IOException{
boolean dbExists=checkDataBase();
if(dbExists){
}else{
this.getReadableDatabase();
try{
copyDataBase();
}catch(IOException e){
throw new Error("Error copying databse");
}
}
}
private boolean checkDataBase(){
SQLiteDatabase checkDB=null;
try{
String myPath=db_path+DATABASE_NAME;
checkDB=SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}catch(SQLiteException e){
}
if(checkDB!=null){
checkDB.close();
}
return checkDB!=null?true:false;
}
//copy database from local asset-folder to the system folder
private void copyDataBase() throws IOException{
//open local database as the input stream
InputStream myInput=context.getAssets().open(DATABASE_NAME);
//path to the just created empty database
String outFileName=db_path+DATABASE_NAME;
//open the emoty database as the output stream
OutputStream myOutput=new FileOutputStream(outFileName);
//transfer bytes from the input file to the output file
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 void openDataBase() throws SQLException{
//open the database
String myPath=db_path+DATABASE_NAME;
myDataBase=SQLiteDatabase.openDatabase(mypath, null,SQLiteDatabase.OPEN_READONLY);
}*/
@Override
public void onCreate(SQLiteDatabase db) {
//db.execSQL(CREATE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
//delete table
db.execSQL(DROP_TABLE);
onCreate(db);
}`
原木猫是这样的:
05-08 04:24:31.644: D/dalvikvm(7015): GC_FOR_ALLOC freed 124K, 6% free 3242K/3440K, paused 40ms, total 42ms
05-08 04:24:32.134: D/gralloc_goldfish(7015): Emulator without GPU emulation detected.
05-08 04:25:42.644: I/Choreographer(7015): Skipped 62 frames! The application may be doing too much work on its main thread.
05-08 04:25:45.244: W/FileUtils(7015): Failed to chmod(/data/data/com.example.login/databases/DATABASE_NAME): libcore.io.ErrnoException: chmod failed: EPERM (Operation not permitted)
应用程序可以在模拟器上运行,但不能在手机上运行。另外,我的手机没有SD卡,外部sqlite数据库可以吗?1)在线 我想应该是
super(context,DATABASE_NAME,null,DATABASE_VERSION);
2) 数据库名称的值是否真的与资产中的db名称相同
3) 数据库名称应包含扩展名。类似于“mydb.db”的内容。可能重复的是,DATABASE_NAME是资产下的数据库名称。我从文件浏览器中导出它,然后编辑它。如果有扩展,应该在哪里使用“mydb.db”。如果我没有SD卡,会有什么影响?谢谢。1)在初始化玩具数据库名称的地方写一行。2) 您使用内部存储,因此外部存储是否存在并不重要。
super(context,DATABASE_NAME,null,DATABASE_VERSION);