将.db文件导出并导入我的设备。 你好!我在编程方面有点新,我学会了如何在我的应用程序上创建数据库。 所以我创建了一个包含大量数据的数据库表,并且每件事都运行正常。 是否可以在我的设备或电子邮件上备份.db文件?以及如何将数据库文件再次导入我的应用程序? 对我来说非常重要,我试图在网络上找到答案,但什么都不懂。
public class DbMovie {
//my rows:
public static final String ROWID = "_id";
public static final String MOVIE_NAME = "moviename";
public static final String MOVIE_YEAR = "movieYear";
public static final String MOVIE_ACTORS = "movieActor";
public static final String SYNOPSiS = "synopsis";
public static final String URL_PIC = "pic";
public static final String URL_BIG_PIC = "pic2";
public static final String RATE = "rate";
public static final String TABLE_NAME = "tablename";
public static final String DATABASE_NAME = "dataBasename.db";
public static final int VERSION = 1;
private class Helper extends SQLiteOpenHelper{
public Helper(Context context){
super(context, DATABASE_NAME, null, VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " +TABLE_NAME +
"(" + ROWID + " INTEGER PRIMARY KEY , " +MOVIE_NAME + " TEXT, " +SYNOPSiS + " TEXT,"+MOVIE_YEAR+" TEXT,"+RATE+" TEXT,"+URL_PIC+" TEXT,"+URL_BIG_PIC+" TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
onCreate(db);
}
}
答案 0 :(得分:1)
以下是我使用的导出和导入:
导出:
exportDb.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
File direct = new File(Environment
.getExternalStorageDirectory()
+ "/My Car Fuels Database");
if (!direct.exists()) {
if (direct.mkdir()) {
// directory is created;
}
}
try {
exportDB();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
@SuppressLint("SdCardPath")
private void exportDB() throws IOException {
// Open your local db as the input stream
try {
String inFileName = "/data/data/com.example.mycarfuel/databases/MyDatabase";
File dbFile = new File(inFileName);
FileInputStream fis = new FileInputStream(dbFile);
String outFileName = Environment.getExternalStorageDirectory()
+ "/MyDatabase";
// Open the empty db as the output stream
OutputStream output = new FileOutputStream(outFileName);
// transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = fis.read(buffer)) > 0) {
output.write(buffer, 0, length);
}
// Close the streams
output.flush();
output.close();
fis.close();
} catch (Exception e) {
Toast.makeText(getBaseContext(), e.toString(), Toast.LENGTH_LONG)
.show();
}
Toast.makeText(SettingsActivity.this,
"Database has been saved to your phone memory as 'MyDatabase'",
Toast.LENGTH_LONG).show();
}
导入:
importDb.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
importDB();
}
});
// importing database
@SuppressLint("SdCardPath")
private void importDB() {
try {
// File file = getBaseContext().getFileStreamPath(Environment.getExternalStorageDirectory()
// + "/MyDatabase");
// if(file.exists()){
FileInputStream fis = new FileInputStream(Environment.getExternalStorageDirectory()
+ "/MyDatabase");
String outFileName = "/data/data/com.example.mycarfuel/databases/MyDatabase";
OutputStream output = new FileOutputStream(outFileName);
byte[] buffer = new byte[1024];
int length;
while ((length = fis.read(buffer)) > 0) {
output.write(buffer, 0, length);
}
// Close the streams
output.flush();
output.close();
fis.close();
Toast.makeText(SettingsActivity.this, "Database loaded succesfully",
Toast.LENGTH_LONG).show();
//}else{
//Toast.makeText(SettingsActivity.this, "File does not exist in 'mnt/sdcard/' location. Please add the 'MyDatabase' file here in order to import",
// Toast.LENGTH_LONG).show();
//}
} catch (Exception e) {
Toast.makeText(getBaseContext(), e.toString(), Toast.LENGTH_LONG)
.show();
}
}
希望有所帮助