mercredi 29 avril 2015

near synatx error (code 1) insert Android SQLite

I am creating a Contact Book app where users input name, email address and number. I want this data to be saved in a database, but I can't seem to get the insert method to work: The error I'm getting is:

android.database.sqlite.SQLiteException: near "Number": syntax error (code 1): , while compiling: INSERT INTO CONTACTSTABLE(Phone Number,Email Address,Name) VALUES (?,?,?)

Here is where I put my database:

public class DatabaseAdapter{

MySQLiteHelper dbhelper;

public DatabaseAdapter(Context context){
    dbhelper=new MySQLiteHelper(context);
}

public long insertData(String name, String phone, String email){
    SQLiteDatabase db=dbhelper.getWritableDatabase();
    ContentValues contentValues=new ContentValues();
    contentValues.put(dbhelper.NAME, name);
    contentValues.put(dbhelper.NUMBER, phone);
    contentValues.put(dbhelper.EMAIL, email);
    long id=db.insert(MySQLiteHelper.TABLE_NAME, null, contentValues);
    return id;
}
static class MySQLiteHelper extends SQLiteOpenHelper {
    private static final String DATABASE_NAME = "contacts.db";
    private static final String TABLE_NAME = "CONTACTSTABLE";
    private static final String UID = "_id";
    private static final String NAME = "Name";
    private static final String EMAIL = "Email Address";
    private static final String NUMBER = "Phone Number";
    private static final int DATABASE_VERSION = 1;

    private static final String DATABASE_CREATE = "CREATE TABLE "
            + TABLE_NAME + " ( " + UID
            + " INTEGER PRIMARY KEY AUTOINCREMENT, " + NAME
            + " TEXT, " + NUMBER
            + " TEXT, " + EMAIL
            + " TEXT" + " ) ";

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

    @Override
    public void onCreate(SQLiteDatabase database) {
        database.execSQL(DATABASE_CREATE);
    }

    //onupgrade calls database needs to be upgraded. use to drop tables, called when you update data version
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.w(MySQLiteHelper.class.getName(),
                "Upgrading database from version " + oldVersion + " to "
                        + newVersion + ", which will destroy all old data");
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
        onCreate(db);
    }}}

Here is my MainActivity where I call the "insertData" method (I took a bunch of other things out for simplicity):

public class MainActivity extends ActionBarActivity implements View.OnClickListener {
EditText name;
EditText number;
EditText email;
Button submit;
DatabaseAdapter dbHelper;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    name=(EditText) findViewById(R.id.name);
    number=(EditText) findViewById(R.id.number);
    email=(EditText) findViewById(R.id.email);
    submit=(Button) findViewById(R.id.button);


    dbHelper=new DatabaseAdapter(this);
    submit.setOnClickListener(this);
}


@Override
public void onClick(View v) {
    if (isEmpty(name) || isEmpty(number) || isEmpty(email))
        preview.setText("Please fill out all of the boxes before submitting");
    else {
        String n=name.getText().toString();
        String p=number.getText().toString();
        String e=email.getText().toString();

       Contact c = new Contact(n,p,e);
       ContactArray.contacts.add(c);
       dbHelper.insertData(n,p,e);
}}

Aucun commentaire:

Enregistrer un commentaire