2015-05-29 25 views
0

我在服務器上有一個數據庫作爲應用程序內置庫。 下載:(1)沒有這樣的數據庫表,從URL下載

private static boolean downloadDatabase(Context context) { 
    try { 
      // Log.d(TAG, "downloading database"); 
      URL url = new URL("http://url/"); 
      /* Open a connection to that URL. */ 
      URLConnection ucon = url.openConnection(); 
      /* 
      * Define InputStreams to read from the URLConnection. 
      */ 
      InputStream is = ucon.getInputStream(); 
      BufferedInputStream bis = new BufferedInputStream(is); 
      /* 
      * Read bytes to the Buffer until there is nothing more to read(-1). 
      */ 
      ByteArrayBuffer baf = new ByteArrayBuffer(50); 
      int current = 0; 
      while ((current = bis.read()) != -1) { 
        baf.append((byte) current); 
      } 

      /* Convert the Bytes read to a String. */ 
      FileOutputStream fos = null; 
      // Select storage location 
      fos = context.openFileOutput(Global.getName(), Context.MODE_PRIVATE); 

      fos.write(baf.toByteArray()); 
      fos.close(); 
      // Log.d(TAG, "downloaded"); 
    } catch (IOException e) { 
      Log.e(TAG, "downloadDatabase Error: " , e); 
      return false; 
    } catch (NullPointerException e) { 
      Log.e(TAG, "downloadDatabase Error: " , e); 
      return false; 
    } catch (Exception e){ 
      Log.e(TAG, "downloadDatabase Error: " , e); 
      return false; 
    } 
    return true; 

這是DBHelper,其中數據庫複製到數據/數據/ ...目錄。

public class DBHelper extends SQLiteOpenHelper { 

private static String DB_PATH = "/data/data/mypackage/databases"; 
public static String DB_NAME = Global.getName(); 
private static final int DB_VERSION = 1; 
private static final String SP_KEY_DB_VER = "db_ver"; 

private SQLiteDatabase db; 
private Context context; 

public DBHelper(Context context) { 

     super(context, DB_NAME, null, DB_VERSION); 
     this.context = context; 
     initialize(); 
    } 

    private void initialize() { 
     if (databaseExists()) { 
      SharedPreferences prefs = PreferenceManager 
        .getDefaultSharedPreferences(context); 
      int dbVersion = prefs.getInt(SP_KEY_DB_VER, 1); 
      if (DB_VERSION != dbVersion) { 
       File dbFile = context.getDatabasePath(DB_NAME); 
       if (!dbFile.delete()) { 
        Log.w("MY APP", "Unable to update database"); 
       } 
      } 
     } 
     if (!databaseExists()) { 
      createDatabase(); 
     } 
    } 

    private boolean databaseExists() { 
     File dbFile = context.getDatabasePath(DB_NAME); 
     Log.e("TAG","database exist?"+dbFile.exists()); 
     return dbFile.exists(); 
    } 

    /** 
    * Creates database by copying it from assets directory. 
    */ 
    private void createDatabase() { 
     String parentPath = context.getDatabasePath(DB_NAME).getParent(); 
     String path = context.getDatabasePath(DB_NAME).getPath(); 
     File file = new File(parentPath); 
     if (!file.exists()) { 
      if (!file.mkdir()) { 
       Log.w("MY APP", "Unable to create database directory"); 
       return; 
      } 
     } 

     InputStream is = null; 
     OutputStream os = null; 
     try { 
      is = context.openFileInput(Global.getDictionary()); 
      os = new FileOutputStream(path); 

      byte[] buffer = new byte[1024]; 
      int length; 
      while ((length = is.read(buffer)) > 0) { 
       os.write(buffer, 0, length); 
      } 
      os.flush(); 

     } catch (IOException e) { 
      e.printStackTrace(); 
     } finally { 
      if (is != null) { 
       try { 
        is.close(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
      } 
      if (os != null) { 
       try { 
        os.close(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
      } 
     } 
    } 


    public void openDataBase() throws SQLException{ 

     String myPath = DB_PATH + DB_NAME; 
     db= SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE); 

    } 


    @Override 
    public synchronized void close() { 

      if(db != null) 
       db.close(); 

      super.close(); 

    } 




public String getStr() { 
    databaseExists(); 
    SQLiteDatabase db = this.getReadableDatabase(); 
    String s=null; 
    Cursor c = db.rawQuery("SELECT indexStr from tablename where id=2 ",null); 
    while (c.moveToNext()) { 
      s = c.getString(0); 
    } 
    c.close(); 
    db.close(); 
    return s; 
} 



@Override 
public void onCreate(SQLiteDatabase arg0) {} 

@Override 
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {} 

我明白了,我的應用程序下載аfter數據庫佔用的內存更多的空間,但是當我運行任何SELECT返回「(1)沒有這樣的表」,但數據庫中存在。可能數據庫爲空,但爲什麼? P.S.所有權限寫入WRITE_EXTERNAL_STORAGE/READ_EXTERNAL_STORAGE。

回答

0

我覺得你的表名是不正確

Cursor c = db.rawQuery("SELECT indexStr from tablename where id=2 ",null); 

表的名稱是真正的「tablename」 ???

檢查的

public static String DB_NAME = Global.getName(); 

也許你正在閱讀不同的數據庫中的值!

+0

我寫這個就像一個例子。 – arcenciel4

0

嘗試將此代碼添加到onCreate()方法並查看錯誤是否消失。

SQLiteDatabase db2 = openOrCreateDatabase("dbname", Context.MODE_PRIVATE, null); 
    db2.execSQL("CREATE TABLE IF NOT EXISTS table_name(row1 INT PRIMARY KEY, row2 VARCHAR, row3 VARCHAR, row4 VARCHAR,);"); 

如果是這樣,那表示您的表格不是根據您之前嘗試的方式創建的。

檢查數據庫是否填充的最佳方法是單擊DDMS並檢查數據庫文件是否在您的項目下創建。如果它被創建,從設備中拉出文件,並通過SQLite數據庫瀏覽器查看它,如SQLiteBrowser

相關問題