Create table and add primary key and foreign key to it - Android Database

Android examples for Database:Primary Key

Description

Create table and add primary key and foreign key to it

Demo Code


//package com.java2s;

import android.database.sqlite.SQLiteDatabase;

import android.util.Log;

public class Main {
    private static final String LOG_TAG = "BoardDatabaseOpenHelper";

    private static void createSavedGameStateTable(SQLiteDatabase db) {
        Log.d(LOG_TAG, "Creating saved game state table");
        StringBuilder sql = new StringBuilder();

        sql.append("CREATE TABLE SavedGameState (");
        sql.append("Engine smallint,");
        sql.append("Id smallint,");
        sql.append("Row smallint,");
        sql.append("Column smallint,");
        sql.append("Chip smallint,  State smallint,");
        sql.append("PRIMARY KEY (Engine, Id, Row, Column),");
        sql.append("FOREIGN KEY (Engine) REFERENCES GameEngine(Id)");
        sql.append(");");

        db.execSQL(sql.toString());//w w  w  . jav  a  2 s.com
    }
}

Related Tutorials