SQLiteException错误代码= 1'columnName'附近的语法错误

时间:2011-11-06 14:39:08

标签: android sqlite

我是android平台的新手。我正在开展一个项目,我必须在一个活动上制作一个带有两个日历界面的共享日历。用户可以使用事件标题,事件时间,余数,描述,来往等事件详细信息保存事件。我已经在android网站上给出了记事本教程。我使用相同的模式制作了应用程序。但我无法找到错误。每次我运行我的代码SQLite异常错误代码1命中。我已经花了20多个小时但却找不到任何解决方案。我也试过调试它但根本没用。请帮我解决这个错误。这将是一个很大的帮助。提前谢谢大家。

这是我试图插入数据库的地方

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

    public class MyListView extends ListActivity {

        int j=0;
        private EventsDbAdapter mDbHelper;
        private Cursor EventsCursor;

        static final String[] hours = new String[]{
            "00:00",
            "01:00",
            "02:00",
            "03:00",
            "04:00",
            "05:00",
            "06:00",
            "07:00",
            "08:00",
            "09:00",
            "10:00",
            "11:00",
            "12:00",
            "13:00",
            "14:00",
            "15:00",
            "16:00",
            "17:00",
            "18:00",
            "19:00",
            "20:00",
            "21:00",
            "22:00",
            "23:00"
        };
        private int pos=0;
        private int ACTIVITY_CREATE=0;
        private ArrayAdapter<String> ar;
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            ar = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, hours);
            setListAdapter(ar);
            getListView().setTextFilterEnabled(true);

            mDbHelper = new EventsDbAdapter(this);
            mDbHelper.open();

        }

        protected void onListItemClick(ListView l, View v, int position, long id) {
            super.onListItemClick(l, v, position, id);
            pos = position;

            String str = this.getListAdapter().getItem(position).toString();

            if(str.length()<6)
            {
                Intent myIntent = new Intent(MyListView.this, event.class);
                MyListView.this.startActivityForResult(myIntent,0);
            }
            else
            {
                Cursor c = EventsCursor;
                EventsCursor=mDbHelper.fetchAllNotes();
                startManagingCursor(EventsCursor);

                // Create an array to specify the fields we want to display in the list (only TITLE)
                String[] from = new String[]{EventsDbAdapter.KEY_TITLE};


                c.moveToPosition(position);

                Intent i = new Intent(this, event.class);

                i.putExtra(EventsDbAdapter.KEY_ROWID, id);

                i.putExtra(EventsDbAdapter.KEY_TITLE, c.getString(c.getColumnIndexOrThrow(EventsDbAdapter.KEY_TITLE)));

                i.putExtra(EventsDbAdapter.KEY_TO, c.getString(c.getColumnIndexOrThrow(EventsDbAdapter.KEY_TO)));

                i.putExtra(EventsDbAdapter.KEY_FROM, c.getString(c.getColumnIndexOrThrow(EventsDbAdapter.KEY_FROM)));

                i.putExtra(EventsDbAdapter.KEY_DESCRIPTION, c.getString(c.getColumnIndexOrThrow(EventsDbAdapter.KEY_DESCRIPTION)));

                startActivityForResult(i, 1);
            }
     }



        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
               super.onActivityResult(requestCode, resultCode, intent);

               Bundle extras = intent.getExtras();

                if(requestCode==0 && resultCode==-1)
                {
                        String title = extras.getString(EventsDbAdapter.KEY_TITLE);
                        String to = extras.getString(EventsDbAdapter.KEY_TO);
                        String from = extras.getString(EventsDbAdapter.KEY_FROM);
                        String description = extras.getString(EventsDbAdapter.KEY_DESCRIPTION);
                        mDbHelper.createEvent(title, to,from,description);
                        hours[pos]=hours[pos]+title;
                        ar.notifyDataSetChanged();

                }
                else if(requestCode==1)
                {
                    Long rowId = extras.getLong(EventsDbAdapter.KEY_ROWID);
                    if (rowId != null) {
                        String editTitle = extras.getString(EventsDbAdapter.KEY_TITLE);
                        String to = extras.getString(EventsDbAdapter.KEY_TO);
                        String from = extras.getString(EventsDbAdapter.KEY_FROM);
                        String description = extras.getString(EventsDbAdapter.KEY_DESCRIPTION);
                        mDbHelper.updateEvent(rowId, editTitle, to,from,description);

                        hours[pos]=hours[pos]+editTitle;
                        ar.notifyDataSetChanged();

                    }
                }
            }
    }

`

public class EventsDbAdapter 
{
    public static final String KEY_TITLE = "title";
    public static final String KEY_TO = "tochecking";
    public static final String KEY_FROM = "fromchecking";
    public static final String KEY_DESCRIPTION = "description";
        public static final String KEY_ROWID = "_id";

        private static final String TAG = "EventsDbAdapter";
        private DatabaseHelper mDbHelper;
        private SQLiteDatabase mDb;

/**
 * Database creation sql statement
 */
    private static final String DATABASE_CREATE =
            " create table " + " DATABASE_TABLE " + " ("
            + KEY_ROWID + " integer primary key autoincrement,      "
            + KEY_TITLE + " text not null, "
            + KEY_TO + " text not null, "
            + KEY_FROM + " text not null,"+KEY_DESCRIPTION+" text not null);";


       private static final String DATABASE_NAME = "data";
       private static final String DATABASE_TABLE = "events";
       private static final int DATABASE_VERSION = 2;

       private final Context mCtx;

       private static class DatabaseHelper extends SQLiteOpenHelper {

       DatabaseHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
       }
       @Override
       public void onCreate(SQLiteDatabase db) {

        System.out.print("testing");
        db.execSQL(DATABASE_CREATE);
       }

       @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 notes");
           onCreate(db);
       }
    }

    /**
     * Constructor - takes the context to allow the database to be
     * opened/created
     * 
     * @param ctx the Context within which to work
     */
      public EventsDbAdapter(Context ctx) {
        this.mCtx = ctx;
      }

    /**
     * Open the notes database. If it cannot be opened, try to create a new
     * instance of the database. If it cannot be created, throw an exception to
     * signal the failure
     * 
     * @return this (self reference, allowing this to be chained in an
     *         initialization call)
     * @throws SQLException if the database could be neither opened or created
     */
    public EventsDbAdapter open() throws SQLException {
        mDbHelper = new DatabaseHelper(mCtx);
        mDb =mDbHelper.getWritableDatabase();
        return this;
    }

    public void close() {
        mDbHelper.close();
    }


    /**
     * Create a new note using the title and body provided. If the note is
     * successfully created return the new rowId for that note, otherwise return
     * a -1 to indicate failure.
     * 
     * @param title the title of the note
     * @param body the body of the note
     * @return rowId or -1 if failed
     */
    public long createEvent(String title, String to , String from , String description) {
        ContentValues initialValues = new ContentValues();
        initialValues.put(KEY_TITLE, title);
        initialValues.put(KEY_TO, to);
        initialValues.put(KEY_FROM, from);
        initialValues.put(KEY_DESCRIPTION, description);

        return mDb.insert(DATABASE_TABLE, null, initialValues);


    }

    /**
     * Delete the note with the given rowId
     * 
     * @param rowId id of note to delete
     * @return true if deleted, false otherwise
     */
    public boolean deleteNote(long rowId) {

        return mDb.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
    }

    /**
     * Return a Cursor over the list of all notes in the database
     * 
     * @return Cursor over all notes
     */
    public Cursor fetchAllNotes() {

        return mDb.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_TITLE,KEY_TO,KEY_FROM,
                KEY_DESCRIPTION}, null, null, null, null, null);
    }

    /**
     * Return a Cursor positioned at the note that matches the given rowId
     * 
     * @param rowId id of note to retrieve
     * @return Cursor positioned to matching note, if found
     * @throws SQLException if note could not be found/retrieved
     */
    public Cursor fetchEvent(long rowId) throws SQLException {

        Cursor mCursor =

            mDb.query(true, DATABASE_TABLE, new String[] {KEY_ROWID, KEY_TITLE,KEY_TO,KEY_FROM,
                    KEY_DESCRIPTION}, KEY_ROWID + "=" + rowId, null,
                    null, null, null, null);
        if (mCursor != null) {
            mCursor.moveToFirst();
        }
        return mCursor;

    }

    /**
     * Update the note using the details provided. The note to be updated is
     * specified using the rowId, and it is altered to use the title and body
     * values passed in
     * 
     * @param rowId id of note to update
     * @param title value to set note title to
     * @param body value to set note body to
     * @return true if the note was successfully updated, false otherwise
     */
    public boolean updateEvent(long rowId, String title, String to ,String from,String description) {
        ContentValues args = new ContentValues();
        args.put(KEY_TITLE, title);
        args.put(KEY_TO, to);
        args.put(KEY_FROM, from);
        args.put(KEY_DESCRIPTION, description);

        return mDb.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) > 0;
    }
}

`

1 个答案:

答案 0 :(得分:1)

这是问题吗?

" create table " + " DATABASE_TABLE " + " ("

这里,DATABASE_TABLE是一个字符串,即你发出CREATE TABLE DATABASE_TABLE

稍后您将DATABASE_TABLE变量称为“events”