2013-04-17 140 views
-1

我想創建一個Android應用程序,我需要一個數據庫。我創造我的SQLite數據庫,但是當我運行我的應用程序收到此錯誤:SQLite數據庫錯誤

java.lang.RuntimeException: Unable to start activity ComponentInfo 
{com.example.budgetmanagerapp/com.example.budgetmanagerapp.Activity.Home}: 
android.database.sqlite.SQLiteException: near "existsday": syntax error (code 1): , while 
compiling: create table if not existsday(idDay integer primary key autoincrement, day integer); 

這裏是我的代碼:

public class DatabaseHelper extends SQLiteOpenHelper{ 
public static final String TABLE_DAY = "day"; 
public static final String ID_DAY = "idDay"; 
public static final String DAY = "day"; 

public static final String CREATE_TABLE_DAY = "create table if not exists" + TABLE_DAY + "(" 
     + ID_DAY + " integer primary key autoincrement, " + DAY +" integer);"; 

private static final String DATABASE_NAME ="budgetManager.db"; 
private static final int version = 1; 

public DatabaseHelper(Context context) { 
    super(context, DATABASE_NAME, null, version); 
    } 

@Override 
public void onCreate(SQLiteDatabase db) { 
    db.execSQL(CREATE_TABLE_DAY); 
} 

@Override 
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_PERSON); 
    onCreate(db); 
} 

} 

public class Home extends ListActivity { 

protected SQLiteDatabase db; 
protected Cursor cursor; 
protected ListAdapter adapter; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.home); 

    db = (new DatabaseHelper(this)).getWritableDatabase(); 

    viewData(); 
} 

@SuppressWarnings("deprecation") 
public void viewData(){ 
    cursor = db.rawQuery("SELECT day FROM day", 
         null); 

    adapter = new SimpleCursorAdapter(this, R.layout.home, 
            cursor, new String[]{"day"}, 
            new int[] {R.id.ceva}); 
    setListAdapter(adapter); 
} 
} 

有人能告訴我問題出在哪裏?

+0

增加空間前:'「創建表,如果不存在」' 後:'「創建表,如果不存在」' –

回答

3

在保留字exists和表名之間添加空格,如下所示。在聲明中

public static final String CREATE_TABLE_DAY = "create table if not exists " + TABLE_DAY + " (" + ID_DAY + " integer primary key autoincrement, " + DAY +" integer);";