0

I followed a tutorial to create an SQLite database and create tables in it for an application. But all of this happens at runtime. Now I'm looking for a way to create the database and table so I can re-use them in the future. So the same table will be updated with data that is inserted in the app. The database and table will only be created once.

This the code I'm using to create the database and tabel at runtime:

SQLiteHelperCourse courseDbHelper = new SQLiteHelperCourse(this);
    SQLiteDatabase dbw = courseDbHelper.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(CourseEntry.COLUMN_NAME_COURSE_ID, "1");
    values.put(CourseEntry.COLUMN_NAME_COURSE_NAME, "Android");
    values.put(CourseEntry.COLUMN_NAME_CREDITS, "3");
    values.put(CourseEntry.COLUMN_NAME_SEMESTER, "1st");
    values.put(CourseEntry.COLUMN_NAME_YEAR, "3rd");
    values.put(CourseEntry.COLUMN_NAME_COURSE_ID, "2");
    values.put(CourseEntry.COLUMN_NAME_COURSE_NAME, "SBP");
    values.put(CourseEntry.COLUMN_NAME_CREDITS, "3");
    values.put(CourseEntry.COLUMN_NAME_SEMESTER, "1st");
    values.put(CourseEntry.COLUMN_NAME_YEAR, "3rd");
    values.put(CourseEntry.COLUMN_NAME_COURSE_ID, "3");
    values.put(CourseEntry.COLUMN_NAME_COURSE_NAME, "IT Infrastructure");
    values.put(CourseEntry.COLUMN_NAME_CREDITS, "3");
    values.put(CourseEntry.COLUMN_NAME_SEMESTER, "1st");
    values.put(CourseEntry.COLUMN_NAME_YEAR, "3rd");
    dbw.insert(CourseEntry.TABLE_NAME, CourseEntry.COLUMN_NAME_NULLABLE, values);

    SQLiteDatabase dbr = courseDbHelper.getReadableDatabase();
    String[] projection = { CourseEntry.COLUMN_NAME_COURSE_NAME};
    final ArrayList<String> list = new ArrayList<String>();

    Cursor curs = dbr.query(CourseEntry.TABLE_NAME, 
            projection, 
            null, 
            null, 
            null, 
            null, 
            null);
    curs.moveToFirst();

    while(curs.moveToNext())
    {
        list.add(curs.getString(curs.getColumnIndexOrThrow(CourseEntry.COLUMN_NAME_COURSE_NAME)));
    }

EDIT: The code I provided is being called in the onCreate event of the activity.

Here is my SQLiteHelper class code:

public class SQLiteHelperCourse extends SQLiteOpenHelper {

public static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "AndroidApplication.db";

public SQLiteHelperCourse(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

private static final String TEXT_TYPE = " TEXT";
private static final String COMMA_SEP = ", ";
private static final String SQL_CREATE_ENTRIES = 
        "CREATE TABLE " + CourseEntry.TABLE_NAME + " (" + CourseEntry._ID + " INTEGER PRIMARY KEY, "
                                                        + CourseEntry.COLUMN_NAME_COURSE_ID + TEXT_TYPE + COMMA_SEP
                                                        + CourseEntry.COLUMN_NAME_COURSE_NAME + TEXT_TYPE + COMMA_SEP
                                                        + CourseEntry.COLUMN_NAME_YEAR + TEXT_TYPE + COMMA_SEP
                                                        + CourseEntry.COLUMN_NAME_SEMESTER + TEXT_TYPE + COMMA_SEP
                                                        + CourseEntry.COLUMN_NAME_CREDITS + TEXT_TYPE + ")";

private static final String SQL_DELETE_ENTRIES = "DROP TABLE IF EXISTS " + CourseEntry.TABLE_NAME;

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL(SQL_CREATE_ENTRIES);

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL(SQL_DELETE_ENTRIES);
    onCreate(db);
}

public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    onUpgrade(db, oldVersion, newVersion);
}

}

4
  • Have you looked into XML parsing? If you don't want to set up a db server it may be a possible alternative. Here is a simple XML Parsing Tutorial for web development. While it is for web development, the concepts covered in it apply to whatever kind of app you want to use it for. I recommend using the xmlreader method over linqtoxml(both covered in tutorial) if you're using it for a makeshift database. Commented Dec 17, 2014 at 14:23
  • 1
    What is the question, exactly? Commented Dec 17, 2014 at 14:24
  • The question is: How can I create a table in the database only once, so it's not recreated every time the app is launched. Commented Dec 17, 2014 at 14:29
  • 1
    It's already so. You create the table once, only if it's not existing (but I don't see the table creation in the code you posted), and then reuse it each time in your app, with the data you put in. Commented Dec 17, 2014 at 14:32

1 Answer 1

1

Assuming you want to insert your three courses to the database only once when the database is created:

  • Move the insert code to database helper onCreate(). It gets called once when the database file didn't exist to start with. Use the SQLiteDatabase supplied as an argument there instead of calling getWritableDatabase() recursively.

  • Insert three times. ContentValues put() overwrites any value with the same key so essentially you're only really inserting your third course.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.