在将数据插入SQLite DB时获取异常

时间:2013-02-16 13:32:45

标签: android sqlite exception

我正在尝试在SQLite中保存数据但是获得异常,我检查了所有内容但仍然收到错误 这是我的logcat:

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 个答案:

答案 0 :(得分:1)

我认为database.execSQL("DROP TABLE IF EXISTS " + DATABASE_BABY_CREATE);应该是

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

另一张桌也一样。但是,您应该尝试将问题减少到基本部分,而不是尽可能多地发布源代码。另外,考虑到Notes.java中引发了异常,添加该文件的相关部分会有所帮助。