這是我的代碼。我使用「sqlite瀏覽器」創建了一個.db文件,並將其放置到我的工作空間的「資產」文件夾中。但'.db'文件沒有被複制到應用程序路徑中(當我在DDMS(data/data/com.example.trans /)數據庫文件夾沒有被創建時)。此外.db文件不從資產文件夾複製。無法打開真實設備中的db文件
這裏是我的代碼:
public class ConnectionFactory extends SQLiteOpenHelper{
private static String DB_Path = "/data/data/com.example.translationapp/databases/";
public static String DB_Name = "Translator.db";
public SQLiteDatabase myDb;
File dbFile;
public Context con;
public ConnectionFactory(Context context) {
super(context, DB_Name, null,1);
// TODO Auto-generated constructor stub
this.con= context;
}
public void OpenDb() throws IOException
{
boolean dbCheck = checkDb();
if(dbCheck)
{
}
else
{
this.getWritableDatabase();
try
{
copyDatabase();
}
catch(IOException e)
{
throw new Error("Sorry");
}
}
}
public boolean checkDb() throws IOException
{
SQLiteDatabase checkDb =null;
try
{
String myPath = DB_Path+DB_Name;
checkDb = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}
catch(SQLiteException e)
{
}
if(checkDb!= null)
{
checkDb.close();
}
return checkDb !=null ? true:false;
}
public void copyDatabase()throws IOException
{
try
{
File filetext = con.getFileStreamPath(DB_Name);
boolean exists = filetext.exists();
if(!exists)
{
String outFileName = DB_Path+ DB_Name;
myDb=con.openOrCreateDatabase(outFileName, Context.MODE_PRIVATE, null);
OutputStream myOut = new FileOutputStream(filetext.getAbsolutePath());
InputStream myin = con.getAssets().open(DB_Name);
byte[] buffer = new byte[1024];
int length;
while((length = myin.read(buffer))>0)
{
myOut.write(buffer,0,length);
}
myOut.flush();
myOut.close();
myin.close();
}
}
catch(IOException e)
{
e.printStackTrace();
}
}
public void openDatabase() throws SQLException
{
String myPath = DB_Path+DB_Name;
myDb = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}
public synchronized void close()
{
if(myDb != null)
{
myDb.close();
super.close();
}
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
你不會能夠這樣寫的,除非你的設備是植根 – DjHacktorReborn 2013-03-10 11:38:19
@ DjHacktorReborn :請讓我如何rescarve這個問題programitically ..對不起,問plzzzzz – Raviteja 2013-03-10 11:41:37
使用遊標運行查詢您的數據庫搜索存儲的表中,你會發現結果 – DjHacktorReborn 2013-03-10 11:42:54