1) 版本号表示数据库的较新版本,即对其模式的修改。
2) 新表的创建改变了数据库模式,所以你应该增加版本。
4) 根据我的经验,您可以使用“adb uninstall your package”删除数据库,如果您有内容提供商,这将特别有用。卸载还会删除内容提供程序。如果您想从给定点重新启动并说将表添加到数据库并重新测试直到正确为止,您将执行 adb uninstall ...并将起始数据库的副本放在项目资产文件夹中,然后使用如果数据库不存在,则提供用于重新创建数据库的代码。
5) 您可以通过多种方式查看数据库文件。在我的 Windows 7 平台上使用过 2 个独立程序;SQLite 数据库浏览器 2.0 b1 和 sqlitestudio。如果你使用 eclipse 你并添加插件 SQLLIteManager,com.questoid.sqlitemanager_1.0.0.jar。
希望这可以帮助。
这是我的 dbhelper 的源代码,如果项目不存在,我会在其中复制项目的数据库。
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class TDAdbHelper extends SQLiteOpenHelper {
private static String DATABASE_PATH;
private static final String DATABASE_NAME = "tda.db";
private static final int DATABASE_VERSION = 1;
private Context context;
private SQLiteDatabase db;
TDAdbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
this.context = context;
String packageName = context.getPackageName();
DATABASE_PATH = String.format("//data//data//%s//databases//", packageName);
Log.i(this.getClass().toString(), "... before calling openDatabase ");
openDataBase();
Log.i(this.getClass().toString(), "... after return openDatabase ");
}
@Override
public void onCreate(SQLiteDatabase db) {
Log.i(this.getClass().toString(), "... Starting TDAdb.onCreate ");
TDAdb.onCreate(db);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
TDAdb.onUpgrade(db, oldVersion, newVersion);
}
//Performing a database existence check
private boolean checkDataBase() {
Log.i(this.getClass().toString(), "... Starting checkDatabase ");
SQLiteDatabase checkDb = null;
try {
String path = DATABASE_PATH + DATABASE_NAME;
checkDb = SQLiteDatabase.openDatabase(path, null,
SQLiteDatabase.OPEN_READONLY);
} catch (SQLException e) {
Log.e(this.getClass().toString(), "Error while checking db");
}
//Android doesn’t like resource leaks, everything should
// be closed
if (checkDb != null) {
checkDb.close();
}
return checkDb != null;
}
//Method for copying the database
private void copyDataBase() throws IOException {
//Open a stream for reading from our ready-made database
//The stream source is located in the assets
Log.i(this.getClass().toString(), "... in copyDataBase ");
InputStream externalDbStream = context.getAssets().open(DATABASE_NAME);
//Path to the created empty database on your Android device
String outFileName = DATABASE_PATH + DATABASE_NAME;
//Now create a stream for writing the database byte by byte
OutputStream localDbStream = new FileOutputStream(outFileName);
//Copying the database
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = externalDbStream.read(buffer)) > 0) {
localDbStream.write(buffer, 0, bytesRead);
}
//Don’t forget to close the streams
localDbStream.close();
externalDbStream.close();
}
//This piece of code will create a database if it’s not yet created
public void createDataBase() {
Log.i(this.getClass().toString(), "... in createDataBase ");
boolean dbExist = checkDataBase();
if (!dbExist) {
this.getReadableDatabase();
try {
copyDataBase();
} catch (IOException e) {
Log.e(this.getClass().toString(), "Copying error");
throw new Error("Error copying database!");
}
} else {
Log.i(this.getClass().toString(), "Database already exists");
}
}
public SQLiteDatabase openDataBase() throws SQLException {
String path = DATABASE_PATH + DATABASE_NAME;
Log.i(this.getClass().toString(), "Starting openDatabase " + path);
if (db == null) {
createDataBase();
db = SQLiteDatabase.openDatabase(path, null,
SQLiteDatabase.OPEN_READWRITE);
}
return db;
}
}
注意我在 db.java 中所做的修改
*
/* private static final String DATABASE_CREATE =
"CREATE TABLE if not exists " + CHAPTER_TABLE + " (" +
KEY_ROWID + " integer PRIMARY KEY autoincrement," +
COL_CHAPTER + "," +
COL_CHAPTERTITLE + "," +
// KEY_CONTINENT + "," +
" UNIQUE (" + COL_CHAPTER +"));"; */
public static void onCreate(SQLiteDatabase db) {
Log.i(LOG_TAG, "onCreate");
}
public static void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(LOG_TAG, "Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
onCreate(db);
}
}
*