2015-04-02 2 views
0

MainActivity.classCREER: erreur de syntaxe:, lors de la compilation: DROP TABLE IF EXISTS

@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 
    } 

    ..... 
    .. 

Le code ci-dessus crée une base de données, table et insère les données. Mais, si je change le schéma comme changer lastlogin à lastlogiin,

private static final String USER_LOGIN_TIME = "lastlogiin"; 

Il ne prend pas effet. Puis, j'ai changé la version de base de données à

private static final int DATABASE_VERSION = 2; 

Mais, il jette l'erreur.

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) 

Répondre

4

@ user3289108 remplacer la ligne suivante dans onUpgrade() méthode

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

Pour

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

Merci ... ça marche :) – user3289108