Android access to external DB files

Source: Internet
Author: User

Note: This code refers to the online example, forgot what address, so can not provide instructions, the original author see Don't blame, can remind me add.

Sometimes you import DB files for operation, and there is a difference in accessing the local database.

First create a folder raw under RES and import the db file into

Then write a method to get Sqlitedatabase

private static Sqlitedatabase database; public static final String database_filename = "city.db"; This is the DB file name public static final String package_name = "com.diz.db"; This is your own. Project package path public static final String Database_path = "/data" + environment.getdatadirectory (). GetAbsolutePath () + "/" + package_name; Get storage location address public static Sqlitedatabase OpenDatabase (context context) {try {String databasefilename = database_p
			ATH + "/" + database_filename;
			File dir = new file (Database_path);
			if (!dir.exists ()) {Dir.mkdir (); } if (! (
				New File (DatabaseFileName)). Exists ()) {InputStream is = Context.getresources (). Openrawresource (r.raw.city);
				FileOutputStream fos = new FileOutputStream (databasefilename);
				byte[] buffer = new byte[8192];
				int count = 0;
				while ((count = is.read (buffer)) > 0) {fos.write (buffer, 0, count);
				} fos.close ();
			Is.close (); Database = Sqlitedatabase.openorcreatedatabase (DatabaseFileName, Null);
		return database;
		catch (Exception e) {e.printstacktrace ();
	return null; }


Using this method, you can get the Sqlitedatabase instance object, and then in the activity

Sqlitedatabase db = OpenDatabase (this);


The database can then be manipulated.

String sql = "SELECT * from tcity where sbelongcode= '";
				Cursor c = db.rawquery (sql, NULL);
				C.movetofirst ();
				while (!c.isafterlast ()) {
					String name = c.getstring (C.getcolumnindex ("sname"));
					LOG.I ("msg", name);
					C.movetonext ();
				}


Here just make a query and then output it to logcat.

The SQL statement is written as you want.

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.