1
我在assets
文件夾中有一個SQLite數據庫,然後將其複製到應用程序數據文件夾中。
然後我把它複製到我的電腦上查看數據,但是得到下面的錯誤。SQLiteManager:打開文件時出錯?
這裏是我的DB.java:
public class DB extends SQLiteOpenHelper {
public static final String DIR_SDCARD = Environment
.getExternalStorageDirectory().getAbsolutePath();
public static final String DIR_DATABASE = DIR_SDCARD + "/Android/data/";
private static String DB_NAME = "Rulling_DB.sqlite";
private final Context myContext;
public static String PACKAGE_NAME;
public boolean flag = false;
private void copyDataBase() throws IOException {
// Open your local db as the input stream
InputStream myInput = myContext.getAssets().open(DB_NAME);
// Path to the just created empty db
String outFileName = DIR_DATABASE + PACKAGE_NAME + "/Rulling/" + DB_NAME;
// Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(outFileName);
// transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
try {
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
} catch (IOException e) {
Log.e("Copy", e.toString());
}
// Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}
private boolean checkDataBase() {
SQLiteDatabase checkDB = null;
try {
checkDB = SQLiteDatabase.openDatabase(DIR_DATABASE + PACKAGE_NAME
+ "/Rulling/" + DB_NAME, null, 0);
} catch (SQLiteException e) {
Log.e("asdf", "checkDataBase-->" + e.toString());
}
if (checkDB != null) {
checkDB.close();
}
return checkDB != null ? true : false;
}
@Override
public synchronized SQLiteDatabase getReadableDatabase() {
return super.getReadableDatabase();
}
public DB(Context context) {
super(context, DB_NAME, null, 1);
this.myContext = context;
}
public void CreateandOpenDataBase() throws IOException {
boolean dbExist = false;
try {
dbExist = checkDataBase();
} catch (Exception e) {
Log.i("ERROR", "ERROR in DB Class");
}
if (dbExist) {
} else {
try {
copyDataBase();
} catch (IOException e) {
throw new Error("Error copying database --> " + e.toString());
}
}
}
public SQLiteDatabase openDataBase() throws SQLException {
return SQLiteDatabase.openOrCreateDatabase(DIR_DATABASE + PACKAGE_NAME
+ "/Rulling/" + DB_NAME, null);
}
public boolean CreateFile() {
if (flag == false) {
File file = new File(DIR_DATABASE);
file.mkdirs();
return true;
} else {
return true;
}
}
public void GetPackageName(String res) {
PACKAGE_NAME = res;
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
這裏是我的save.java:
public class Save_GetResponse{
public static final String DIR_SDCARD = Environment.getExternalStorageDirectory().getAbsolutePath();
public static final String DIR_DATABASE = DIR_SDCARD +"/Android/data/";
public SQLiteDatabase sql;
public static String PACKAGE_NAME;
public long InsertData(GetReponse_Structure GRS,Context context){
DB db = new DB(context);
PACKAGE_NAME = context.getApplicationContext().getPackageName();
File file= new File(DIR_DATABASE + PACKAGE_NAME + "/Rulling");
file.mkdirs();
db.GetPackageName(PACKAGE_NAME);
db.CreateFile();
try {
db.CreateandOpenDataBase();
} catch (IOException e) {
e.printStackTrace();
}
sql = db.openDataBase();
ContentValues values = new ContentValues();
values.put("ID", GRS._Id);
values.put("Title",GRS._Title);
values.put("MetaDataID", GRS._MetaDataID);
values.put("Comment", GRS._Comment);
values.put("Tafsir", GRS._Tafsir);
values.put("CategoryID", GRS._CategoryID);
values.put("ParentID", GRS._ParentID);
values.put("LanguageID", GRS._LanguageID);
values.put("TypeInfo", GRS._TypeInfo);
values.put("TableName", GRS._TableName);
values.put("Pavaragi", GRS._Pavaragi);
long LastId = sql.insert("Response_tbl", null, values);
sql.close();
return LastId;
}
}
我需要使用此路徑。 –
我在寫一個語音搜索。當用戶說出我爲用戶返回多個數據時,然後我將所有數據存檔。爲了測試,我現在需要看到數據。 –
當我在sqlite上選擇時,從db中獲取數據。但是當我複製在我的電腦上看到所有的數據行讓我錯誤的打開。昨天這工作良好,但今天不工作(無法打開)。 –