1) 버전 번호는 새로운 버전의 데이터베이스, 즉 해당 스키마의 수정을 나타냅니다.
2) 새 테이블을 만들면 데이터베이스 스키마가 변경되므로 버전을 증가시켜야합니다.
4) 제 경험상 'adb 제거 패키지'를 사용하여 데이터베이스를 삭제할 수 있습니다. 이는 특히 콘텐츠 제공 업체가있는 경우 유용합니다. 제거를 수행하면 콘텐츠 공급자도 제거됩니다. 주어진 지점에서 다시 시작하고 데이터베이스에 표를 추가하고 올바른 시점에 도달 할 때까지 다시 테스트하여 adb 제거를 수행 한 다음 시작 데이터베이스 사본을 프로젝트 자산 폴더에 넣은 다음 존재하지 않는 경우 제공된 코드를 사용하여 데이터베이스를 다시 작성하십시오.
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);
}
}
*