How to create a two table in single Database in Android Application?

前端 未结 2 1039
说谎
说谎 2021-01-03 06:40

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.

相关标签:
2条回答
  • 2021-01-03 07:15

    Try removing the "," at the end of DATABASE_CREATECUS

    0 讨论(0)
  • 2021-01-03 07:20
    use below two class
    
    package Your 'packagename';
    
    
    import android.content.Context;
    import android.database.sqlite.SQLiteDatabase;
    import android.database.sqlite.SQLiteOpenHelper;
    import android.util.Log;
    
    
    public class DBHelper extends SQLiteOpenHelper {
        private static final String DATABASE_NAME = "BistroDB";
    
        private static final int DATABASE_VERSION =1;
    
        // Database creation sql statement
        public static final String Table1= "create table table1name ("Your cloumns");";
    
    
        public static final String Table2 = "create table table2name ("Your cloumns");";
    
    
    
    
        public DBHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }
    
        // Method is called during creation of the database
        @Override
        public void onCreate(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
        @Override
        public void onUpgrade(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);
        }
    
    
            public boolean deleteDatabase(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;
    
        public class DataBaseAdapter {
    
            // Database fields
    
            private Context context;
            private SQLiteDatabase database;
            private DBHelper dbHelper;
    
            public DataBaseAdapter(Context context) {
                this.context = context;
            }
    
            public DataBaseAdapter open() throws SQLException {
                dbHelper = new DBHelper(context);
                database = dbHelper.getWritableDatabase();
                return this;
            }
    
            public void close() {
                dbHelper.close();
            }
    
    
            public Cursor fetchAllTAble1data() {
                return database.query("MenuData", new String[] { "id", "Title",
                        "Image", "Description" }, null, null, null, null, null);
            }
    
            public Cursor fetchAllTable2data() {
                return database.query("RestaurantsData", new String[] {
                        "restaurant_id", "name", "phone", "email", "open_days",
                        "timing", "website", "loc_name", "street", "city", "longitude",
                        "latitude", "zip" }, null, null, null, null, null);
            }
    
            public void deleteTable(String tablename){
                database.execSQL("drop table if exists "+tablename+';');
            }
            public void createIndividualTable(String query){
                database.execSQL(query);
            }
    
    
            public void InsertTable1Data(TAble1 review) {
                ContentValues values = new ContentValues();
                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);
    
            }
    
            public void InsertTable2Data(TAble2 photos) {
                ContentValues values = new ContentValues();
                values.put("photo", photos.Photos);
    
                database.insert("PhotosData", null, values);
    
            }
    
    
    
            public ContentValues createContentValues(String category, String summary,
                    String description) {
                ContentValues values = new ContentValues();
    
                return values;
            }
        }
    
    0 讨论(0)
提交回复
热议问题