mercredi 9 mars 2016

Inserting Static data into SQLlite DB from java code

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.

Aucun commentaire:

Enregistrer un commentaire