2013-09-10 7 views
-1

У меня недавно был код моей базы данных, все сделано и работало, но затем я добавил комментарии, как это было необходимо. Через несколько дней я снова попытался запустить свой код, но теперь база данных сработала, но работала отлично, но я понятия не имею, что не так.Ошибка синтаксиса базы данных Android

Ошибка журнала кошка

09-11 01: 36: 55,414: E/AndroidRuntime (20188): Вызванный: android.database.sqlite.SQLiteException: рядом с "комментарии": синтаксическая ошибка (код 1):, при компиляции: ALTER TABLE добавил комментарии и исправить код;

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

/** 
* The database adapter which controls the uses of the database 
* Uses various methods in the operation of the database 
* @author Michael 
* 
*/ 
public class DatabaseAdapter { 

    //Initialise the static fields 
    static final String KEY_ROWID = "_id"; 
    static final String KEY_NAME = "name"; 
    static final String KEY_EMAIL = "email"; 
    static final String KEY_LNAME = "lname"; 
    static final String KEY_GENDER = "gender"; 
    static final String KEY_DATE = "date"; 
    static final String KEY_HOBBIES = "hobbies"; 
    static final String TAG = "DBAdapter"; 
    static final String DATABASE_NAME = "MyDB"; 
    static final String DATABASE_TABLE = "famous people"; 

    //The database version, MUST INCREMENT AFTER CHANGE 
    static final int DATABASE_VERSION = 16; 

    static final String DATABASE_CREATE = 
     "create table contacts (_id integer primary key autoincrement, " 
     + "name text not null, lname text not null, gender not null, date not null, hobbies not null);"; 

    final Context context; 

    //Create the database helper 
    DatabaseHelper DBHelper; 

    //Create the SQLiteDatabase with db 
    SQLiteDatabase db; 

     /** 
     * The constructor for the databaseadapter 
     * @param ctx 
     */ 
    public DatabaseAdapter(Context ctx) 
    { 
     this.context = ctx; 
     DBHelper = new DatabaseHelper(context); 
    } 

    /** 
    * The database helper which extends the SQLiteOpenHelper 
    */ 
    private static class DatabaseHelper extends SQLiteOpenHelper 
    { 
     //The database helper uses context 
     DatabaseHelper(Context context) 
     { 
      super(context, DATABASE_NAME, null, DATABASE_VERSION); 
     } 

     @Override 
     /** 
     * On creation of the database, attempt to create the database 
     */ 
     public void onCreate(SQLiteDatabase db) 
     { 
      try { 
       db.execSQL(DATABASE_CREATE); 
      } catch (SQLException e) { 
       e.printStackTrace(); 
      } 
     } 

     @Override 
     /** 
     * When the database version changes 
     */ 
     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
     { 
      //Use an if loop to check the version 
      if (oldVersion < newVersion){ 

       //Input what has been changed 
       final String ALTER_TBL = "ALTER TABLE added comments and fix code;"; 

       //Call the execSQL with the ALTER_TBL string 
       db.execSQL(ALTER_TBL); 

      } else { 

       //Log an error that the update did not work 
       Log.w(TAG, "Upgrading database from version " + oldVersion + " to " 
         + newVersion + ", which will destroy all old data"); 

       //Execute the execSQL with string 
       db.execSQL("DROP TABLE IF EXISTS contacts"); 

       //Create a new database 
       onCreate(db); 
      } 
     } 
    } 

    /** 
    * Open the database using Database helper 
    * @return 
    * @throws SQLException 
    */ 
    public DatabaseAdapter open() throws SQLException 
    { 
     //Use the writable database method 
     db = DBHelper.getWritableDatabase(); 
     return this; 
    } 

    /** 
    * Close the database using Database helper 
    */ 
    public void close() 
    { 
     //Close the database 
     DBHelper.close(); 
    } 

    /** 
    * Insert a contact into the database 
    * @param name, lname, gender, date, hobbies 
    * @return 
    */ 
    public long insertContact(String name, String lname, String gender, String date, String hobbies) 
    { 
     //Create new initial Values 
     ContentValues initialValues = new ContentValues(); 

     //Use the put method to put in the first name 
     initialValues.put(KEY_NAME, name); 

     //Use the put method to put in the last name 
     initialValues.put(KEY_LNAME, lname); 

     //Use the put method to put in the gender 
     initialValues.put(KEY_GENDER, gender); 

     //Use the put method to put in the date of birth 
     initialValues.put(KEY_DATE, date); 

     //Use the put method to put in the hobbies 
     initialValues.put(KEY_HOBBIES, hobbies); 

     //insert the initial values into the database table 
     return db.insert(DATABASE_TABLE, null, initialValues); 
    } 

    /** 
    * Delete a contact 
    * @param rowId 
    * @return 
    */ 
    public boolean deleteContact(long rowId) 
    { 
     //Delete the item at rowId 
     return db.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0; 
    } 

    /** 
    * Get all the contacts in the database 
    * @return 
    */ 
    public Cursor getAllContacts() 
    { 
     return db.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_NAME, 
       KEY_LNAME, KEY_GENDER, KEY_DATE, KEY_HOBBIES}, null, null, null, null, null); 
    } 

    /** 
    * Shows the database item at particular rowId 
    * @param rowId 
    * @return 
    * @throws SQLException 
    */ 
    public Cursor getContact(long rowId) throws SQLException 
    { 
     Cursor mCursor = 
       db.query(true, DATABASE_TABLE, new String[] {KEY_ROWID, 
       KEY_NAME, KEY_LNAME, KEY_GENDER, KEY_DATE, KEY_HOBBIES}, KEY_ROWID + "=" + rowId, null, 
       null, null, null, null); 
     if (mCursor != null) { 
      mCursor.moveToFirst(); 
     } 
     return mCursor; 
    } 

    /** 
    * Update the contact with new values 
    * @param rowId, name, lname, gender, date, hobbies 
    * @return 
    */ 
    public boolean updateContact(long rowId, String name, String lname, String gender, String date, String hobbies) 
    { 
     //Create new content values set as args 
     ContentValues args = new ContentValues(); 

     //Put in the new name 
     args.put(KEY_NAME, name); 

     //Put in the new last name 
     args.put(KEY_LNAME, lname); 

     //Put in the new gender 
     args.put(KEY_GENDER, gender); 

     //Put in the new date 
     args.put(KEY_DATE, date); 

     //Put in the new hobby 
     args.put(KEY_HOBBIES, hobbies); 

     //Call the update method and udate the database 
     return db.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) > 0; 
    } 

} 

ответ

2

Ну, утверждение выполняется это:

final String ALTER_TBL = "ALTER TABLE added comments and fix code;"; 

Смотрите функцию onUpgrade() в вашем примере кода.

Это, безусловно, недействительный синтаксис SQL, следовательно, сбой. Чтобы исправить это, вам нужно будет указать действительный оператор таблицы Alter.

+0

Это правильно: «ALTER TABLE» + DATABASE_TABLE + «ДОБАВИТЬ КОЛОНКУ» + KEY_HOBBIES + ";"; – user2663646

+0

@ user2663646 Да, за исключением ";" часть. Я не уверен, что он что-то делает. Лучше удалите его. Интересно, как добавить несколько столбцов. –

Смежные вопросы