Reputation: 232
Im working on an android app at the moment and have created a database which has a few tables. I have successfully created the database and all works fine.
What i want to do now is to add content into the database. This content will be static and be used to populate pages with figures etc.
Im not sure of how to set the insert up what so ever any help on this would be great.
See my code below where i have created the database and tables.
package com.example.testerrquin.euro2016fanguide;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
/*** Created by rquin on 10/03/2016.*/
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "Euro2016.db";
public static final String TABLE_USERS = "Users_table";
public static final String TABLE_CAPACITY = "Capacity_table";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("Create table " + TABLE_USERS +" (UserID INTEGER PRIMARY KEY AUTOINCREMENT, Forename TEXT, Surname TEXT, Email TEXT, Password TEXT)");
db.execSQL("Create table " + TABLE_CAPACITY + " (CapacityID INTEGER PRIMARY KEY AUTOINCREMENT, Capacity INTEGER)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_USERS);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_CAPACITY);
onCreate(db);
}
}
Ideally i would like data to be inserted to the Capacity table like:
CapacityID Capacity
1 10000
2 40000
3 70000
Thanks.
Upvotes: 3
Views: 831
Reputation: 560
If you want to insert static data inside your database, and that data will not be modified at any moment, I think the best approach it's insert it in the onCreate method, after you create the tables. So your onCreate will look like:
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("Create table " + TABLE_USERS +" (UserID INTEGER PRIMARY KEY AUTOINCREMENT, Forename TEXT, Surname TEXT, Email TEXT, Password TEXT)");
db.execSQL("Create table " + TABLE_CAPACITY + " (CapacityID INTEGER PRIMARY KEY AUTOINCREMENT, Capacity INTEGER)");
db.execSQL("INSERT INTO " + TABLE_CAPACITY + " VALUES (10000, 40000, 70000)");
}
Upvotes: 1