我在尝试向名为“my_card”的表中插入数据时遇到了一个非常奇怪的错误。
这些是我在单独的接口中定义的一些常量:
//tables name
public static final String TABLE_MY_CARD= "my_card" ;
public static final String TABLE_MY_CONTACTS = "my_contacts" ;
// Columns
public static final String NAME = "name" ;
public static final String PHONE = "phone" ;
public static final String EMAIL = "email";
public static final String FACEBOOK = "facebook";
public static final String WEBSITE = "web";
public static final String MAJOR = "major";
public static final String ULINK = "ulink";
接下来我有一个继承了SQLiteOpenHelper的类,其中包含两个函数。最重要的是第一个函数,它可以通过参数创建表格。
public class MySQLite_methods extends SQLiteOpenHelper implements Tables {
private static final String DATABASE_NAME = "my_database.db" ;
private static final int DATABASE_VERSION = 1;
private String TABLE_NAME;
/** Create a helper object for the Events database */
public MySQLite_methods (Context ctx, String table_name)
{
super(ctx, DATABASE_NAME, null, DATABASE_VERSION);
this.TABLE_NAME = table_name;
}
@Override
public void onCreate(SQLiteDatabase db)
{
String sql = "CREATE TABLE IF NOT EXISTS " + TABLE_NAME +
" (" + _ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
NAME + " TEXT NOT NULL," +
PHONE + " TEXT NOT NULL,"+
EMAIL + " TEXT NOT NULL,"+
FACEBOOK + " TEXT,"+
WEBSITE + " TEXT,"+
MAJOR + " TEXT NOT NULL,"+
ULINK + " TEXT NOT NULL" + ");" ;
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
}
最后,在我的应用程序中,按下保存按钮后,将创建一个类的实例,并且一些EditText的值需要插入到数据库中:
MySQLite_methods mysql = new MySQLite_methods (context, TABLE_MY_CARD);
Log.d("geo", "before getWr....");
// getting the database..
SQLiteDatabase db = mysql.getWritableDatabase();
// creating an object of type ContentValues which represents the record i am gonna add into the DB
ContentValues values = new ContentValues();
values.put(NAME, name.getText().toString());
values.put(PHONE, phone.getText().toString());
values.put(EMAIL, email.getText().toString());
values.put(FACEBOOK, facebook.getText().toString());
values.put(WEBSITE, website.getText().toString());
values.put(MAJOR, major.getText().toString());
values.put(ULINK, ulink.getText().toString());
Log.d("geo", "before insert");
// inserting data
db.insertOrThrow(TABLE_MY_CARD, null, values);
在插入的那一瞬间,它会抛出以下异常:
"android.database.sqlite.SQLiteException: table my_card has no column named web ...."
我一遍又一遍地检查了拼写错误,但没有发现...所以现在我不知道该怎么办了...有什么想法吗? 谢谢