2013-02-16 211 views
0

我試圖在SQLite中保存數據,但得到異常,我檢查了一切,但仍然出現錯誤。
這裏是我的logcat:在將數據插入到SQLite數據庫時發生異常

03-01 07:50:51.240: E/AndroidRuntime(3984): FATAL EXCEPTION: main 
03-01 07:50:51.240: E/AndroidRuntime(3984): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.appstage.babyimmunisation/com.appstage.babyimmunisation.Notes}: android.database.sqlite.SQLiteException: near "create": syntax error: , while compiling: DROP TABLE IF EXISTS create table IF NOT EXISTS babydetails(notes_id integer primary key autoincrement, babyname text not null, babyIMAGE text not null, babygender text not null, babydob text not null, babybloodgroup text not null); 

03-01 07:50:51.240: E/AndroidRuntime(3984): Caused by: android.database.sqlite.SQLiteException: near "create": syntax error: , while compiling: DROP TABLE IF EXISTS create table IF NOT EXISTS babydetails(notes_id integer primary key autoincrement, babyname text not null, babyIMAGE text not null, babygender text not null, babydob text not null, babybloodgroup text not null); 
03-01 07:50:51.240: E/AndroidRuntime(3984):  at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method) 

03-01 07:50:51.240: E/AndroidRuntime(3984):  at com.appstage.babyimmunisation.Notes.onCreate(Notes.java:31) 

這裏是我的代碼:
AddNotes.java

DBHelper db; 
GetSet gs = new GetSet(); 

btnAddNotes.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 
      // TODO Auto-generated method stub 
      db=new DBHelper(getBaseContext()); 
      db.getWritableDatabase(); 
      gs.setNotes(notes); 
      db.addNotes(gs); 
      //db.close(); 

     } 
    }); 

DBHelper.java

private static final String DATABASE_NOTES_CREATE = "create table IF NOT EXISTS " 
     + TABLE_NOTES + "(" + COLUMN_NOTES_ID 
     + " integer primary key autoincrement, " 
     + COLUMN_NOTES + " text not null);"; 

private static final String DATABASE_BABY_CREATE = "create table IF NOT EXISTS " 
     + TABLE_BABYDETAILS + "(" + COLUMN_BABYID 
     + " integer primary key autoincrement, " 
     + COLUMN_BABYNAME + " text not null, " 
     + COLUMN_BABYIMAGE + " text not null, " 
     + COLUMN_BABYGENDER + " text not null, " 
     + COLUMN_BABYDOB + " text not null, " 
     + COLUMN_BABYBLOODGROUP + " text not null);"; 

public DBHelper open() throws SQLException 
{ 
    db = this.getWritableDatabase(); 
    return this; 
} 

public DBHelper(Context context) 
{ 
    super(context, DATABASE_NAME, null, DATABASE_VERSION);  
} 


@Override 
public void onCreate(SQLiteDatabase database) 
{ 
    database.execSQL(DATABASE_BABY_CREATE); 
    database.execSQL(DATABASE_NOTES_CREATE);  
} 

@Override 
public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) 
{ 

    Log.w(DBHelper.class.getName(), "Upgrading database from version " 
      + oldVersion + " to " + newVersion 
      + ", which will destroy all old data"); 
    //database.execSQL("DROP TABLE IF EXISTS " + DATABASE_NOTES_CREATE); 
    database.execSQL("DROP TABLE IF EXISTS " + DATABASE_BABY_CREATE); 
    database.execSQL("DROP TABLE IF EXISTS " + DATABASE_NOTES_CREATE); 
    onCreate(database); 
} 

//getting all notes details 
public List<GetSet>getAllNotes() 
{ 
    List<GetSet>LocwiseProfileList=new ArrayList<GetSet>(); 
    String selectQuery= "SELECT * FROM " + TABLE_NOTES; 
     db = this.getWritableDatabase(); 
     Cursor cursor = db.rawQuery(selectQuery, null); 

     // looping through all rows and adding to list 
     if (cursor.moveToFirst()) 
     { 
      do { 
       GetSet owq= new GetSet(); 
       owq.setNotesId(cursor.getInt(0)); 
       owq.setNotes(cursor.getString(1)); 
       LocwiseProfileList.add(owq); 
       } 
      while(cursor.moveToNext()); 
      db.close(); 
     } 
     return LocwiseProfileList;  
} 
public void addNotes(GetSet gs) 
    { 
     try 
     { 
      SQLiteDatabase db= this.getWritableDatabase(); 
      ContentValues values = new ContentValues(); 
      values.put(COLUMN_NOTES, gs.getNotes());       
      db.insert(TABLE_NOTES, null, values); 
      db.close(); 
     } 
     catch(Exception e) 
     { 
      e.printStackTrace(); 
     } 


    } 

public void addBabyDetails(GetSet gs) { 
    // TODO Auto-generated method stub 
    try 
    { 
     SQLiteDatabase db= this.getWritableDatabase(); 
     ContentValues values = new ContentValues(); 
     values.put(COLUMN_BABYNAME, gs.getAddBabyName()); 
     values.put(COLUMN_BABYDOB, gs.getAddBabyDOB()); 
     values.put(COLUMN_BABYGENDER, gs.getAddBabyGender()); 
     values.put(COLUMN_BABYBLOODGROUP, gs.getAddBabyBloodGroup()); 
     values.put(COLUMN_BABYIMAGE, gs.getImagePath()); 
     db.insert(TABLE_BABYDETAILS, null, values); 
     db.close(); 
    } 
    catch(Exception e) 
    { 
     e.printStackTrace(); 
    } 


} 

請給我任何參考或暗示關於我的錯誤或我錯了?

回答

1

我覺得database.execSQL("DROP TABLE IF EXISTS " + DATABASE_BABY_CREATE);而應是

database.execSQL("DROP TABLE IF EXISTS " + TABLE_BABYDETAILS); 

與同爲其他表。但是,您應該嘗試將您的問題減少到基本部分,而不是儘可能多地發佈源代碼。另外,考慮到一個例外是Notes.java拋出,加入(相關部分),該文件將是有益的。