Skip to content Skip to sidebar Skip to footer

How To Create A Two Table In Single Database In Android Application?

I have Created a database in Sqllite Android Application and I tried to add two tables in my Database, but I have problem to create that Database. First Table only Created. Can any

Solution 1:

use below two classpackage Your 'packagename';


import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;


publicclassDBHelperextendsSQLiteOpenHelper {
    privatestaticfinalStringDATABASE_NAME="BistroDB";

    privatestaticfinalintDATABASE_VERSION=1;

    // Database creation sql statementpublicstaticfinal String Table1= "create table table1name ("Your cloumns");";


    publicstaticfinalStringTable2="create table table2name ("Your cloumns");";




    publicDBHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    // Method is called during creation of the database@OverridepublicvoidonCreate(SQLiteDatabase database) {
        database.execSQL(table1);
       database.execSQL(table2);

    }

    // Method is called during an upgrade of the database, e.g. if you increase// the database version@OverridepublicvoidonUpgrade(SQLiteDatabase database, int oldVersion,
            int newVersion) {
        Log.w(DBHelper.class.getName(),
                "Upgrading database from version " + oldVersion + " to "
                        + newVersion + ", which will destroy all old data");
        database.execSQL("DROP TABLE IF EXISTS table1");
        database.execSQL("DROP TABLE IF EXISTS table2");

        onCreate(database);
    }


        publicbooleandeleteDatabase(Context context) {
            return context.deleteDatabase(DATABASE_NAME);
        }

}

Use below class to insert values into table

  package 'Your package name';



    import android.content.ContentValues;
    import android.content.Context;
    import android.database.Cursor;
    import android.database.SQLException;
    import android.database.sqlite.SQLiteDatabase;

    publicclassDataBaseAdapter {

        // Database fieldsprivateContext context;
        privateSQLiteDatabase database;
        privateDBHelper dbHelper;

        publicDataBaseAdapter(Context context) {
            this.context = context;
        }

        publicDataBaseAdapteropen() throws SQLException {
            dbHelper = newDBHelper(context);
            database = dbHelper.getWritableDatabase();
            returnthis;
        }

        publicvoidclose() {
            dbHelper.close();
        }


        publicCursorfetchAllTAble1data() {
            return database.query("MenuData", newString[] { "id", "Title",
                    "Image", "Description" }, null, null, null, null, null);
        }

        publicCursorfetchAllTable2data() {
            return database.query("RestaurantsData", newString[] {
                    "restaurant_id", "name", "phone", "email", "open_days",
                    "timing", "website", "loc_name", "street", "city", "longitude",
                    "latitude", "zip" }, null, null, null, null, null);
        }

        publicvoiddeleteTable(String tablename){
            database.execSQL("drop table if exists "+tablename+';');
        }
        publicvoidcreateIndividualTable(String query){
            database.execSQL(query);
        }


        publicvoidInsertTable1Data(TAble1 review) {
            ContentValues values = newContentValues();
            values.put("Name", review.Name);
            values.put("Email", review.Email);
            values.put("Comment", review.Comment);
            values.put("Rating", review.Rating);

            database.insert("ReviewsData", null, values);

        }

        publicvoidInsertTable2Data(TAble2 photos) {
            ContentValues values = newContentValues();
            values.put("photo", photos.Photos);

            database.insert("PhotosData", null, values);

        }



        publicContentValuescreateContentValues(String category, String summary,
                String description) {
            ContentValues values = newContentValues();

            return values;
        }
    }

Solution 2:

Try removing the "," at the end of DATABASE_CREATECUS

Post a Comment for "How To Create A Two Table In Single Database In Android Application?"