SQLite - creating the database



To add a SQLite database to your app you need to:

1. Create a class that extends the SQLiteOpenHelper superclass

public class SQLOpenHelper extends SQLiteOpenHelper {
    public SQLOpenHelper(Context context) {
        super(context, [your database], null, [your version number]);

    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL([SQL command to create table]);

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS "+[String with the name of the table]);
        onCreate(db);
    }

}

where:

[your database]
is a String which ends ind .db

[your version number] is an int;

[String with the name of the table]
self explanatory

[SQL command to create table]
is a String in this format:
"CREATE TABLE [NAME OF TABLE] ( _ID INTEGER PRIMARY KEY AUTOINCREMENT, [name of column] [Type of data], [name of column] [Type of data] ... );"


No comments:

Post a Comment