-1

我正在尝试创建一个 Android 应用程序,我需要一个数据库。我用 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);
}
}

谁能告诉我问题出在哪里?

4

1 回答 1

3

在保留字exists和表名之间添加空格,如下所示。

public static final String CREATE_TABLE_DAY = "create table if not exists " + TABLE_DAY + " (" + ID_DAY + " integer primary key autoincrement, " + DAY +" integer);";
于 2013-04-17T12:06:26.483 回答