打开数据库附近的SQLException" IS":语法错误(代码1):

时间:2015-08-02 14:36:23

标签: android sqlite

我创建了一个名为myHealth的SQLlite数据库,它有以下表:TABLE_PERSON,TABLE_CONTACT和TABLE_MEDICALPROFILE

我正在尝试创建一个personin table person,我收到以下错误

08-02 15:22:27.930  15501-15501/? E/memtrack﹕ Couldn't load memtrack module (No such file or directory)
08-02 15:22:27.931  15501-15501/? E/android.os.Debug﹕ failed to load memtrack module: -2
08-02 15:23:16.364  15511-15511/? E/SQLiteLog﹕ (1) near "IS": syntax error
08-02 15:23:16.403  15511-15511/? E/PersonDAO﹕ SQLException on openning database near "IS": syntax error (code 1): , while compiling: CREATE TABLE person(_id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL,surname TEXT NOT NULL,gender TEXT NOT NULL,dob TEXT NOT NULL,height REAL NOT NULL,weight REAL NOT NULL,systolicbp INTEGER NOT NULL,diastolicbp INTEGER NOT NULL,bpdate TEXT IS NOT NULL,maxhrrate INTEGER NOT NULL,minhrrate INTEGER NOT NULL );
08-02 15:25:07.187  15511-15511/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.s210121629.myhealth, PID: 15511
    java.lang.NullPointerException: Attempt to invoke virtual method 'long android.database.sqlite.SQLiteDatabase.insert(java.lang.String, java.lang.String, android.content.ContentValues)' on a null object reference
            at com.example.s210121629.myhealth.Database.PersonDAO.createPerson(PersonDAO.java:65)
            at com.example.s210121629.myhealth.SignupActivity$5.run(SignupActivity.java:143)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

我创建的Databasehelper类被调用:DatabaseHelperOg代码在

之下
public class DatabaseHelperOg extends SQLiteOpenHelper{


    public static final String TAG = "DatabaseHelperOg";

    // Columns of Person Table
    public static final String TABLE_PERSON = "person";
    public static final String COLUMN_PERSON_ID = "_id";
    public static final String COLUMN_PERSON_NAME = "name";
    public static final String COLUMN_PERSON_SURNAME = "surname";
    public static final String COLUMN_PERSON_GENDER = "gender";
    public static final String COLUMN_PERSON_DOB = "dob";
    public static final String COLUMN_PERSON_HEIGHT = "height";
    public static final String COLUMN_PERSON_WEIGHT = "weight";
    public static final String COLUMN_PERSON_SYSTOLICBP = "systolicbp";
    public static final String COLUMN_PERSON_DIASTOLICBP = "diastolicbp";
    public static final String COLUMN_PERSON_BPDATE = "bpdate";
    public static final String COLUMN_PERSON_MAXRATE = "maxhrrate";
    public static final String COLUMN_PERSON_MINRATE = "minhrrate";

    // columns of the contact table
    public static final String TABLE_CONTACT = "contacts";
    public static final String COLUMN_CONTACT_ID = COLUMN_PERSON_ID;
    public static final String COLUMN_CONTACT_NAME = "kinname";
    public static final String COLUMN_CONTACT_SURNAME = "kinsurname";
    public static final String COLUMN_CONTACT_CELLPHONE = "kincellphone";
    public static final String COLUMN_CONTACT_CAREGIVER_NAME = "caregivername";
    public static final String COLUMN_CONTACT_CAREGIVER_SURNAME = "caregiversurname";
    public static final String COLUMN_CONTACT_CAREGIVER_CELLPHONE = "caregivercell";
    public static final String COLUMN_CONTACT_CAREGIVER_TELEPHONE = "caregivertel";
    public static final String COLUMN_CONTACT_PERSON_ID = "person_id";

    //column of the medical profile table
    public static final String TABLE_MEDICALPROFILE = "medicalprofile";
    public static final String COLUMN_MEDICALPROFILE_ID = COLUMN_PERSON_ID;
    public static final String COLUMN_MEDICALPROFILE_CONDITION = "condition";
    public static final String COLUMN_MEDICALPROFILE_DATEDIAGNOSED = "datediagnosed";
    public static final String COLUMN_MEDICALPROFILE_PERSON_ID = "medical_id";

    private static final String DATABASE_NAME = "myhealth.db";
    private static final int DATABASE_VERSION = 1;

    // SQL statement of the person table creation
    private static final String SQL_CREATE_CONTACT = "CREATE TABLE " + TABLE_CONTACT + "("
            + COLUMN_CONTACT_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
            + COLUMN_CONTACT_NAME + " TEXT NOT NULL, "
            + COLUMN_CONTACT_SURNAME + " TEXT NOT NULL, "
            + COLUMN_CONTACT_CELLPHONE + " TEXT NOT NULL, "
            + COLUMN_CONTACT_CAREGIVER_NAME + " TEXT NOT NULL, "
            + COLUMN_CONTACT_CAREGIVER_SURNAME + " TEXT NOT NULL, "
            + COLUMN_CONTACT_CAREGIVER_CELLPHONE + " TEXT NOT NULL, "
            + COLUMN_CONTACT_CAREGIVER_TELEPHONE + " TEXT NOT NULL, "
            + COLUMN_CONTACT_PERSON_ID + " INTEGER NOT NULL "
            +");";


    // SQL statement of the person table creation
    private static final String SQL_CREATE_TABLE_PERSON = "CREATE TABLE " + TABLE_PERSON + "("
            + COLUMN_PERSON_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
            + COLUMN_PERSON_NAME + " TEXT NOT NULL,"
            + COLUMN_PERSON_SURNAME + " TEXT NOT NULL,"
            + COLUMN_PERSON_GENDER + " TEXT NOT NULL,"
            + COLUMN_PERSON_DOB + " TEXT NOT NULL,"
            + COLUMN_PERSON_HEIGHT + " REAL NOT NULL,"
            + COLUMN_PERSON_WEIGHT + " REAL NOT NULL,"
            + COLUMN_PERSON_SYSTOLICBP + " INTEGER NOT NULL,"
            + COLUMN_PERSON_DIASTOLICBP + " INTEGER NOT NULL,"
            + COLUMN_PERSON_BPDATE + " TEXT IS NOT NULL,"
            + COLUMN_PERSON_MAXRATE + " INTEGER NOT NULL,"
            + COLUMN_PERSON_MINRATE + " INTEGER NOT NULL "
            +");";

    // SQL statement of the medical profile table creation
    private static final String SQL_CREATE_TABLE_MEDICAL_PROFILE = "CREATE TABLE " + TABLE_MEDICALPROFILE + "("
            + COLUMN_MEDICALPROFILE_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
            + COLUMN_MEDICALPROFILE_CONDITION + " TEXT NOT NULL, "
            + COLUMN_MEDICALPROFILE_DATEDIAGNOSED + " TEXT NOT NULL, "
            + COLUMN_MEDICALPROFILE_PERSON_ID + " INTEGER NOT NULL "
            +");";

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

    @Override
    public void onCreate(SQLiteDatabase database) {
        database.execSQL(SQL_CREATE_TABLE_PERSON);
        database.execSQL(SQL_CREATE_CONTACT);

        database.execSQL(SQL_CREATE_TABLE_MEDICAL_PROFILE);

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.w(TAG,
                "Upgrading the database from version " + oldVersion + " to " + newVersion);
        // clear all data
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACT);
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_PERSON);
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_MEDICALPROFILE);
        // recreate the tables
        onCreate(db);
    }

    public DatabaseHelperOg(Context context, String name, SQLiteDatabase.CursorFactory factory,int version) {
        super(context, DATABASE_NAME, factory, DATABASE_VERSION);
    }
}

PersonDAO 公共类PersonDAO {     public static final String TAG =" PersonDAO&#34 ;;

// Database fields
private SQLiteDatabase mDatabase;
private DatabaseHelperOg mDbHelper;
private Context mContext;

private String[] mAllColumns = {DatabaseHelperOg.COLUMN_PERSON_ID, DatabaseHelperOg.COLUMN_PERSON_NAME, DatabaseHelperOg.COLUMN_PERSON_SURNAME,
        DatabaseHelperOg.COLUMN_PERSON_GENDER, DatabaseHelperOg.COLUMN_PERSON_DOB, DatabaseHelperOg.COLUMN_PERSON_HEIGHT, DatabaseHelperOg.COLUMN_PERSON_WEIGHT, DatabaseHelperOg.COLUMN_PERSON_SYSTOLICBP, DatabaseHelperOg.COLUMN_PERSON_DIASTOLICBP,
        DatabaseHelperOg.COLUMN_PERSON_BPDATE, DatabaseHelperOg.COLUMN_PERSON_MAXRATE, DatabaseHelperOg.COLUMN_PERSON_MINRATE};

public PersonDAO(Context context) {
    this.mContext = context;
    mDbHelper = new DatabaseHelperOg(context);
    // open the database
    try {
        open();
    } catch (SQLException e) {
        Log.e(TAG, "SQLException on openning database " + e.getMessage());
        e.printStackTrace();
    }
}

public void open() throws SQLException {
    mDatabase = mDbHelper.getWritableDatabase();
}

public void close() {
    mDbHelper.close();
}

    public Person createPerson(String mName, String mSurname, String mGender, String mDob, double mHeight, double mWeight, Integer mSystolic, Integer mDiastolic, String mBpdate, Integer mMaxrate, Integer mMinrate) {
        ContentValues values = new ContentValues();
        values.put(DatabaseHelperOg.COLUMN_PERSON_NAME, mName);
        values.put(DatabaseHelperOg.COLUMN_PERSON_SURNAME, mSurname);
        values.put(DatabaseHelperOg.COLUMN_PERSON_GENDER, mGender);
        values.put(DatabaseHelperOg.COLUMN_PERSON_DOB, mDob);
        values.put(DatabaseHelperOg.COLUMN_PERSON_HEIGHT, mHeight);
        values.put(DatabaseHelperOg.COLUMN_PERSON_WEIGHT, mWeight);
        values.put(DatabaseHelperOg.COLUMN_PERSON_SYSTOLICBP, mSystolic);
        values.put(DatabaseHelperOg.COLUMN_PERSON_DIASTOLICBP, mDiastolic);
        values.put(DatabaseHelperOg.COLUMN_PERSON_BPDATE, mBpdate);
        values.put(DatabaseHelperOg.COLUMN_PERSON_MAXRATE, mMaxrate);
        values.put(DatabaseHelperOg.COLUMN_PERSON_MINRATE, mMinrate);
        long insertId = mDatabase
                .insert(DatabaseHelperOg.TABLE_PERSON, null, values);
        Cursor cursor = mDatabase.query(DatabaseHelperOg.TABLE_PERSON, mAllColumns,
                DatabaseHelperOg.COLUMN_PERSON_ID + " = " + insertId, null, null,
                null, null);
        cursor.moveToFirst();
        Person newPerson = cursorToPerson(cursor);
        cursor.close();
        return newPerson;
    }

    public void deleteContact(Person person) {
        long id = person.getId();
        // delete all employees of this company
        ContactDAO employeeDao = new ContactDAO(mContext);
        List<Contact> listContacts = employeeDao.getContactsOfPerson(id);
        if (listContacts != null && !listContacts.isEmpty()) {
            for (Contact e : listContacts) {
                employeeDao.deleteContact(e);
            }
        }

        System.out.println("the deleted company has the id: " + id);
        mDatabase.delete(DatabaseHelperOg.TABLE_PERSON, DatabaseHelperOg.COLUMN_PERSON_ID
                + " = " + id, null);
    }

    public List<Person> getAllPeople() {
        List<Person> listCompanies = new ArrayList<Person>();

        Cursor cursor = mDatabase.query(DatabaseHelperOg.TABLE_PERSON, mAllColumns,
                null, null, null, null, null);
        if (cursor != null) {
            cursor.moveToFirst();
            while (!cursor.isAfterLast()) {
                Person person = cursorToPerson(cursor);
                listCompanies.add(person);
                cursor.moveToNext();
            }

            // make sure to close the cursor
            cursor.close();
        }
        return listCompanies;
    }

    public Person getPersonById(long id) {
        Cursor cursor = mDatabase.query(DatabaseHelperOg.TABLE_PERSON, mAllColumns,
                DatabaseHelperOg.COLUMN_PERSON_ID + " = ?",
                new String[]{String.valueOf(id)}, null, null, null);
        if (cursor != null) {
            cursor.moveToFirst();
        }

        Person person = cursorToPerson(cursor);
        return person;
    }

    protected Person cursorToPerson(Cursor cursor) {
        Person person = new Person();
        person.setId(cursor.getLong(0));
        person.setmName(cursor.getString(1));
        person.setmSurname(cursor.getString(2));
        person.setmGender(cursor.getString(3));
        person.setmDob(cursor.getString(4));
        person.setmHeight(cursor.getDouble(5));
        person.setmWeight(cursor.getDouble(6));
        person.setmSystolic(cursor.getInt(7));
        person.setmDiastolic(cursor.getInt(8));
        person.setmBpdate(cursor.getString(9));
        person.setmMaxrate(cursor.getInt(10));
        person.setmMinrate(cursor.getInt(11));

        return person;
    }
}

SignUp我调用insertPerson的活动

public class SignupActivity extends AppCompatActivity {

    private static final String TAG = "SignupActivity";

    @InjectView(R.id.input_name)
    EditText _nameText;
    @InjectView(R.id.input_email)
    EditText _emailText;
    @InjectView(R.id.input_password)
    EditText _passwordText;
    @InjectView(R.id.btn_signup)
    Button _signupButton;
    @InjectView(R.id.link_login)
    TextView _loginLink;
    @InjectView((R.id.input_height))
    EditText _heightText;
    @InjectView((R.id.input_weight))
    EditText _weightText;
    @InjectView((R.id.input_surname))
    EditText _surnameText;
    @InjectView(R.id.input_dob)
    EditText _dob;
    @InjectView(R.id.radiogroupGender)
    RadioGroup _gender;
    @InjectView(R.id.input_confirm_password)
    EditText _confirmpassword;
    private DatePickerDialog pickdob;
    private DateFormat dateformatter;
    LoginDataBaseAdapter loginDataBaseAdapter;
    private PersonDAO mPersonDao;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_signup);
        ButterKnife.inject(this);
        loginDataBaseAdapter = new LoginDataBaseAdapter(this);
        loginDataBaseAdapter = loginDataBaseAdapter.open();
        this.mPersonDao = new PersonDAO(this);
        _signupButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                signup();
            }
        });
        dateformatter = new SimpleDateFormat("dd-MM-yyyy", Locale.US);
        _loginLink.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Finish the registration screen and return to the Login activity
                Intent intentlogin = new Intent(getApplicationContext(), LoginActivity.class);
                startActivity(intentlogin);
                finish();
            }
        });

        _dob.setInputType(InputType.TYPE_NULL);
        _dob.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openDatePicker();
                pickdob.show();
            }
        });


    }

    public void openDatePicker() {
        Calendar newCalendar = Calendar.getInstance();
        pickdob = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {

            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                Calendar newDate = Calendar.getInstance();
                newDate.set(year, monthOfYear, dayOfMonth);
                _dob.setText(dateformatter.format(newDate.getTime()));
            }

        }, newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));

    }

    public void signup() {
        Log.d(TAG, "Signup");

        if (!validate()) {
            onSignupFailed();
            return;
        }

        _signupButton.setEnabled(false);

        final ProgressDialog progressDialog = new ProgressDialog(SignupActivity.this,
                R.style.AppTheme_Dark_Dialog);
        progressDialog.setIndeterminate(true);
        progressDialog.setMessage("Creating Account...");
        progressDialog.show();

        final String name = _nameText.getText().toString();
        final String email = _emailText.getText().toString();
        final String password = _passwordText.getText().toString();
        final String surname = _surnameText.getText().toString();
        final String dob = _dob.getText().toString();
        final Double height = Double.parseDouble(_heightText.getText().toString());
        final Double weight = Double.parseDouble(_weightText.getText().toString());

        // TODO: Implement your own signup logic here.

        new android.os.Handler().postDelayed(
                new Runnable() {
                    public void run() {
                        // On complete call either onSignupSuccess or onSignupFailed
                        // depending on success
                        loginDataBaseAdapter.insertEntry(email, password);
                        mPersonDao.createPerson(name,surname,"Male","dob",height,weight,20,20,"Today",20,20);
                        onSignupSuccess();
                        // onSignupFailed();
                        progressDialog.dismiss();
                    }
                }, 3000);
    }


    public void onSignupSuccess() {
        _signupButton.setEnabled(true);
        setResult(RESULT_OK, null);

        finish();
    }

    public void onSignupFailed() {
        Toast.makeText(getBaseContext(), "Login failed", Toast.LENGTH_LONG).show();

        _signupButton.setEnabled(true);
    }

    public boolean validate() {
        boolean valid = true;

        String name = _nameText.getText().toString();
        String email = _emailText.getText().toString();
        String password = _passwordText.getText().toString();
        Double height = Double.parseDouble(_heightText.getText().toString());
        Double weight = Double.parseDouble(_weightText.getText().toString());
        String surname = _surnameText.getText().toString();
        int selected = _gender.getCheckedRadioButtonId();
        RadioButton b = (RadioButton) findViewById(selected);
        String passord = _confirmpassword.getText().toString();
        String thegender = b.getText().toString();

        if (name.isEmpty() || name.length() < 3) {
            _nameText.setError("at least 3 characters");
            valid = false;
        } else {
            _nameText.setError(null);
        }

        if (thegender.isEmpty()) {
            _gender.animate();
            valid = false;
        } else {
            _gender.isActivated();
        }
        if (surname.isEmpty() || surname.length() < 3) {
            _surnameText.setError("at least 3 characters");
            valid = false;

        } else {
            _surnameText.setError(null);
        }

        if (height < 1.00 || height > 2.00 || height.equals("")) {
            _heightText.setError("the height is invalid");
            valid = false;
        } else {
            _heightText.setError(null);
        }

        if (weight < 40 || weight > 110 || weight.equals("")) {
            _weightText.setError("the weight is invalid");
            valid = false;
        } else {
            _weightText.setError(null);
        }
        if (email.isEmpty() || !android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches()) {
            _emailText.setError("enter a valid email address");
            valid = false;
        } else {
            _emailText.setError(null);
        }

        if (password.isEmpty() || password.length() < 4 || password.length() > 10) {
            _passwordText.setError("between 4 and 10 alphanumeric characters");
            valid = false;
        } else {
            _passwordText.setError(null);
        }
        if (_confirmpassword.equals(passord)) {
            _confirmpassword.setError("passwords do not match");
            valid = false;
        } else {
            _confirmpassword.setError(null);
        }
        return valid;
    }
    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();

        loginDataBaseAdapter.close();
    }
}

人类

public class Person implements Serializable {

    public static final String TAG = "Person";
    private static final long serialVersionUID = -7406082437623008161L;

    private long mId;
    private String mName;
    private String mSurname;
    private String mGender;
    private String mDob;
    private double mHeight;
    private double mWeight;
    private Integer mSystolic;
    private Integer mDiastolic;
    private String mBpdate;
    private Integer mMaxrate;
    private Integer mMinrate;

    public Person() {

    }

    public Person(String mName, String mSurname, String mGender, String mDob, double mHeight, double mWeight, Integer mSystolic, Integer mDiastolic, String mBpdate, Integer mMaxrate, Integer mMinrate) {
        this.mName = mName;
        this.mSurname = mSurname;
        this.mGender = mGender;
        this.mDob = mDob;
        this.mHeight = mHeight;
        this.mWeight = mWeight;
        this.mSystolic = mSystolic;
        this.mDiastolic = mDiastolic;
        this.mBpdate = mBpdate;
        this.mMaxrate = mMaxrate;
        this.mMinrate = mMinrate;
    }

    public long getId() {
        return mId;
    }

    public void setId(long mId) {
        this.mId = mId;
    }

    public String getmName() {
        return mName;
    }

    public String getmSurname() {
        return mSurname;
    }

    public String getmGender() {
        return mGender;
    }

    public String getmDob() {
        return mDob;
    }

    public Double getmHeight() {
        return mHeight;
    }

    public Double getmWeight() {
        return mWeight;
    }

    public Integer getmDiastolic() {
        return mDiastolic;
    }

    public Integer getmSystolic() {
        return mSystolic;
    }

    public String getmBpdate() {
        return mBpdate;
    }

    public Integer getmMaxrate() {
        return mMaxrate;
    }

    public Integer getmMinrate() {
        return mMinrate;
    }

    public void setmName(String mName) {
        this.mName = mName;
    }

    public void setmSurname(String mSurname) {
        this.mSurname = mSurname;
    }

    public void setmGender(String mGender) {
        this.mGender = mGender;
    }

    public void setmDob(String mDob) {
        this.mDob = mDob;
    }

    public void setmHeight(Double mHeight) {
        this.mHeight = mHeight;
    }

    public void setmDiastolic(Integer mDiastolic) {
        this.mDiastolic = mDiastolic;
    }

    public void setmMaxrate(Integer mMaxrate) {
        this.mMaxrate = mMaxrate;
    }

    public void setmBpdate(String mBpdate) {
        this.mBpdate = mBpdate;
    }

    public void setmSystolic(Integer mSystolic) {
        this.mSystolic = mSystolic;
    }

    public void setmWeight(Double mWeight) {
        this.mWeight = mWeight;
    }

    public void setmMinrate(Integer mMinrate) {
        this.mMinrate = mMinrate;
    }
}

我认为我的错误与我检查的数据类型有关,它似乎不是问题,因为我在SQL语句中将我的double声明为REAL NOT NULL。

有人可以试着解释我哪里出错

1 个答案:

答案 0 :(得分:0)

你写了

  

COLUMN_PERSON_BPDATE +“TEXT IS NOT NULL,”+

你必须写

  

TEXT NOT NULL

删除

  

IS