Quantcast
Channel: Create SQLite database + table and save them for future use - Stack Overflow
Viewing all articles
Browse latest Browse all 2

Create SQLite database + table and save them for future use

$
0
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;@Overridepublic void onCreate(SQLiteDatabase db) {    db.execSQL(SQL_CREATE_ENTRIES);}@Overridepublic 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);}

}


Viewing all articles
Browse latest Browse all 2

Latest Images

Trending Articles





Latest Images