askIT

Create table (Android SQLite database)

Use the class DBadapter to create tables. In this class you need to write the database name, table name and database version.

You need to also creata a string that contains the table layout.

There are two diffrent methods that can create and delete tables.

Example of DBadapter.class:

package com.nettport.stram;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import android.widget.Toast;

/**
* Created by bruker on 29.12.2015.
*/

public class DBAdapter {

        // Database and table
        private static final String databaseName                = "StramDB.db";
        private static final String databaseTableDietPlan = "diet_plan";
        private static final int databaseVersion                = 1;


        // Create tables Diet Plan
        private static final String databaseCreateTableDietPlan =
                "CREATE TABLE IF NOT EXISTS " + databaseTableDietPlan + " " +
                        "( dp_id INTEGER PRIMARY KEY AUTOINCREMENT, " +
                        " dp_title VARCHAR," +
                        " dp_date VARCHAR," +
                        " dp_total_with_workout_energy INT," +
                        " dp_total_with_workout_proteins INT," +
                        " dp_total_with_workout_carbs INT," +
                        " dp_total_with_workout_fat INT," +
                        " dp_total_with_workout_sum INT," +
                        " dp_total_with_workout_percentage_proteins INT," +
                        " dp_total_with_workout_percentage_carbs INT," +
                        " dp_total_with_workout_percentage_fat INT," +
                        " dp_total_without_workout_energy INT," +
                        " dp_total_without_workout_proteins INT," +
                        " dp_total_without_workout_carbs INT," +
                        " dp_total_without_workout_fat INT," +
                        " dp_total_without_workout_sum INT," +
                        " dp_total_without_workout_percentage_proteins INT," +
                        " dp_total_without_workout_percentage_carbs INT," +
                        " dp_total_without_workout_percentage_fat INT);";



        // Private database variables
        private final Context context;
        private DatabaseHelper DBHelper;
        private SQLiteDatabase db;


        // Class DBAdapter
        public DBAdapter(Context ctx) {
        this.context = ctx;
        DBHelper = new DatabaseHelper(context);
        } // end public DBAdapter

        // Start DBAdapter
        private static class DatabaseHelper extends SQLiteOpenHelper {
                DatabaseHelper(Context context) {
                        super(context, databaseName, null, databaseVersion);
                }

                @Override
                public void onCreate(SQLiteDatabase db)
                {
                        try {
                                db.execSQL(databaseCreateTableDietPlan);
                        } catch (SQLException e) {
                                e.printStackTrace();
                        }
                } // end public void onCreate

                @Override
                public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {


                        db.execSQL("DROP TABLE IF EXISTS " + databaseTableDietPlan);
                        onCreate(db);

                } // end public void onUpgrade
        } // end private static class DatabaseHelper extends SQLiteOpenHelper

        //---opens the database---
        public DBAdapter open() throws SQLException {
                db = DBHelper.getWritableDatabase();
                return this;
        }

        //---closes the database---
        public void close() {
                DBHelper.close();
        }
}

Add comment

avatar_blank_60.png

Comment:

Name:

Email:

Comments

No comments yet. You can be the first one to comment. Just write your comment in the form and click on the submit button.