2013-04-17 141 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);";