SQLite 데이터베이스를 sdcard로 내보낼 수있게되었습니다. 누군가가 Sqlite 데이터베이스를 장치로 가져 오는 데 도움이된다면 감사하겠습니다.프로그래밍 방식으로 장치에 Sqlite 데이터베이스 가져 오기
나는 newbi in android 나는 성공적으로 많이 시도했다.
수정 ... 사전에
public class MainActivity extends Activity implements OnClickListener {
private static final String SAMPLE_DB_NAME = "TrekBook";
private static final String SAMPLE_TABLE_NAME = "Info";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.button1).setOnClickListener(this);
findViewById(R.id.button2).setOnClickListener(this);
findViewById(R.id.button3).setOnClickListener(this);
findViewById(R.id.button4).setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.button1:
deleteDB();
break;
case R.id.button2:
exportDB();
break;
case R.id.button3:
createDB();
break;
case R.id.button4:
importDB();
break;
}
}
private void deleteDB(){
boolean result = this.deleteDatabase(SAMPLE_DB_NAME);
if (result==true) {
Toast.makeText(this, "DB Deleted!", Toast.LENGTH_LONG).show();
}
}
private void exportDB(){
File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();
FileChannel source=null;
FileChannel destination=null;
String currentDBPath = "/data/"+ "com.authorwjf.sqliteexport" +"/databases/"+SAMPLE_DB_NAME;
String backupDBPath = SAMPLE_DB_NAME;
File currentDB = new File(data, currentDBPath);
File backupDB = new File(sd, backupDBPath);
try {
source = new FileInputStream(currentDB).getChannel();
destination = new FileOutputStream(backupDB).getChannel();
destination.transferFrom(source, 0, source.size());
source.close();
destination.close();
Toast.makeText(this, "DB Exported!", Toast.LENGTH_LONG).show();
} catch(IOException e) {
e.printStackTrace();
}
}
private void createDB() {
SQLiteDatabase sampleDB = this.openOrCreateDatabase(SAMPLE_DB_NAME, MODE_PRIVATE, null);
sampleDB.execSQL("CREATE TABLE IF NOT EXISTS " +
SAMPLE_TABLE_NAME +
" (LastName VARCHAR, FirstName VARCHAR," +
" Rank VARCHAR);");
sampleDB.execSQL("INSERT INTO " +
SAMPLE_TABLE_NAME +
" Values ('Kirk','James, T','Captain');");
sampleDB.close();
sampleDB.getPath();
Toast.makeText(this, "DB Created @ "+sampleDB.getPath(), Toast.LENGTH_LONG).show();
}
private void importDB(){
File f=new File("/data/data/com.authorwjf.sqliteexport/databases/TrekBook");
FileInputStream fis=null;
FileOutputStream fos=null;
try
{
fis=new FileInputStream(f);
fos=new FileOutputStream("/mnt/sdcard/DB/TrekBook");
while(true)
{
int i=fis.read();
if(i!=-1)
{fos.write(i);}
else
{break;}
}
fos.flush();
Toast.makeText(this, "DB dump OK", Toast.LENGTH_LONG).show();
}
catch(Exception e)
{
e.printStackTrace();
Toast.makeText(this, "DB dump ERROR", Toast.LENGTH_LONG).show();
}
finally
{
try
{
fos.close();
fis.close();
}
catch(Exception ioe)
{}
}
}
}
activity_main
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SQLite DB to SD Demo"/>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Delete" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Export" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Create" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Import" />
</LinearLayout>
감사합니다 ....
덕분에 간단한 계획을 도와, 미안, 그것은 말한다 .. 나 지금 시도 DB 덤프를 – user1710911
하자 ... 멀리 내 컴퓨터에서이었다 오류 – user1710911
왜 ?? 당신은 신중하게 그것을 교차 확인 PATH을 변경합니다. –