-1

我最近完成了我的数据库代码并正常工作,但随后我根据需要添加了注释。几天后,我再次尝试运行我的代码,但数据库现在崩溃了,之前它运行良好,但我不知道出了什么问题。

日志猫错误是

09-11 01:36:55.414: E/AndroidRuntime(20188): Caused by: android.database.sqlite.SQLiteException: near "comments": syntax error (code 1): , while compile: 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;
    }

}
4

1 回答 1

2

好吧,正在执行的语句是:

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

请参阅代码示例中的 onUpgrade() 函数。

它肯定不是有效的 SQL 语法,因此失败了。您将需要提供有效的 Alter Table 语句来解决此问题。

于 2013-09-10T15:48:11.340 回答