引起:android.database.sqlite.SQLiteException:near" TABLEProject&#34 ;:语法错误(代码1):,同时编译:

时间:2015-01-23 07:39:18

标签: android sqlite

这是我的数据库帮助

public class DBHelper extends SQLiteOpenHelper {

    private static final int DATABASE_VERSION = 4;

    private static final String DATABASE_NAME = "organizer.db";

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

    @Override
    public void onCreate(SQLiteDatabase db){

        **String CREATE_TABLE_PROJECT = "CREATE TABLE" + Project.TABLE + "("
                + Project.KEY_ID + "INTEGER PRIMARY KEY AUTOINCREMENT,"
                + Project.KEY_title + "TEXT,"
                + Project.KEY_type + "TEXT,"
                + Project.KEY_priority + "INTEGER,"
                + Project.KEY_timeframe + "TEXT,"
                + Project.KEY_start + "TEXT,"
                + Project.KEY_end + "TEXT,"
                + Project.KEY_cost + "INTEGER,"
                + Project.KEY_status + "TEXT" +");";
        db.execSQL(CREATE_TABLE_PROJECT);**

    }

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

        db.execSQL("DROP TABLE IF EXIST" + Project.TABLE);

        onCreate(db);
    }

}

这是我对表格和字段的声明

public class Project  {

    public static final String TABLE = "Project";

    public static final String KEY_ID = "id";
    public static final String KEY_title = "title";
    public static final String KEY_type = "type";
    public static final String KEY_priority = "priority";
    public static final String KEY_timeframe = "timeframe";
    public static final String KEY_start = "start";
    public static final String KEY_end = "end";
    public static final String KEY_status = "status";
    public static final String KEY_cost = "cost";

    public int project_ID;
    public String title;
    public String type;
    public int priority;
    public String timeframe;
    public String start;
    public String end;
    public String status;
    public int cost;


}

这是我的xml将显示的地方

public class ProjectDetail extends Activity implements View.OnClickListener{

    Button btnSave, btnDelete, btnClose;
    EditText eTtitle, etType, eTprio, eTtf;
    EditText eTsd, eTed,eTcost,eTstat;
    private int _Project_Id=0;

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

        btnSave = (Button) findViewById(R.id.btnSave);
        btnDelete = (Button) findViewById(R.id.btnClose);
        btnClose = (Button) findViewById(R.id.btnClose);

        eTtitle = (EditText) findViewById(R.id.eTtitle);
        etType = (EditText) findViewById(R.id.eTtype);
        eTprio = (EditText) findViewById(R.id.eTprio);
        eTtf = (EditText) findViewById(R.id.eTtf);
        eTsd = (EditText) findViewById(R.id.eTsd);
        eTed = (EditText) findViewById(R.id.eTed);
        eTcost = (EditText) findViewById(R.id.eTcost);
        eTstat = (EditText) findViewById(R.id.eTstat);

        _Project_Id=0;
        Intent intent = getIntent();
        _Project_Id = intent.getIntExtra("project_Id", 0);
        ProjectCrud pcrud = new ProjectCrud(this);
        Project project = new Project();
        project = pcrud.getProjectById(_Project_Id);

        eTtitle.setText(project.title);
        etType.setText(project.type);
        eTprio.setText(String.valueOf(project.priority));
        eTtf.setText(project.timeframe);
        eTsd.setText(project.start);
        eTed.setText(project.end);
        eTcost.setText(String.valueOf(project.cost));
        eTstat.setText(project.status);

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_project_detail, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(View view) {
        if (view== findViewById(R.id.btnSave)){
            ProjectCrud pcrud = new ProjectCrud(this);
            Project project = new Project();

            project.title=eTtitle.getText().toString();
            project.type=etType.getText().toString();
            project.priority= Integer.parseInt(eTprio.getText().toString());
            project.timeframe=eTtf.getText().toString();
            project.start=eTsd.getText().toString();
            project.end=eTed.getText().toString();
            project.cost=Integer.parseInt(eTcost.getText().toString());
            project.status=eTstat.getText().toString();
            project.project_ID=_Project_Id;

            if(_Project_Id==0){
                _Project_Id=pcrud.insert(project);

                Toast.makeText(this, "New Project Created", Toast.LENGTH_SHORT).show();
            }else {
                pcrud.update(project);
                Toast.makeText(this, "Project Updated", Toast.LENGTH_SHORT).show();
            }

        }else if (view == findViewById(R.id.btnDelete)){
            ProjectCrud pcrud = new ProjectCrud(this);
            pcrud.delete(_Project_Id);

            Toast.makeText(this, "Project Deleted", Toast.LENGTH_SHORT).show();
            finish();
        }else if (view == findViewById(R.id.btnClose)){
            finish();
        }

    }
}

1 个答案:

答案 0 :(得分:1)

由于create语句中的间距错误,这种情况发生了: 您需要将所有常量定义为

public static final String TABLE = " Project ";

注意字符串中的空格。 如果将空格放在查询中而不是常量中,那么它将更有用 -

       String CREATE_TABLE_PROJECT = "CREATE TABLE " + Project.TABLE + "("
        + Project.KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
        + Project.KEY_title + " TEXT,"
        + Project.KEY_type + " TEXT,"
        + Project.KEY_priority + " INTEGER,"
        + Project.KEY_timeframe + " TEXT,"
        + Project.KEY_start + " TEXT,"
        + Project.KEY_end + " TEXT,"
        + Project.KEY_cost + " INTEGER,"
        + Project.KEY_status + " TEXT" +");";