2012-02-10 51 views
-1
package com.m; 
import android.content.Context; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteOpenHelper; 

public class HellowWebView extends SQLiteOpenHelper { 
    static final String UR_DB_NAME="mimo.sqlite"; 
    public static final String KEY_ROWID = "_syid"; 
    public static final String KEY_ROWIDc = "_conid"; 
    static final String tableName = "CATEGORY"; 
    static final String New_tableName = "CATEGORY_NEW"; 
    static final String col_name = "NAME"; 
    static final String col_id = "PARENT_ID"; 
    static final String col_name_new = "NAME"; 
    static final String col_id_new = "PARENT_ID"; 
    public static final int version='1'; 
    public static Context context; 

    SQLiteDatabase sql; 

    public HellowWebView(Context context) { 
     super(context, UR_DB_NAME, null, version); 
    } 

    @Override 
    public void onCreate(SQLiteDatabase db) { 
     // TODO Auto-generated method stub 
     try{ 
     sql = context.openOrCreateDatabase(UR_DB_NAME, 0, null); 
     getWritableDatabase(); 
     db.execSQL("CREATE TABLE " + New_tableName + "(_syid INTEGER PRIMARY 
KEY,col_name_new TEXT,col_id_new NUMBER)");  
     db.execSQL("INSERT INTO UR_DB_NAME.New_tableName(col_name_new,col_id_new) 
SELECT * from tableName",null); 
     db.close(); 
     }catch(Exception e){ 
     } 
    } 

    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     // TODO Auto-generated method stub 
    } 
} 
+1

and ...?我在這裏看不到問題或問題 – njzk2 2012-02-10 15:54:54

回答

0

您可以複製上創建的數據庫文件的現有數據庫文件:這個你可以去下面步驟 -

  1. 檢查現有的數據庫。 (第一次沒有分貝)。
  2. 創建數據庫。
  3. 現在,您可以實際上用創建的db文件替換預先創建的數據庫。

這裏是更好的參考代碼:

private boolean checkDataBase() { 
    try { 
     String myPath = DB_PATH + DB_NAME; 
     try { 
      Log.v("---DatabaseHelper--", 
        "---within checkDataBase opening database----"); 
      myDataBase = SQLiteDatabase.openDatabase(myPath, null, 
        SQLiteDatabase.OPEN_READONLY); 

     } catch (SQLiteException e) { 
      Log.v("---DatabaseHelper--", "---within checkDataBase----" + e); 

     } 

     if (myDataBase != null) { 

      myDataBase.close(); 

     } 
    } catch (Throwable e) { 
     StringWriter errorLog = new StringWriter(); 
     e.printStackTrace(new PrintWriter(errorLog)); 
     serverComm.sendErrorLog(errorLog.toString()); 

    } 

    return myDataBase != null ? true : false; 
} 

然後getReadableDatabase(),爲您的應用程序創建一個空白分貝..然後用你的數據庫代替它,你是把它放在資產的文件夾 -

private void copyDataBase() throws IOException { 
    try { 
     Log.v("---DatabaseHelper--", "---within copyDataBase----"); 

     // Open your local db as the input stream 
     InputStream myInput = myContext.getAssets().open(
       "DB_NAME.sqlite"); 
     // Path to the just created empty db 
     String outFileName = DB_PATH + 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; 
     while ((length = myInput.read(buffer)) > 0) { 
      myOutput.write(buffer, 0, length); 

     } 
     Log.v("---DatabaseHelper--", "--- after coping----"); 
     // Close the streams 
     myOutput.flush(); 
     myOutput.close(); 
     myInput.close(); 
    } catch (Throwable e) { 
     StringWriter errorLog = new StringWriter(); 
     e.printStackTrace(new PrintWriter(errorLog)); 
     serverComm.sendErrorLog(errorLog.toString()); 

    } 

} 
0

有一個很好的解決方案,這here

  1. 正在準備SQLite數據庫文件。

假設你已經創建了你的sqlite數據庫,我們需要對它進行一些修改。 如果您沒有sqlite管理員,我建議您下載適用於Win/Linux/Mac的開源SQLite數據庫瀏覽器。

打開數據庫,添加一個名爲「新表格android_metadata」,您可以執行以下SQL語句來做到這一點:

CREATE TABLE "android_metadata" ("locale" TEXT DEFAULT 'en_US') 

在「android_metadata現在,插入一個單行文本「EN_US」 「表:

INSERT INTO "android_metadata" VALUES ('en_US') 

然後,有必要重命名錶的主id字段爲‘_id’因此Android會知道你的表的id字段綁定。 通過按編輯表格按鈕編輯表格,然後選擇要編輯的表格並最終選擇要重命名的字段,可以輕鬆地在SQLite數據庫瀏覽器中執行此操作。

將所有數據表的id字段重命名爲「_id」並添加「android_metadata」表後,您的數據庫就可以在您的Android應用程序中使用了。

enter image description here

注意:在這個圖片,我們看到表「類別」和「內容」與重命名爲「_id」的id字段和剛剛添加表「android_metadata」。

  1. 在您的Android 應用程序中複製,打開並訪問您的數據庫。

現在只需將您的數據庫文件放在項目的「assets」文件夾中,並通過從「android.database.sqlite」包中擴展SQLiteOpenHelper類來創建Database Helper類。

讓您DataBaseHelper類是這樣的:

public class DataBaseHelper extends SQLiteOpenHelper{ 

//The Android's default system path of your application database. 
private static String DB_PATH = "/data/data/YOUR_PACKAGE/databases/"; 

private static String DB_NAME = "myDBName"; 

private SQLiteDatabase myDataBase; 

private final Context myContext; 

/** 
    * Constructor 
    * Takes and keeps a reference of the passed context in order to access to the application assets and resources. 
    * @param context 
    */ 
public DataBaseHelper(Context context) { 

super(context, DB_NAME, null, 1); 
this.myContext = context; 
} 

/** 
    * Creates a empty database on the system and rewrites it with your own database. 
    * */ 
public void createDataBase() throws IOException{ 

boolean dbExist = checkDataBase(); 

if(dbExist){ 
//do nothing - database already exist 
}else{ 

//By calling this method and empty database will be created into the default system path 
//of your application so we are gonna be able to overwrite that database with our database. 
this.getReadableDatabase(); 

try { 

copyDataBase(); 

} catch (IOException e) { 

throw new Error("Error copying database"); 

} 
} 

} 

/** 
    * Check if the database already exist to avoid re-copying the file each time you open the application. 
    * @return true if it exists, false if it doesn't 
    */ 
private boolean checkDataBase(){ 

SQLiteDatabase checkDB = null; 

try{ 
String myPath = DB_PATH + DB_NAME; 
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY); 

}catch(SQLiteException e){ 

//database does't exist yet. 

} 

if(checkDB != null){ 

checkDB.close(); 

} 

return checkDB != null ? true : false; 
} 

/** 
    * Copies your database from your local assets-folder to the just created empty database in the 
    * system folder, from where it can be accessed and handled. 
    * This is done by transfering bytestream. 
    * */ 
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 = DB_PATH + 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; 
while ((length = myInput.read(buffer))>0){ 
myOutput.write(buffer, 0, length); 
} 

//Close the streams 
myOutput.flush(); 
myOutput.close(); 
myInput.close(); 

} 

public void openDataBase() throws SQLException{ 

//Open the database 
String myPath = DB_PATH + DB_NAME; 
myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY); 

} 

@Override 
public synchronized void close() { 

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

super.close(); 

} 

@Override 
public void onCreate(SQLiteDatabase db) { 

} 

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

} 

// Add your public helper methods to access and get content from the database. 
// You could return cursors by doing "return myDataBase.query(....)" so it'd be easy 
// to you to create adapters for your views. 

} 

就是這樣。 現在您可以創建此DataBaseHelper類的新實例,並調用createDataBase()和openDataBase()方法。請記住在DB_PATH字符串中將「YOUR_PACKAGE」更改爲應用程序包名稱空間(即:com.examplename.myapp)。

... 

    DataBaseHelper myDbHelper = new DataBaseHelper(); 
    myDbHelper = new DataBaseHelper(this); 

    try { 

    myDbHelper.createDataBase(); 

    } catch (IOException ioe) { 

    throw new Error("Unable to create database"); 

    } 

    try { 

    myDbHelper.openDataBase(); 

    }catch(SQLException sqle){ 

    throw sqle; 

    } 

    ... 
相關問題