Android copies the database in the project to SD

Source: Internet
Author: User
Put the database file under assets to write code
	/** * Copy Database * * @param CTX * @param isnew/public void copydatabase (Context ctx, Boolean isnew) {//whether the initial  The database if (isnew) {//Check SQLite database file exists if ((new File (Db_path + db_name)). Exists () = false) {//AS
		        The SQLite database file does not exist, and then check to see if there is a file F = new file (Db_path) in the DB directory;
		        If the database directory does not exist, create the new directory if (!f.exists ()) {F.mkdir (); try {//Get assets directory where we implement ready SQLite database as input stream InputStream is = Ctx.getassets ()
		            . open (db_name);
		
		            Output stream OutputStream OS = new FileOutputStream (Db_path + db_name);
		            File Write byte[] buffer = new byte[1024];
		            int length;
		            while (length = is.read (buffer) > 0) {os.write (buffer, 0, length);
		            ///Close file stream Os.flush ();
		            Os.close ();
		        Is.close (); catch (ExceptIon e) {e.printstacktrace (); }
		    }
		}
	}
Two important variables:
	Database path
	final static String Db_path = "/data/data/com.example.test/database/";
	Database name
	final static String db_name = "Ywyd.sqlite";
Whether the test was successful:
public void TestData () {
		///below test/data/data/com.test.db/databases/whether the database works Sqlitedatabase db
        = Sqlitedatabase.openorcreatedatabase (Db_path + db_name, null);
        Cursor Cursor = Database.rawquery ("SELECT * from Sys_config", null);
        if (Cursor.getcount () > 0) {
            cursor.movetofirst ();
			String strtest = cursor.getstring (3);
			See if the output information is correct
			System.out.println (strtest);
        }
        Cursor.close ();
	}
Called in the activity:
Sqllitehelper helper = new Sqllitehelper ();
		Helper.copydatabase (This.getbasecontext (), true);
		Helper.testdata ();
Original Address http://blog.csdn.net/yueritian/article/details/46471669

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.