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