mardi 21 juillet 2015

android :Export data from asset's folder (sqllite db) and import it in app's database

I am trying to create a method where i can copy data stored in an sqllite db into my app's DB. The structure is similiar. I just dont want to insert rows one by one.

I am looking for a method through which i copy pre-entered data stored in assets folder or external storage to my apps db.

I am having trouble filling data in my app. I do not want to write 5000 insert statements programitically for each row and column.

Can anyone suggest a method. I am new to android so please be explain in detail or provide a link.

My DB Helper class

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DBHelper extends SQLiteOpenHelper {
    public  SQLiteDatabase db1;


    // Static Final Variable database meta information

    static final String DATABASE = "assesmenttool.db";
    static final int DATABASE_VERSION = 1;

    //Table Student Details
    static final String TABLEStudent = "StudentDetails";
    static final String S_ID = "_id";
    static final String SchoolID = "SchoolID";
    static final String SchoolName = "schoolname";
    static final String StudentFirstName = "StudentFirstName";
    static final String StudentLastName ="StudentLastName";
    static final String StudentClassLevel ="StudentClassLevel";
    static final String RollNo="RollNo";
    static final String TestDate ="TestDate";

     //Table Response Details   
    static final String TABLEResponse = "TableResponse";
    static final String R_ID = "_id";
    static final String StudentID = "StudentID";
    static final String R_QuestionID = "QuestionID";
    static final String QuestOptionID = "QuestOptionID";

    //Table Question Master
    static final String TableQuestionMaster = "TableQuestionMaster";
    static final String Q_ID= "_id";
    static final String Module_ID = "Module_ID";
    static final String SubModule_ID = "SubModule_ID";
    static final String SubModuleQuestion_ID ="SubModuleQuestion_ID";
    static final String Question_ID= "Question_ID"; 
    static final String Title = "Title";
    static final String Module = "Module";
    static final String TitleDescription = "TitleDescription";
    static final String QuestionText = "QuestionText";
    static final String QuestionImage = "QuestionImage";
    static final String QuestionTemplate = "QuestionTemplate";
    static final String CorrectOptionID = "CorrectOptionID";

    //Table Template Master
    static final String TableTemplateMaster = "TemplateMaster";
    static final String T_ID= "_id";
    static final String Template_ID= "Template_ID"; 
    static final String Name = "Name";
    static final String Description = "Description";

    //Table Question Option 
    static final String TableQuestionOption = "TableQuestionOption";
    static final String TQP_ID= "_id";
    static final String TQP_QuestionID = "QuestionID";
    static final String OptionText = "OptionText";

    //Table Class Master
    static final String TableClassMaster = "TableClassMaster";
    static final String Class_ID= "_id";
    static final String Class = "class";


    // Override constructor
    public DBHelper(Context context) {
        super(context, DATABASE, null, DATABASE_VERSION);

    }


    // Override onCreate method
    @Override
    public void onCreate(SQLiteDatabase db) {


        //Create Table Student Details
        db.execSQL("CREATE TABLE " + TABLEStudent + " ( " + S_ID
                + " INTEGER PRIMARY KEY AUTOINCREMENT, " + SchoolID + " text, "
                + SchoolName + " text, " + StudentFirstName + " text, "  + StudentLastName + " text, " + RollNo + " text," + TestDate + " text," + StudentClassLevel + " text)");


        //Create Table Response Details     
        db.execSQL("CREATE TABLE " + TABLEResponse + " ( " + R_ID
                + " INTEGER PRIMARY KEY AUTOINCREMENT, " + StudentID + " text, "
                + R_QuestionID + " text, " + QuestOptionID + " text)");


        //Create Table Question Master
        db.execSQL("CREATE TABLE " + TableQuestionMaster + " ( " + Q_ID
                + " INTEGER PRIMARY KEY AUTOINCREMENT, " +  Question_ID + " text, " +  Module_ID + " text, " +  SubModule_ID + " text, " +  SubModuleQuestion_ID + " text,  " + Title + " text, "  +  Module + " text," 
                + TitleDescription + " text, " + QuestionText + " text, "  + QuestionImage + " text, " + QuestionTemplate + " text," + CorrectOptionID + " text)");


        //Create Table Template Master
        db.execSQL("CREATE TABLE " + TableTemplateMaster + " ( " + T_ID
                + " INTEGER PRIMARY KEY AUTOINCREMENT, " + Name + " text, "
                + Description + " text)");


        //Create Table Question Option  
        db.execSQL("CREATE TABLE " + TableQuestionOption + " ( " + TQP_ID
                + " INTEGER PRIMARY KEY AUTOINCREMENT, " + TQP_QuestionID + " text, "
                + OptionText + " text)");   

        //Create Table Class Master 
        db.execSQL("CREATE TABLE " + TableClassMaster + " ( " + Class_ID
                + " INTEGER PRIMARY KEY AUTOINCREMENT, " + Class + " text)");   

    }

       public List<String> getAllClasses(){
            List<String> labels = new ArrayList<String>();

            // Select All Query
            String selectQuery = "SELECT  * FROM " + TableClassMaster;

            SQLiteDatabase db = this.getReadableDatabase();
            Cursor cursor = db.rawQuery(selectQuery, null);

            // looping through all rows and adding to list
            if (cursor.moveToFirst()) {
                do {
                    labels.add(cursor.getString(1));
                } while (cursor.moveToNext());
            }

            // closing connection
            cursor.close();
            db.close();

            // returning lables
            return labels;
        }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

        // Drop old version table
        db.execSQL("Drop table " + TABLEStudent);
        db.execSQL("Drop table " + TABLEResponse);
        db.execSQL("Drop table " + TableQuestionMaster);
        db.execSQL("Drop table " + TableTemplateMaster);
        db.execSQL("Drop table " + TableQuestionOption);
        db.execSQL("Drop table " + TableClassMaster);
        // Create New Version table
        onCreate(db);
    }




}

Aucun commentaire:

Enregistrer un commentaire