简体   繁体   中英

Executing sql in android

I'm trying to create tables in sqlite android development. My problem is, only one table is beaing created but the other one is not.

Here's how I create them.

    @Override
    public void onCreate(SQLiteDatabase db) 
    {
        try
        {
            db.execSQL(DATABASE_CREATE);
            db.execSQL(DATABASE_CREATE2);
        }
        catch(SQLException e)
        {
            e.printStackTrace();
        }
    }

Variables:

private static final String DATABASE_CREATE =
    "create table if not exists profiles (_id integer primary key autoincrement, "
    + "firstname VARCHAR not null, age VARCHAR not null, " 
    + "heightft VARCHAR not null, heightin VARCHAR not null, weight VARCHAR not null, duration VARCHAR not null, bmi VARCHAR not null);";

private static final String DATABASE_CREATE2 =
        "create table if not exists routines (_id integer primary key autoincrement, "
        + "weightclass VARCHAR not null, musclegroup VARCHAR not null, " 
        + "exercise VARCHAR not null, numberofsets VARCHAR not null, numeberofrepitition VARCHAR not null, day VARCHAR not null);";

Any ideas on what the problem is? Thanks!

Update:

private static final String DATABASE_NAME = "fgtDB";
private static final String DATABASE_TABLE = "profiles";
private static final String DATABASE_TABLE2 = "routines";
private static final int DATABASE_VERSION = 2;

private static class DatabaseHelper extends SQLiteOpenHelper 
{
    DatabaseHelper(Context context) 
    {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) 
    {
        try
        {
            db.execSQL(DATABASE_CREATE);
                    db.execSQL(DATABASE_CREATE2);
        }
        catch(SQLException e)
        {
            e.printStackTrace();
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
    {
        Log.w(TAG, "Upgrading database from version " + oldVersion 
                + " to "
                + newVersion + ", which will destroy all old data");
        db.execSQL("DROP TABLE IF EXISTS profiles");
        onCreate(db);
    }
}    

It could be that you have first created one table, and added another table when the database is created.
If it is the case try to change the database version, because the onCreate(SQLiteDatabase db) method is only called once, if the version is the same.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM