I am facing a really weird error,while trying to insert into a table called “my_card”.
These are some constants I defined in a separate interface:
//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";
After that I have a class which extends SQLiteOpenHelper, with 2 functions. The most important is the first one which creates the table given by a parameter.
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);
}
}
And finally, in my activity, after pressing a button for save, an instance of the class will be created and the values of some EditText-s have to be inserted into the database:
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);
At the moment of the insert it gives this exception:
“android.database.sqlite.SQLiteException: table my_card has no column named web ….”
I checked over and over again for typo mistakes, but none… So i dont know what to do now… Any idea?
Thank you
Your create SQL starts with
CREATE TABLE IF NOT EXISTS. So my idea is you added web column while the device has already had that table. If this is the case then just reinstall the app – it will delete the DB, so you can be sure the table will be created with that column.