2015-04-29 68 views
-1

我正在創建一個聯繫簿應用程序,其中用戶輸入姓名,電子郵件地址和號碼。我想這個數據被保存在數據庫中,但我似乎無法得到插入方法的工作: 我得到的錯誤是:附近synatx錯誤(代碼1)插入Android SQLite

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

這裏就是我把我的數據庫:

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

這裏是我的MainActivity,我稱之爲「insertData」的方法(我帶了簡單一堆其他的事情了):

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

有coulmn名稱中的空格是一個壞主意 –

回答

1

從列名中刪除所有空格,然後嘗試agai並告訴我們是否有其他編譯錯誤。 您可以使用_而不是空格,例如Phone_number

+0

這工作。非常感謝你的幫助! –

相關問題