import java.io.File;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.util.Log;
/**
* A helper class to manage database creation and version management.
* You create a subclass implementing {@link #onCreate}, {@link #onUpgrade} and
* optionally {@link #onOpen}, and this class takes care of opening the database
* if it exists, creating it if it does not, and upgrading it as necessary.
* Transactions are used to make sure the database is always in a sensible state.
* <p>For an example, see the NotePadProvider class in the NotePad sample application,
* in the <em>samples/</em> directory of the SDK.</p>
*/
public abstract class CustomSQLiteOpenHelper {
private static final String TAG = CustomSQLiteOpenHelper.class.getSimpleName();
private final Context mContext;
private final String mName;
private final CursorFactory mFactory;
private final int mNewVersion;
private 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 #getReadableDatabase} is called.
*
* @param context to use to open or create the database
* @param name of the database file, or null for an 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} will be used to upgrade the database
*/
public CustomSQLiteOpenHelper(Context context, String name, CursorFactory factory, int version) {
if (version < 1) throw new IllegalArgumentException("Version must be >= 1, was " + version);
mContext = context;
mName = name;
mFactory = factory;
mNewVersion = version;
}
/**
* Create and/or open a database that will be used for reading and writing.
* Once opened successfully, the database is cached, so you can call this
* method every time you need to write to the database. Make sure to call
* {@link #close} when you no longer need it.
*
* <p>Errors such as bad permissions or a full disk may cause this operation
* to fail, but future attempts may succeed if the problem is fixed.</p>
*
* @throws SQLiteException if the database cannot be opened for writing
* @return a read/write database object valid until {@link #close} is called
*/
public synchronized SQLiteDatabase getWritableDatabase() {
if (mDatabase != null && mDatabase.isOpen() && !mDatabase.isReadOnly()) {
return mDatabase; // The database is already open for business
}
if (mIsInitializing) {
throw new IllegalStateException("getWritableDatabase called recursively");
}
// If we have a read-only database open, someone could be using it
// (though they shouldn't), which would cause a lock to be held on
// the file, and our attempts to open the database read-write would
// fail waiting for the file lock. To prevent that, we acquire the
// lock on the read-only database, which shuts out other users.
boolean success = false;
SQLiteDatabase db = null;
try {
mIsInitializing = true;
if (mName == null) {
db = SQLiteDatabase.create(null);
} else {
String path = getDatabasePath(mName).getPath();
db = SQLiteDatabase.DatabopenOrCreatease(path, mFactory);
}
int version = db.getVersion();
if (version != mNewVersion) {
db.beginTransaction();
try {
if (version == 0) {
onCreate(db);
} else {
onUpgrade(db, version, mNewVersion);
}
db.setVersion(mNewVersion);
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
}
onOpen(db);
success = true;
return db;
} finally {
mIsInitializing = false;
if (success) {
if (mDatabase != null) {
try { mDatabase.close(); } catch (Exception e) { }
}
mDatabase = db;
} else {
if (db != null) db.close();
}
}
}
/**
* Create and/or open a database. This will be the same object returned by
* {@link #getWritableDatabase} unless some problem, such as a full disk,
* requires the database to be opened read-only. In that case, a read-only
* database object will be returned. If the problem is fixed, a future call
* to {@link #getWritableDatabase} may succeed, in which case the read-only
* database object will be closed and the read/write object will be 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 recursively");
}
try {
return getWritableDatabase();
} catch (SQLiteException e) {
if (mName == null) throw e; // Can't open a temp database read-only!
Log.e(TAG, "Couldn't open " + mName + " for writing (will try read-only):", e);
}
SQLiteDatabase db = null;
try {
mIsInitializing = true;
String path = getDatabasePath(mName).getPath();
db = 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;
}
}
/**
* Called when the database is 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 be upgraded. The implementation
* should use this method to drop tables, add tables, or do anything else it
* needs to upgrade to the new schema version.
*
* <p>The SQLite ALTER TABLE documentation can be found
* <a href="http://sqlite.org/lang_altertable.html">here</a>. If you add new columns
* you can use ALTER TABLE to insert them into a live table. If you rename or remove columns
* you can use ALTER TABLE to rename the old table, then create the new table and then
* populate 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) {}
/**
* 获取DB存储路径
*/
public File getDatabasePath(String dbpath)
{
return new File(Environment.getExternalStorageDirectory()+"/" + dbpath);
}
}
引用自
http://www.eoeandroid.com/forum-viewthread-tid-54969-fromuid-98242.html
分享到:
相关推荐
SQLite 是一个轻量级的关系型数据库管理系统,广泛应用于嵌入式系统和移动应用中。它无需服务器进程,可以直接在客户端应用程序中使用。JDBC (Java Database Connectivity) 是Java语言访问数据库的标准接口,允许...
10. **丰富的SQL支持**:SQLite3实现了大部分SQL92标准,包括子查询、视图、触发器和存储过程。 解压"sqlite-autoconf-3330000.tar.gz"后,您将得到SQLite3的源代码,可以进行以下操作: 1. **编译构建**:使用...
System.Data.SQLite.dll是SQLite在.NET平台上的一个封装库,它为C#和其他.NET语言提供了对SQLite数据库引擎的全面支持。SQLite是一款轻量级、自包含的数据库系统,广泛应用于嵌入式系统和移动应用,因为它不需要单独...
DB.Browser.for.SQLite-3.12.2-win64 这是一款由多位开发者共同开发的工具,最初由 Mauricio Piacentini 设计并发布到公共领域。该工具主要用于浏览和编辑 SQLite 数据库,它兼容标准的 SQLite 2.x 和 3.x 数据库...
SQLite是一款开源、轻量级的嵌入式关系型数据库,广泛应用于移动应用、小型设备以及对数据存储有需求但资源有限的系统。`sqlite-autoconf-3290000.tar.gz` 是SQLite的一个源码包,版本号为3290000,通过这个包我们...
总的来说,`sqlite-jdbc-3.8.7.jar`是Java开发人员连接SQLite数据库的重要工具,它简化了数据库操作,使开发者能够利用SQLite的轻量级特性在Java应用中实现数据存储和管理。在实际项目中,需要根据具体需求和环境来...
System.Data.SQLite 是一个开源的 .NET Framework 数据提供程序,它实现了 SQLite 数据库引擎与 ADO.NET 的集成。这个版本号1.0.112.0的源码包,据描述,是支持内嵌加密功能的最后一个版本。这在开发过程中具有重要...
在标题"sqlite-autoconf.tar.gz_sqlite_sqlite 加密_sqlite3autoconf_wxsqlite"中,我们可以看到与 SQLite 相关的几个关键点:加密、sqlite3autoconf 和 wxSQLite。 1. **SQLite 加密**: SQLite 提供了一个可选的...
DB Browser for SQLite 是一个可视化工具。用于创建,设计和编辑SQLite兼容的数据库文件。它适用于希望创建数据库,检索和编辑数据的用户和开发人员。它采用了熟悉的电子表格一样的界面,你不需要学习复杂的SQL命令...
sqlite3提供了加密函数,但没有实现。 ...使用时,把sqlite3.lib和sqlite3.dll链接到你的工程中,在sqlite3_open后使用sqlite3_key(db,szPassword,nPasswordLen);,其他和未加密的库使用方法一样。
2. **src/**:源代码目录,包含SQLite的主要C语言实现,如数据库引擎的核心代码、SQL解析器、VFS(虚拟文件系统)层等。 3. **docs/**:文档目录,包括API参考、设计文档和用户指南,帮助开发者理解和使用SQLite。 4...
DBBrowser,全称为DB.Browser.for.SQLite,是一款开源、免费且功能强大的SQLite数据库管理工具,专为非技术用户和开发者设计。它提供了图形化的用户界面,使得用户无需编写SQL命令即可进行数据库的创建、浏览、编辑...
`sqlite3.c`包含了SQLite的全部实现,而`sqlite3.h`是相应的头文件,定义了接口和数据类型。 集成SQLite到你的项目中,只需将这两个文件添加到你的工程,并链接SQLite库。在C/C++项目中,可以像这样创建和操作...
总之,sqlitedb库文件是SQLite数据库的一种存储形式,具备轻量、独立、易用的特点,广泛应用于各种应用场景。了解其基本操作和特性对于进行数据管理至关重要。通过使用相应的工具和编程接口,开发者可以高效地处理和...
SQLite 是一个开源、轻量级、自包含的数据库引擎,广泛应用于嵌入式系统和移动应用中。"sqlite-snapshot-202101271915.tar.gz" 是一个特定时间点(2021年1月27日19时15分)SQLite 数据库的快照,打包成的tar.gz格式...
SQLite是一款开源、轻型的数据库管理系统,被广泛应用于各种领域,尤其适合于嵌入式系统和需要快速开发简单数据存储的应用。"sqlite-3.3.8.tar.gz" 是SQLite数据库的一个版本,版本号为3.3.8,通常以tar.gz格式打包...
1. **src**:SQLite的核心源代码,包括解析器、SQL解释器、B树实现、事务处理等。 2. **test**:测试套件,用于验证SQLite的功能和性能,这些测试用例对于理解和调试SQLite很有帮助。 3. **doc**:文档资料,如API...
标题 "DB.Browser.for.SQLite-3.12.2-win32.zip" 指的是一个针对Windows 32位系统的DB Browser for SQLite的版本3.12.2的压缩包。DB Browser for SQLite是一款开源、免费且用户友好的图形界面工具,用于浏览和操作...
System.Data.SQLite 和 SQLite.Interop 是两个在.NET环境中使用SQLite数据库的关键组件。SQLite是一个开源、轻量级、自包含的SQL数据库引擎,广泛应用于嵌入式系统和桌面应用中,尤其是那些需要快速、可靠且无需...
1. `sqlite3.c`: 包含了SQLite的主要实现,是一个大的C语言源文件,包含了所有的SQLite函数和逻辑。 2. `sqlite3.h`: 头文件,包含了SQLite的公共API,供其他C语言程序调用。 3. `Makefile`: 构建脚本,指导如何编译...