0

MainActivity.class

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    DatabaseHandler db = new DatabaseHandler(this);

    // Inserting Users
    Log.d("Insert: ", "Inserting ..");
    db.addContact(new Contact("Ravi", "kumar", 1, getDateTime()));
}

 public String getDateTime() {
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
    Date date = new Date();
    return simpleDateFormat.format(date);
}

DatabaseHandler.class

public class DatabaseHandler extends SQLiteOpenHelper {


    Context context;
    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "dbname";
    private static final String TABLE_USERS = "users";

    private static final String USERS_ID = "id";
    private static final String USER_NAME = "name";
    private static final String USER_LAST_NAME = "lastname";
    private static final String USER_USER_LEVEL = "userlevel";
    private static final String USER_LOGIN_TIME = "lastlogin";


    String CREATE_USERS_TABLE = "CREATE TABLE " + TABLE_USERS + "("
            + USERS_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + USER_NAME + " TEXT,"
            + USER_LAST_NAME + " TEXT NULL," + USER_USER_LEVEL + " INTEGER," + USER_LOGIN_TIME + " TEXT" + ")";




    public DatabaseHandler(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        this.context = context;

        Toast.makeText(context,"Constructor is called", Toast.LENGTH_LONG).show();
        System.out.println("Sql query : " +CREATE_USERS_TABLE);

    }

    @Override
    public void onCreate(SQLiteDatabase db) {


        db.execSQL(CREATE_USERS_TABLE);

        Toast.makeText(context,"on Create is called", Toast.LENGTH_LONG).show();


    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int i, int i2) {

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

        Toast.makeText(context,"on Upgrade is called", Toast.LENGTH_LONG).show();



    }


    public void addContact(Contact contact) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(USER_NAME, contact.get_name()); // Contact Name
        values.put(USER_LAST_NAME, contact.get_lname());
        values.put(USER_USER_LEVEL, contact.get_level()); // Contact Phone Number
        values.put(USER_LOGIN_TIME,contact.get_lastlogin());
        //Inserting Row
        db.insert(TABLE_USERS, null, values);

        db.close(); // Closing database connection
    }

  .....
  ..

The above code creates a database, table and inserts the data. But, if i change the schema like changing lastlogin to lastlogiin,

private static final String USER_LOGIN_TIME = "lastlogiin";

It doesn't takes effect. Then, i changed the database version to

private static final int DATABASE_VERSION = 2;

But, it throws the error.

Logcat :

 at dalvik.system.NativeStart.main(Native Method)      Caused by:
android.database.sqlite.SQLiteException: near "CREATE": syntax error:
, while compiling: DROP TABLE IF EXISTS CREATE TABLE users(id INTEGER
PRIMARY KEY AUTOINCREMENT,name TEXT,lastname TEXT NULL,userlevel
INTEGER,lastlogiin TEXT)

1 Answer 1

4

@user3289108 replace following line in onUpgrade() method

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

To

db.execSQL("DROP TABLE IF EXISTS " + TABLE_USERS );
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.