2011-11-06 89 views
0

我是android平臺的新手。我正在開發一個項目,在該項目中,我必須在一項活動中製作具有兩個日曆界面的共享日曆。用戶可以保存事件的詳細信息,事件標題,事件時間,餘額,描述,等等。我已經完成了安卓網站上的記事本教程。我以相同的模式提出了我的申請。但我無法找到錯誤。每次我運行我的代碼SQLite異常與錯誤代碼1命中。我花了20多個小時,但找不到任何解決方案。我試圖調試它,但沒有用處。請幫我解決這個錯誤。這將是一個很大的幫助。謝謝大家。SQLiteException錯誤代碼= 1'columnName'附近的語法錯誤

這其中m試圖插入到數據庫

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

`

+0

其中,在創建一個字符串? –

+0

你在說什麼創造?數據庫幫助器onCreate已創建 –

+0

...在哪裏創建數據庫。看起來像rushman有它,但。關鍵是,你不能只說「我得到這個錯誤」,而不提供任何有關你何時/如何/如何得到錯誤的信息。 –

回答

1

這是問題的線?

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

這裏,DATABASE_TABLE是即您發出CREATE TABLE DATABASE_TABLE

後來你指的DATABASE_TABLE變量即 「事件」

+0

感謝rushman對你的建議我已經將「DATABASE_TABLE」更改爲DATABASE_TABLE,但仍然有一個執行 –

+1

好的。另一個建議。是否有名爲「from」和「to」的列?這些是SQL保留字。 – rushman

+1

是rushman這些是我的專欄名稱 –

相關問題