引起的:android.database.sqlite。SQLiteException:临近“TABLEProject”:语法错误(代码1):,编译时:

时间:2021-10-03 01:10:50

this my databasehelper

我databasehelper

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);
    }

}

this my declaration for table and field

这是我对表和字段的声明

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;


}

this is where my xml will show

这就是我的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 个解决方案

#1


1  

This is happening due to the spacing error in the create statements: You need to define all your constants as

这是由于create语句中的间隔错误造成的:您需要将所有常量定义为

public static final String TABLE = " Project ";

Note the spaces in the string. It will be more useful if you put the spaces in the query instead of the constants as-

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

       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" +");";

#1


1  

This is happening due to the spacing error in the create statements: You need to define all your constants as

这是由于create语句中的间隔错误造成的:您需要将所有常量定义为

public static final String TABLE = " Project ";

Note the spaces in the string. It will be more useful if you put the spaces in the query instead of the constants as-

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

       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" +");";