2014-07-19 34 views
0

我試圖通過將資產文件夾複製到設備來使用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只創建兩個空白表,我也想要裏面的數據。由於表中有數百個條目,所以我無法手動添加它們。

請人幫助....

+0

How do you say你變得空白DB?你是怎麼檢查的? –

+0

@Paresh Mayani是的...... –

+1

它給出了一個錯誤,說沒有找到這樣的表。所以爲了使程序運行,我必須從DDMS推送db –

回答

0

所以最後我做到了通過以下DBHelper類工作: -

public class DBHelper extends SQLiteOpenHelper { 

public SQLiteDatabase database = null; 
public File databaseFile; 
public static String databaseName = "collegeDb.sqlite"; 
public String databasePath = ""; 
Context mContext; 

public DBHelper(Context paramContext) { 

    super(paramContext, databaseName, null, 1); 
    this.mContext = paramContext; 

    Log.d("data", "package name:" + paramContext.getPackageName()); 

    this.databasePath = ("data/data/" + paramContext.getPackageName() + "/databases/"+databaseName); 
    this.databaseFile = new File(this.databasePath); 
    if (!this.databaseFile.exists()) 
     try { 
      deployDataBase(DBHelper.databaseName, this.databasePath); 
      return; 
     } catch (IOException localIOException) { 
      localIOException.printStackTrace(); 
     } 
} 

private void deployDataBase(String dbNAme, String dbPath) 
     throws IOException { 
    InputStream localInputStream = this.mContext.getAssets().open(dbNAme); 
    FileOutputStream localFileOutputStream = new FileOutputStream(dbPath); 
    byte[] arrayOfByte = new byte[1024]; 
    while (true) { 
     int i = localInputStream.read(arrayOfByte); 
     if (i <= 0) { 
      localFileOutputStream.flush(); 
      localFileOutputStream.close(); 
      localInputStream.close(); 
      return; 
     } 
     localFileOutputStream.write(arrayOfByte, 0, i); 
    } 
} 

@Override 
public synchronized void close() { 

    if (database != null) 
     database.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 

} 
/** 
* Getting all colleges 
* returns list of colleges 
* */ 
public List<String> getAllColleges(){ 
    List<String> colleges = new ArrayList<String>(); 

    // Select All Query 
    String selectQuery = "SELECT * FROM collegeslist"; 

    SQLiteDatabase db = this.getReadableDatabase(); 
    Cursor cursor = db.rawQuery(selectQuery, null); 

    // looping through all rows and adding to list 
    if (cursor.moveToFirst()) { 
     do { 
      colleges.add(cursor.getString(1)); 
     } while (cursor.moveToNext()); 
    } 

    // closing connection 
    cursor.close(); 
    db.close(); 

    // returning colleges 
    return colleges; 
} 

/** 
    * Getting all products 
    * returns list of products 
    * */ 
public List<String> getAllProducts(){ 
    List<String> products = new ArrayList<String>(); 

    // Select All Query 
    String selectQuery = "SELECT * FROM productslist"; 

    SQLiteDatabase db = this.getReadableDatabase(); 
    Cursor cursor = db.rawQuery(selectQuery, null); 

    // looping through all rows and adding to list 
    if (cursor.moveToFirst()) { 
     do { 
      products.add(cursor.getString(1)); 
     } while (cursor.moveToNext()); 
    } 

    // closing connection 
    cursor.close(); 
    db.close(); 

    // returning products 
    return products; 
} 
} 

感謝您的幫助的朋友。

0

您需要添加的數據庫名稱的數據庫文件的路徑:

private static String DB_NAME = "collegeDb2"; 

應該是這樣的:

private static String DB_NAME = "<path_where_you_are_copying_db>/collegeDb2.db"; 

例:如果你已經複製你的db文件在這裏:/storage/sdcard0/test.db,那麼它應該是:

private static String DB_NAME = "/storage/sdcard0/test.db"

另外你不需要你的createDatabase()方法。至於如果它不將數據庫自動生成存在時,它的super被稱爲

+1

你能看到'String myPath = DB_PATH + DB_NAME;'代碼中嗎?' –

+1

我在'myPath' –

相關問題