2013-12-22 200 views
2

我在我的電腦(本地主機)上擁有數千個記錄的數據庫。我只想將包含這些記錄的錶轉換爲可在Android上訪問的數據庫。我聽說過SQLite,但無論如何將.sql文件轉換爲SQLite數據庫並將其脫機存儲在我的android手機上?將.sql錶轉換爲SQLITE數據庫?

+0

需要更多關於.sql文件格式的信息。有很多sql數據庫格式。 – lentz

+0

更多的信息會很好。不知道你的SQL文件的確切格式,因爲SQL是一種語言而不是特定的文件格式,你可以嘗試在Firefox中使用「SQLite Manager」擴展。 – lentz

回答

2

是的,你可以做到這一點。首先,您需要將.sql文件轉換爲android能夠理解的sqlite數據庫。在你的新的SQLite架構中,你需要

  1. android_metadata表,並插入一個語言環境行。我定住爲 「en_US」

    CREATE TABLE android_metadata(區域TEXT)

  2. 一個表來保存數據。您可以根據自己的喜好命名該表,但請確保您已將_id作爲主鍵。例如

    CREATE TABLE MYDATA(_id INTEGER PRIMARY KEY,數據文本)

然後,您需要從.sql文件導入數據到新的數據表。你可能會發現SQLiteBrowser有用http://sourceforge.net/projects/sqlitebrowser/

一旦你這樣做了,你現在有一個sqlite數據庫,你可以加載,並從你的android應用程序讀取。

將您的sqlite數據庫複製到您的android項目資產文件夾中。然後,您可以創建數據庫幫助程序,它將打開並構建應用程序可以訪問的脫機sqlite數據庫。

import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.OutputStream; 

import android.content.Context; 
import android.database.SQLException; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteException; 
import android.database.sqlite.SQLiteOpenHelper; 
import android.util.Log; 

public class MyDbHelper extends SQLiteOpenHelper { 
    // The Android's default system path of your application database. 
    private static String DB_PATH = "/data/data/yourpackagename/databases/"; 

    private static String DB_NAME = "your_db_name.db"; 

    private static final String DATA_TABLE_NAME = "your_data_table_name"; 
    private static final String COLUMN_YOUR_DATA_COLUMN_NAME = "data"; 

    private SQLiteDatabase myDataBase; 

    private final Context myContext; 

    /** 
    * Constructor: MyDbHelper Takes and keeps a reference of the passed context 
    * in order to access to the application assets and resources. 
    * 
    * @param context 
    */ 
    public MyDbHelper(Context context) { 
     super(context, DB_NAME, null, 1); 
     this.myContext = context; 
    } 

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

     boolean dbExist = checkDataBase(); 

     if (dbExist) { 
      Log.i("info", "db exists. do nothing"); 
      // 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. 
      Log.i("info", "creating new db"); 
      this.getReadableDatabase(); 
      this.close(); 

      try { 
       copyDataBase(); 
      } catch (IOException e) { 
       close(); 
       throw new Error("Error copying database"); 
      } 
     } 
    } 

    /** 
    * Function: checkDataBase() 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; 
    } 

    /** 
    * Function: copyDataBase() 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(); 

    } 

    /** 
    * Function: openDataBase() 
    * 
    */ 
    public void openDataBase() throws SQLException { 
     // Open the database 
     String myPath = DB_PATH + DB_NAME; 
     myDataBase = SQLiteDatabase.openDatabase(myPath, null, 
       SQLiteDatabase.OPEN_READONLY); 
    } 

    /** 
    * Function: close() 
    * 
    * @Override close() method 
    */ 
    @Override 
    public synchronized void close() { 

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

    } 

    /** 
    * Function: onCreate() 
    * 
    * @Override onCreate() method 
    */ 
    @Override 
    public void onCreate(SQLiteDatabase db) { 

    } 

    /** 
    * Function: onUpgrade() 
    * 
    * @Override onUpgrade() method 
    */ 
    @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. 

} 
相關問題