SQLiteException因语法错误而抛出

时间:2015-08-10 11:36:50

标签: android sqlite

这是一个将个人信息值和存储带入SQLite数据库的代码。

public class AddAPersonActivity extends Activity implements View.OnClickListener, AdapterView.OnItemSelectedListener, RadioGroup.OnCheckedChangeListener {
    private Button btnAdd;
    private EditText etName;
    private TextView tvDob;
    private Spinner spNationality;
    private RadioButton rbMale, rbFemale;
    private Calendar birthday;
    private NameValidator nameValidator;
    private RadioGroup rgGenderGroup;
    private String gender = "";

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

        btnAdd = (Button) findViewById(R.id.btnAdd);
        etName = (EditText) findViewById(R.id.etName);
        tvDob = (TextView) findViewById(R.id.tvDob);
        spNationality = (Spinner) findViewById(R.id.spNationality);
        rbMale = (RadioButton) findViewById(R.id.rbMale);
        rbFemale = (RadioButton) findViewById(R.id.rbFemale);
        rgGenderGroup = (RadioGroup) findViewById(R.id.rgGenderGroup);
        nameValidator = new NameValidator();

        birthday = Calendar.getInstance();
        Locale[] locales = Locale.getAvailableLocales();
        ArrayList<String> countries = new ArrayList<>();
        for(Locale locale: locales) {
            String country = locale.getDisplayCountry();
            if(country.trim().length() > 0 && !countries.contains(country)) {
                countries.add(country);
            }
        }

        Collections.sort(countries);
        for(String country: countries) {
            System.out.println(country);
        }

        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, countries);
        dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        spNationality.setAdapter(dataAdapter);
        spNationality.setSelection(0);

        spNationality.setOnItemSelectedListener(this);
        tvDob.setOnClickListener(this);
        rgGenderGroup.setOnCheckedChangeListener(this);
        btnAdd.setOnClickListener(this);
    }

    void updateLabel() {
        String format = "MM/dd/yy";
        SimpleDateFormat dateFormat = new SimpleDateFormat(format, Locale.US);
        tvDob.setText(dateFormat.format(birthday.getTime()));
    }

    @Override
    public void onClick(View v) {
        switch(v.getId()) {
            case R.id.btnAdd:
                String name = etName.getText().toString();
                Calendar today = Calendar.getInstance();
                int age = today.get(Calendar.YEAR) - today.get(Calendar.YEAR);
                if(today.get(Calendar.MONTH) < birthday.get(Calendar.MONTH)) {
                    age--;
                } else if(today.get(Calendar.MONTH) == birthday.get(Calendar.MONTH) && today.get(Calendar.DAY_OF_MONTH) < birthday.get(Calendar.DAY_OF_MONTH)) {
                    age--;
                }

                String nationality = spNationality.getSelectedItem().toString();

                DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
                java.util.Date currentDate = new java.util.Date();
                Date currentTime = new Date(currentDate.getTime());
                String created_at = dateFormat.format(currentTime);

                if(!(name.length() > 0)) {
                    Toast.makeText(this, "Please don't leave the name empty.", Toast.LENGTH_LONG).show();
                } else if(!(tvDob.getText().toString().length() > 0)) {
                    Toast.makeText(this, "Please set a valid birthday.", Toast.LENGTH_LONG).show();
                } else if(birthday.getTime().compareTo(new java.util.Date()) > 0) {
                    Toast.makeText(this, "Wrong birthday. Please set the valid birthday again.", Toast.LENGTH_LONG).show();
                } else if(!(rbMale.isChecked() || rbFemale.isChecked())) {
                    Toast.makeText(this, "Please choose your gender.", Toast.LENGTH_LONG).show();
                } else if(!nameValidator.validate(etName.getText().toString())) {
                    Toast.makeText(this, "Wrong name. Please enter a proper name.", Toast.LENGTH_LONG).show();
                } else {
                    Toast.makeText(this, "New person is added to the database!", Toast.LENGTH_LONG).show();
                    SQLiteHandler mDBHelper = new SQLiteHandler(this);

                    // Get the data repository in write mode
                    SQLiteDatabase db = mDBHelper.getWritableDatabase();

                    // Create a new map of values, where column names are the keys
                    ContentValues values = new ContentValues();
                    values.put(PersonalInfoEntry.COLUMN_NAME, name);
                    values.put(PersonalInfoEntry.COLUMN_AGE, age);
                    values.put(PersonalInfoEntry.COLUMN_NATIONALITY, nationality);
                    values.put(PersonalInfoEntry.COLUMN_GENDER, gender);
                    values.put(PersonalInfoEntry.COLUMN_CREATED_AT, created_at);

                    db.insert(PersonalInfoEntry.TABLE_NAME, null, values);
                    db.close();
                }
                break;

            case R.id.tvDob:
                DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {

                    @Override
                    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                        birthday.set(Calendar.YEAR, year);
                        birthday.set(Calendar.MONTH, monthOfYear);
                        birthday.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                        updateLabel();
                    }
                };
                new DatePickerDialog(this, date, birthday.get(Calendar.YEAR), birthday.get(Calendar.MONTH), birthday.get(Calendar.DAY_OF_MONTH)).show();
                break;
        }
    }

    void addPersonToGroupTable() {

    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch(checkedId) {
            case(R.id.rbMale):
                gender = "M";
                break;
            case(R.id.rbFemale):
                gender = "F";
                break;
        }
    }
}

此代码显示没有错误,所以我虽然它可以正常工作。但非常不幸的是它停止显示以下信息。

android.database.sqlite.SQLiteException: near "Group": syntax error (code 1): , while compiling: CREATE TABLE Group ( Name TEXT NOT NULL, Age INT, Nationality TEXT NOT NULL, Gender CHAR(1) NOT NULL, Created_At TEXT);

现在我对数据库世界很陌生。它真的是查询的问题,还是有什么问题我还没注意到代码?

非常感谢您的帮助,如果您需要更多信息,请告诉我,我会尽快更新。

2 个答案:

答案 0 :(得分:2)

您无法在表格创建代码中使用Group,因为GROUP是保留关键字

供参考:https://sqlite.org/lang_keywords.html

答案 1 :(得分:1)

使用前缀或后缀将表名更改为user_groupGroup是在SQLite中预定义的关键字,否则无法使用。