डेटाबेस संस्करणों हैंडलिंग अनुप्रयोग विकास की बहुत ही महत्वपूर्ण हिस्सा है:
DatabaseHelper.java
public class DatabaseHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 4;
private static final String DATABASE_NAME = "database.db";
private static final String TAG = DatabaseHelper.class.getName();
private static DatabaseHelper mInstance = null;
private final Context context;
private DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
this.context = context;
}
public static synchronized DatabaseHelper getInstance(Context ctx) {
if (mInstance == null) {
mInstance = new DatabaseHelper(ctx.getApplicationContext());
}
return mInstance;
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(BookEntry.SQL_CREATE_BOOK_ENTRY_TABLE);
// The rest of your create scripts go here.
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.e(TAG, "Updating table from " + oldVersion + " to " + newVersion);
// You will not need to modify this unless you need to do some android specific things.
// When upgrading the database, all you need to do is add a file to the assets folder and name it:
// from_1_to_2.sql with the version that you are upgrading to as the last version.
try {
for (int i = oldVersion; i < newVersion; ++i) {
String migrationName = String.format("from_%d_to_%d.sql", i, (i + 1));
Log.d(TAG, "Looking for migration file: " + migrationName);
readAndExecuteSQLScript(db, context, migrationName);
}
} catch (Exception exception) {
Log.e(TAG, "Exception running upgrade script:", exception);
}
}
@Override
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
private void readAndExecuteSQLScript(SQLiteDatabase db, Context ctx, String fileName) {
if (TextUtils.isEmpty(fileName)) {
Log.d(TAG, "SQL script file name is empty");
return;
}
Log.d(TAG, "Script found. Executing...");
AssetManager assetManager = ctx.getAssets();
BufferedReader reader = null;
try {
InputStream is = assetManager.open(fileName);
InputStreamReader isr = new InputStreamReader(is);
reader = new BufferedReader(isr);
executeSQLScript(db, reader);
} catch (IOException e) {
Log.e(TAG, "IOException:", e);
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
Log.e(TAG, "IOException:", e);
}
}
}
}
private void executeSQLScript(SQLiteDatabase db, BufferedReader reader) throws IOException {
String line;
StringBuilder statement = new StringBuilder();
while ((line = reader.readLine()) != null) {
statement.append(line);
statement.append("\n");
if (line.endsWith(";")) {
db.execSQL(statement.toString());
statement = new StringBuilder();
}
}
}
}
एक उदाहरण परियोजना एक ही लिंक भी प्रदान की जाती है। मुझे लगता है कि आपके पास पहले से ही श्रेणी AppDbHelper SQLiteOpenHelper
है। जब आप इसे विस्तारित करेंगे तो आपको onCreate
और onUpgrade
विधि लागू करने की आवश्यकता होगी।
onCreate
और onUpgrade
तरीकों
onCreate
जब एप्लिकेशन नव स्थापित कहा जाता है कहा जाता है।
onUpgrade
जब ऐप अपडेट किया गया था।
डेटाबेस संस्करणों को व्यवस्थित करना मैं कक्षा विधियों में संस्करणों का प्रबंधन करता हूं। इंटरफेस माइग्रेशन के कार्यान्वयन बनाएँ। जैसे पहले संस्करण के लिए MigrationV1
वर्ग बनाने के लिए, दूसरा संस्करण MigrationV1ToV2
बनाने (ये मेरे नामकरण परंपरा हैं)
public interface Migration {
void run(SQLiteDatabase db);//create tables, alter tables
}
उदाहरण माइग्रेशन:
public class MigrationV1ToV2 implements Migration{
public void run(SQLiteDatabase db){
//create new tables
//alter existing tables(add column, add/remove constraint)
//etc.
}
}
- प्रवासन वर्गों का उपयोग करना
onCreate
: चूंकि onCreate
कॉल किया जाएगा एप्लिकेशन ताजा स्थापित है, हमें सभी माइग्रेशन (डेटाबेस संस्करण अपडेट) निष्पादित करने की भी आवश्यकता है। तो onCreate
इच्छा इस तरह दिखता है:
public void onCreate(SQLiteDatabase db){
Migration mV1=new MigrationV1();
//put your first database schema in this class
mV1.run(db);
Migration mV1ToV2=new MigrationV1ToV2();
mV1ToV2.run(db);
//other migration if any
}
onUpgrade
: जब आवेदन पहले से स्थापित है इस विधि के नाम से जाना जाएगा और यह नया आवेदन संस्करण में अपडेट है। यदि एप्लिकेशन में कोई डेटाबेस परिवर्तन होता है तो नए माइग्रेशन क्लास में सभी डेटाबेस परिवर्तन और डेटाबेस संस्करण में वृद्धि करें।
उदाहरण के लिए, मान लें कि उपयोगकर्ता ने एप्लिकेशन इंस्टॉल किया है जिसमें डेटाबेस संस्करण 1 है, और अब डेटाबेस संस्करण 2 को अपडेट किया गया है (MigrationV1ToV2
में रखे गए सभी स्कीमा अपडेट)। डेटाबेस स्कीमा के लिए onCreate
में मार डाला जाना चाहिए सभी उन्नयन (
onUpgrade
में उल्लेख किया है): अब जब आवेदन के लिए उन्नत, हम इस तरह
MigrationV1ToV2
में डेटाबेस स्कीमा परिवर्तनों को लागू करने से डेटाबेस अपग्रेड करने की आवश्यकता:
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if (oldVersion < 2) {
//means old version is 1
Migration migration = new MigrationV1ToV2();
migration.run(db);
}
if (oldVersion < 3) {
//means old version is 2
}
}
नोट
[यह] (https://stackoverflow.com/a/3424444/3681880) एक और दिलचस्प समाधान है, लेकिन अब तक का सबसे मजबूत संस्करण मैंने देखा है [यहां] (https://riggaroo.co.za/android-SQLite-डेटाबेस का उपयोग-onupgrade-सही ढंग से /)। – Suragch