Reputation: 315
I've been seeing some of the questions concerning this topic (like, for example, this one) but I think I had everything ok and I already tried Database name changes, Database version changes, re-building...
This is some of my Helper code:
private static final int DATABASE_VERSION = 2;
private static final String CONTACTS_TABLE = "contact";
private static final String DATABASE_NAME = "contactlist";
// Column names...
public static final String KEY_ID = "_id";
public static final String KEY_NAME = "name";
public static final String KEY_FAMILY_NAME = "family_name";
public static final String KEY_PHONE = "phone";
public static final String KEY_EMAIL = "email";
public static final String KEY_ADDRESS = "address";
public static final String KEY_ADD_PHONE = "additional_phone";
// ... and a string array of columns.
private static final String[] COLUMNS = {KEY_ID, KEY_NAME, KEY_FAMILY_NAME,
KEY_PHONE, KEY_EMAIL, KEY_ADDRESS, KEY_ADD_PHONE};
// Build the SQL query that creates the table.
private static final String CONTACTS_TABLE_CREATE =
"CREATE TABLE " + CONTACTS_TABLE + " (" +
KEY_ID + " INTEGER PRIMARY KEY, " +
KEY_NAME + " TEXT, " +
KEY_FAMILY_NAME + "TEXT, " +
KEY_PHONE + "TEXT, " +
KEY_EMAIL + "TEXT, " +
KEY_ADDRESS + "TEXT, " +
KEY_ADD_PHONE + "TEXT);";
public ContactHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
Log.d(TAG, "Construct WordListOpenHelper");
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CONTACTS_TABLE_CREATE);
fillDatabaseWithData(db);
}
This is now the insert method:
public long insert(Contact newContact) {
long newId = 0;
ContentValues values = new ContentValues();
values.put(KEY_NAME, newContact.contactName);
values.put(KEY_FAMILY_NAME, newContact.familyName);
values.put(KEY_PHONE, newContact.contactPhone);
values.put(KEY_EMAIL, newContact.email);
values.put(KEY_ADDRESS, newContact.address);
values.put(KEY_ADD_PHONE, newContact.additionalPhone);
try {
if (mWritableDB == null) {mWritableDB = getWritableDatabase();}
newId = mWritableDB.insert(CONTACTS_TABLE, null, values);
} catch (Exception e) {
Log.d(TAG, "INSERT EXCEPTION! " + e.getMessage());
}
return newId;
}
This is where i call the insert method, after getting all the data from the EditTexts:
public void saveNewContact(){
// Getting contact information
this.getContactInformation();
mDB.insert(newContact);
Intent intent = new Intent(getBaseContext(), ContactListActivity.class);
startActivity(intent);
}
The error I am getting is:
table contacts has no column named address
05-11 15:19:31.732 9363-9363/com.example.patricia.contactlist E/SQLiteDatabase: Error inserting name=Patricia address=UNKNOWN family_name=Vera phone=699999999 email=UNKNOWN additional_phone=UNKNOWN
android.database.sqlite.SQLiteException: table contacts has no column named address (code 1): , while compiling: INSERT INTO contacts(name,address,family_name,phone,email,additional_phone) VALUES (?,?,?,?,?,?)
Thank you in advance!
Upvotes: 0
Views: 1418
Reputation: 11
I had declared this in ContactsDB.java
public class ContactsDB {
public static final String KEY_ROWID="_id";
public static final String KEY_NAME="person_name";
public static final String KEY_CELL="_cell";
Actually when storing it via submit I was doing this
public long createEntry(String name, String cell)
{
ContentValues cv = new ContentValues();
cv.put("KEY_NAME", name);
cv.put("KEY_CELL", cell);
return ourDatabase.insert(DATABASE_TABLE, null, cv);
}
whereas I should have done this
public long createEntry(String name, String cell)
{
ContentValues cv = new ContentValues();
cv.put(KEY_NAME, name);
cv.put(KEY_CELL, cell);
return ourDatabase.insert(DATABASE_TABLE, null, cv);
}
Upvotes: 0
Reputation: 5790
There are missing spaces in your SQL for table creation:
"CREATE TABLE " + CONTACTS_TABLE + " (" +
KEY_ID + " INTEGER PRIMARY KEY, " +
KEY_NAME + " TEXT, " +
KEY_FAMILY_NAME + "TEXT, " +
KEY_PHONE + "TEXT, " +
KEY_EMAIL + "TEXT, " +
KEY_ADDRESS + "TEXT, " +
KEY_ADD_PHONE + "TEXT);";
There will be columns named "addressTEXT", "phoneTEXT" etc.
Try:
"CREATE TABLE " + CONTACTS_TABLE + " (" +
KEY_ID + " INTEGER PRIMARY KEY, " +
KEY_NAME + " TEXT, " +
KEY_FAMILY_NAME + " TEXT, " +
KEY_PHONE + " TEXT, " +
KEY_EMAIL + " TEXT, " +
KEY_ADDRESS + " TEXT, " +
KEY_ADD_PHONE + " TEXT);";
Upvotes: 2