我是android开发公司的新手。我使用资产中的.db文件。我可以创建新的桌子吗?
发布于 2013-10-25 10:36:34
如果您的DB位于资产中,则必须在应用程序开始时将其复制到本地路径中。见下面的代码:
try {
String inputFilename = "database_name.db";
InputStream input = getContext().getAssets().open(inputFilename);
String outputFilename = getContext().getFilesDir().getPath()+inputFilename;
OutputStream output = new FileOutputStream(outputFilename);
byte[] buffer = new byte[1024];
int length;
while((length = input.read(buffer)) > 0) {
output.write(buffer,0,length);
}
output.flush();
output.close();
input.close();
} catch (IOException e) {
e.printStackTrace();
}现在,您可以打开DB:
SQLiteDatabase db = SQLiteDatabase.openDatabase(activity.getFilesDir().getPath()+"database_name.db",null,SQLiteDatabase.OPEN_READWRITE);
try {
db.beginTransaction();
db.execSQL("CREATE TABLE IF NOT EXISTS table_name ( ... )");
db.setTransactionSuccessful();
} catch (SQLException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} finally {
db.endTransaction();
}发布于 2013-10-25 10:11:39
使用Sqlite插件为Firefox打开Sqlite。创建和删除表可以使用sqlite管理器完成。
https://stackoverflow.com/questions/19586714
复制相似问题