我試圖通過將資產文件夾複製到設備來使用SQLite數據庫。如果db正在被複制,我也檢查過使用DDMS。一切都很好,但是當我打開我複製的數據庫時,它裏面沒有表格,即它是一個空白的數據庫。將數據從資產複製到設備內存中的db文件夾
請幫助我如何將db和表一起復制。
這是我的DBHelper類: -
public class DBHelper extends SQLiteOpenHelper {
private static String DB_NAME = "collegeDb2";
private SQLiteDatabase db;
private final Context context;
private String DB_PATH;
public DBHelper(Context context) {
super(context, DB_NAME, null, 1);
this.context = context;
DB_PATH = context.getApplicationInfo().dataDir + "/databases/";
}
public void createDataBase() throws IOException {
boolean dbExist = checkDataBase();
if (dbExist) {
} else {
this.getReadableDatabase();
try {
copyDataBase();
} catch (IOException e) {
throw new Error("Error copying database");
}
}
}
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 getColleges() {
String myPath = DB_PATH + DB_NAME;
db = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READONLY);
Cursor c = db.rawQuery("SELECT * FROM collegeslist", null);
// Note: colleges is the one table in External db. Here we trying to access the records of table from external db.
return c;
}
public Cursor getProducts() {
String myPath = DB_PATH + DB_NAME;
db = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READONLY);
Cursor d = db.rawQuery("SELECT * FROM productslist", null);
// Note: products is the one table in External db. Here we trying to access the records of table from external db.
return d;
}
@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
}
我能夠添加使用此代碼表中onCreate()
: -
public void onCreate(SQLiteDatabase db) {
String CREATE_collegeslistTable = "CREATE TABLE collegeslist ("
+ "_id INTEGER PRIMARY KEY NOT NULL ,Organization_Name VARCHAR DEFAULT (null) ,Organization_No VARCHAR DEFAULT (null) ,Assigned_To VARCHAR DEFAULT (null) ,Billing_Address VARCHAR DEFAULT (null) ,Billing_City VARCHAR DEFAULT (null))";
db.execSQL(CREATE_collegeslistTable);
String CREATE_productslistTable = "CREATE TABLE productslist ("
+ "_id INTEGER PRIMARY KEY NOT NULL ,Product_Name VARCHAR DEFAULT (null) ,Part_Number VARCHAR DEFAULT (null) ,Manufacturer VARCHAR DEFAULT (null) ,Product_Category VARCHAR DEFAULT (null) ,Vendor_Name VARCHAR DEFAULT (null), Unit_Price DOUBLE DEFAULT (null), Usage_Unit VARCHAR DEFAULT (null), Unit INTEGER DEFAULT (null))";
db.execSQL(CREATE_productslistTable);
}
但這在DB只創建兩個空白表,我也想要裏面的數據。由於表中有數百個條目,所以我無法手動添加它們。
請人幫助....
How do you say你變得空白DB?你是怎麼檢查的? –
@Paresh Mayani是的...... –
它給出了一個錯誤,說沒有找到這樣的表。所以爲了使程序運行,我必須從DDMS推送db –