我在我的電腦(本地主機)上擁有數千個記錄的數據庫。我只想將包含這些記錄的錶轉換爲可在Android上訪問的數據庫。我聽說過SQLite,但無論如何將.sql
文件轉換爲SQLite數據庫並將其脫機存儲在我的android手機上?將.sql錶轉換爲SQLITE數據庫?
2
A
回答
2
是的,你可以做到這一點。首先,您需要將.sql文件轉換爲android能夠理解的sqlite數據庫。在你的新的SQLite架構中,你需要
android_metadata表,並插入一個語言環境行。我定住爲 「en_US」
CREATE TABLE android_metadata(區域TEXT)
一個表來保存數據。您可以根據自己的喜好命名該表,但請確保您已將_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.
}
相關問題
- 1. 將MySql數據庫轉換爲SQLite(Tricky!)
- 2. 將sqlite數據庫轉換爲SQLAnywhere?
- 3. 將SQLite數據庫轉換爲Java Arraylist
- 4. 將SQL數據庫轉換爲.sql
- 5. 將SQL Azure轉換爲SQL數據庫
- 6. 如何將SQL CE 3.5數據庫轉換爲SQLite
- 7. 將Access數據庫轉換爲SQL
- 8. 將XML轉換爲SQL Server數據庫
- 9. 將Access數據庫轉換爲SQL Server
- 10. 將SQL Server數據庫轉換爲MySQL
- 11. 如何將excel錶轉換爲android的sqlite數據庫
- 12. 如何將excel錶轉換爲android sqlite數據庫
- 13. 將服務器數據轉換爲Sqlite數據庫
- 14. 將XML數據設計轉換爲SQLite數據庫設計
- 15. 將excel數據轉換爲SQL表
- 16. 將數據庫錶轉換爲JTree
- 17. 將SQL Server數據庫轉換爲本地C#數據庫
- 18. 如何將Visual Foxpro數據庫轉換爲SQL Server數據庫
- 19. 將SQL Server數據庫轉換爲MYSQL數據庫
- 20. 將內存中的sqlite數據庫轉換爲char數組
- 21. 將內存中的sqlite數據庫轉換爲blob/char數組
- 22. 高效地將Beastly SQL錶轉換爲多個Access數據庫
- 23. 將圖表數據庫轉換爲關係數據庫
- 24. 像SQLMetal.exe一樣將SQLite數據庫文件轉換爲「LINQ to SQL」
- 25. 如何將MySQL數據庫文件(.sql)轉換爲PHP中的SQLite文件?
- 26. 將SQLite數據庫表發送到SQL服務器數據庫
- 27. 將SQLite數據庫轉換爲三重存儲
- 28. 將二進制文件轉換爲SQLite數據庫
- 29. 將sqlite中的XBMC數據庫模式4.0a轉換爲mysql
- 30. 將MYSQL轉換爲適用於Android的SQLITE數據庫
需要更多關於.sql文件格式的信息。有很多sql數據庫格式。 – lentz
更多的信息會很好。不知道你的SQL文件的確切格式,因爲SQL是一種語言而不是特定的文件格式,你可以嘗試在Firefox中使用「SQLite Manager」擴展。 – lentz