我的计算机上有一个数据库(localhost),有数千条记录。我只想将包含这些记录的表转换为可在Android上访问的数据库。我听说过SQLite,但是有没有将.sql
文件转换为SQLite数据库并将其在我的Android手机上脱机存储?
答案 0 :(得分:2)
是的,你可以这样做。首先,您需要将.sql文件转换为Android可以理解的sqlite数据库。在新的sqlite架构中,您需要
android_metadata表,并插入一个区域设置行。我将我的设置为“en_US”
CREATE TABLE android_metadata(locale TEXT)
用于保存数据的表格。您可以根据自己的喜好命名此表,但请确保将_id作为主键。例如
CREATE TABLE mydata(_id INTEGER PRIMARY KEY,data TEXT)
然后,您需要将.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.
}