在synatx错误(代码1)附近插入Android SQLite [英] near synatx error (code 1) insert Android SQLite
问题描述
我正在创建一个联系人簿应用程序,用户可以在其中输入姓名,电子邮件地址和号码.我希望将这些数据保存在数据库中,但似乎无法使insert方法起作用: 我收到的错误是:
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 (?,?,?)
在这里放置数据库:
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"方法(为简单起见,我取出了很多其他东西):
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);
}}
推荐答案
从列名中删除所有空格,然后重试,并告诉我们是否还有其他编译错误.
您可以使用_
代替空格,例如Phone_number
.
Remove all spaces from your column names, then try again and tell us if there are other compile-errors.
You can use _
instead of the space, for example Phone_number
.
这篇关于在synatx错误(代码1)附近插入Android SQLite的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!