3

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.

1 Answer 1

1

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)");
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. That makes sense now. Will try tomorrow but it will definitely work. Cheers
Ended up having to use this. db.execSQL("Insert into " + TABLE_CAPACITY + " (Capacity) values (10000),(20000),(30000)"); Your code pointed me in the right direction. Cheers

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.