Public abstract class Sqliteopenhelper {private static final String TAG = SQLiteOpenHelper.class.getSimpleName ();p rivate Final Context mcontext;private final String mname;private final cursorfactory mfactory;private final int Mnewversion;priv Ate sqlitedatabase Mdatabase = null;private Boolean misinitializing = false;/** * Create a Helper object to create, open, and/or manage a database. The * database is not actually created or opened until one of * {@link #getWritableDatabase} or {@link #getReadableDatabas e} is called. * * @param context * To use to open or create the database * @param name * of the database file, or Null for a in-memory database * @param factory * To use for creating cursor objects, or NULL for the default * @param version * Number of the database (starting at 1); If the database is * older, {@link #onUpgrade} 'll be used to upgrade the database */public Sdcardsqliteopenhe Lper (context context, String name,cursOrfactory Factory, int version) {if (version < 1) throw new IllegalArgumentException ("version must is >= 1, was" + ve rsion); mcontext = Context;mname = Name;mfactory = Factory;mnewversion = version;} /** * Create and/or open a database that would be is used for reading and writing. * Once opened successfully, the database is cached, so can call this * method every time you need to write to the data Base. Make sure-to-call * {@link #close} if you longer need it. * * <p> * Errors such as bad permissions or a full disk could cause this operation to * fail, but the future attempts may Succeed if the problem is fixed. * </p> * * @throws sqliteexception * If the database cannot is opened for writing * @return a Read/writ E database object Valid until {@link #close} is called */public synchronized sqlitedatabase getwritabledatabase () {if (mDa Tabase! = null && mdatabase.isopen () &&!mdatabase.isreadonly ()) {return mdatabase;//The database is ALR EadyOpen for Business}if (misinitializing) {throw new IllegalStateException ("Getwritabledatabase called recursively");} If we have a read-only database open, someone could is using it//(though they shouldn ' t), which would cause a lock to be held on//the file, and we attempts to open the database Read-write would//fail waiting for the file lock. To prevent this, we acquire the//lock on the read-only database, which shuts off other users.boolean success = false; Sqlitedatabase db = null;try {misinitializing = true;if (Mname = = null) {db = Sqlitedatabase.create (null);} else {String P Ath = Getdatabasepath (mname). GetPath ();d b = sqlitedatabase.openorcreatedatabase (path, mfactory);} int version = Db.getversion (), if (version! = mnewversion) {db.begintransaction (); try {if (Version = = 0) {onCreate (db);} El SE {onupgrade (db, version, mnewversion);} Db.setversion (mnewversion);d b.settransactionsuccessful ();} finally {db.endtransaction ();}} OnOpen (db); success = True;return db;} catch (Exception e) {e.Printstacktrace ();} finally {misinitializing = False;if (Success) {if (mdatabase! = null) {try {mdatabase.close ();} catch (Exception e) {}}mda Tabase = db;} else {if (db! = null) Db.close ();}} return DB;} /** * Create and/or open a database. This would be is the same object returned by * {@link #getWritableDatabase} unless some problem, such as a full disk, * Requir Es the database to is opened read-only. In this case, a read-only * database object would be returned. If the problem is fixed, a future call * to {@link #getWritableDatabase} may succeed, in which case the read-only * Databa Se object would be closed and the Read/write object is returned * in the future. * * @throws sqliteexception * If the database cannot be opened * @return a database object valid until {@link #getWritableDatabase} or * {@link #close} is called. */public synchronized Sqlitedatabase getreadabledatabase () {if (mdatabase! = null && mdatabase.isopen ()) {return Mdatabase; The database is already open for business}if (misinitializing) {throw new IllegalStateException ("Getreadabledatabase called recursive Ly ");} try {return getwritabledatabase ();} catch (Sqliteexception e) {if (mname = = null) throw e;//Can ' t open a temp database re ad-only! LOG.E (TAG, "couldn ' t open" + mname+ "for writing (would try Read-only):", e);} Sqlitedatabase db = null;try {misinitializing = true; String path = Getdatabasepath (Mname). GetPath ();d b = sqlitedatabase.opendatabase (path, Mfactory,sqlitedatabase.open_ READWRITE); if (db.getversion ()! = mnewversion) {throw new Sqliteexception ("Can ' t upgrade read-only database from version" + db.getversion () + "to" + Mnewversion + ":" + path); OnOpen (DB); LOG.W (TAG, "opened" + Mname + "in read-only mode"); mdatabase = Db;return mdatabase;} finally {misinitializing = false;if (db! = NULL && DB! = mdatabase) Db.close ();}} /** * Close any open database object. */public synchronized void Close () {if (misinitializing) throw new IllegalStateException ("CloseD during initialization "); if (mdatabase! = null && mdatabase.isopen ()) {mdatabase.close (); mdatabase = null;}} Public File Getdatabasepath (string name) {String Extern_path = Null;if (Environment.getexternalstoragestate (). Equals ( environment.media_mounted) = = True) {String DbPath = mcontext.getstring (r.string.dir) + mcontext.getstring (r.string.db _dir) + "/"; Extern_path = Android.os.Environment.getExternalStorageDirectory (). GetAbsolutePath () + DbPath; File F = new file (Extern_path), if (!f.exists ()) {f.mkdirs ();}} return new File (Extern_path + name);} /** * Called when the database was created for the first time. This is where the * creation of tables and the initial population of the tables should * happen. * * @param DB * the database. */public abstract void OnCreate (Sqlitedatabase db);/** * Called when the database needs to is upgraded. The implementation should * use this method to drop tables, add tables, or does anything else it needs * To upgrade to the N EW schema version. * * <p> * The SQLite ALTER TABLE documentation can be found <a * href= "http://sqlite.org/lang_altertable.html" &G T;here</a>. If you add new * columns you can use the ALTER table to insert them into a live TABLE. If You * Rename or remove columns your can use ALTER table to rename the old table, * then create the new table and then PO Pulate The new table with the * contents of the old table. * * @param DB * the database. * @param oldversion * The old database version. * @param newversion * The new database version. */public abstract void Onupgrade (sqlitedatabase db, int oldversion,int newversion);/** * Called when the database has been Opened. Override method should check * {@link sqlitedatabase#isreadonly} before updating the database. * * @param DB * the database. */public void OnOpen (Sqlitedatabase db) {}}
In fact, the path to the generated file is changed.
Put Android SQL files on the SD card