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.