sqlite3 backup

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

//通话记录的数据库privateSQLiteConnectioncallHistoryConn=null;stringstrErrorMessage=string.Empty;//错误信息//数据库本地临时文件privatestringlocalFile=string.Format(@C:\DocumentsandSettings\Administrator\LocalSettings\Temp\AndroidPCSuite\Temp\contacts.db);privatestringlocalDir=string.Format(@C:\DocumentsandSettings\Administrator\LocalSettings\Temp\AndroidPCSuite\Temp);privatestringremote=string.Format(/data/data/com.android.providers.contacts/databases/contacts.db);privatestringbackupFile=string.Format(@D:\我的文档\91Mobile\Android\Backup\contacts.db);privatestringbackupDir=string.Format(@D:\我的文档\91Mobile\Android\Backup);privatevoidDownAndCopyDB(){adb.DownloadFile(remote,localFile);try{if(Directory.Exists(backupDir)){System.IO.File.Copy(localFile,backupFile,true);}else{Directory.CreateDirectory(backupDir);System.IO.File.Copy(localFile,backupFile,true);}}catch(Exception){throw;}}this.StrCnn=DataSource=+localFile+@;Version=3;callHistoryConn=SQLiteClass.GetSQLiteConnection(this.StrCnn);IDataReaderreader=null;reader=SQLiteClass.ExecuteReader(strSQL,this.callHistoryConn,refstrErrMsg);stringsql=select*from[calls]wheretype=+2+andduration+(success?:=)+0;SQLiteClass.ExecuteNoneQuery(DeleteFrom[calls]where_idin+idGroupStr,this.callHistoryConn,null,null,refstrErrMsg);BackingupyourAndroidSQLitedatabasetotheSDcardSubmittedbycharlie.collinsonSun,02/21/2010-11:43Tagged:TutorialsAnotherusefulAndroidprogrammingtipistoeitherallowuserstoselectivelybackupanydatabaseyourapplicationuses,orjustdoitfortheminthebackground(atsomeinterval).Whyisthisimportant?Well,iftheusergetsanewphone(forwhateverreason,lostit,upgraded,chuckeditoutthewindowbeforegoingkayaking)oneofthefirstthingstheywilldoishittheAndroidMarketagainandtrytore-downloadtheapplicationstheyhad(theMarketsavesthisinfoandmakesiteasy,well,generally,thereareafewquirks,butthatisbeyondthescopehere).Also,theymightjustwanttouninstallyourappatsomepointandlatercomebacktoitonanexistingphone.Withoutadatabasebackuptheymightre-acquireyourapplicationbuttheywillbepissed(rightlyso)ifallthedataismissing.Someapplicationshandlethisverywell(notablyEvanCharlton'sMileage,whichisagreatappBTW,hasalwaysupgradedformeflawlessly--Idon'tknowifitusesasimilarmethodtowhatIadvocatehereornot,butitworksverywellhoweveritdoesit),andothersdon't(GoogleListen,Iamlookingatyou;)).TothatendinthisarticlewearegoingtomodifytheAndroidExamplesapplicationwehaveworkedonbeforehereatTotSPtoincludeanewActivitythatallowsuserstobackupthedatabase.Thisisreallyjusttodemonstrateafewwaystodothisratherthananexhaustiveexample,butitshouldgetyoustartedifyouareinterestedinthisarea.OurnewversionofAndroidExamples(completecodeviathelink)willhaveaMenuontheMainactivitythatletsusersgotoanewManageDataactivity--asseeninthescreenshotbelow:OurnewManageDataactivity,whichallowsustobackupourdatabasefile,ANDhandsofftoanothernewclassincasewewanttobackupourdatabaseasXML,isshownbelow:viewsourceprint?001publicclassManageDataextendsActivity{002003privateMyApplicationapplication;004005privateButtonexportDbToSdButton;006privateButtonexportDbXmlToSdButton;007008@Override009publicvoidonCreate(finalBundlesavedInstanceState){010super.onCreate(savedInstanceState);011012this.application=(MyApplication)this.getApplication();013014this.setContentView(R.layout.managedata);015016this.exportDbToSdButton=(Button)this.findViewById(R.id.exportdbtosdbutton);017this.exportDbToSdButton.setOnClickListener(newOnClickListener(){018publicvoidonClick(finalViewv){019if(ManageData.this.isExternalStorageAvail()){020newExportDatabaseFileTask().execute();021}else{022Toast.makeText(ManageData.this,Externalstorageisnotavailable,unabletoexportdata.,023Toast.LENGTH_SHORT).show();024}025}026});027028this.exportDbXmlToSdButton=(Button)this.findViewById(R.id.exportdbxmltosdbutton);029this.exportDbXmlToSdButton.setOnClickListener(newOnClickListener(){030publicvoidonClick(finalViewv){031if(ManageData.this.isExternalStorageAvail()){032newExportDataAsXmlTask().execute(exampledb,exampledata);033}else{034Toast.makeText(ManageData.this,Externalstorageisnotavailable,unabletoexportdata.,035Toast.LENGTH_SHORT).show();036}037}038});039}040041privatebooleanisExternalStorageAvail(){042returnEnvironment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);043}044045privateclassExportDatabaseFileTaskextendsAsyncTaskString,Void,Boolean{046privatefinalProgressDialogdialog=newProgressDialog(ManageData.this);047048//canuseUIthreadhere049protectedvoidonPreExecute(){050this.dialog.setMessage(Exportingdatabase...);051this.dialog.show();052}053054//automaticallydoneonworkerthread(separatefromUIthread)055protectedBooleandoInBackground(finalString...args){056057FiledbFile=058newFile(Environment.getDataDirectory()+/data/com.totsp.androidexamples/databases/example.db);059060FileexportDir=newFile(Environment.getExternalStorageDirectory(),exampledata);061if(!exportDir.exists()){062exportDir.mkdirs();063}064Filefile=newFile(exportDir,dbFile.getName());065066try{067file.createNewFile();068this.copyFile(dbFile,file);069returntrue;070}cat

1 / 21
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功