Reputation: 65
Okay so i have an issue with the creation of an additional table, initially I created a table called Contacts which worked fine.
I then integrated an additional table Messages into my onCreate method within my DBHandler to be created, this is when i started getting issues.
First of all logcat would just refuse to even give an error as to why it wasn't working, then i removed the program off of my avd to start with a fresh new database folder in case this was the issue and now it issuing a syntax error near the my table messages, i have also changed my DBVersion no.
Here is my DBHandler, i believe the problem is occurring within the onCreate method, with the Create messages table.
private static final int DATABASE_VERSION = 2;
private static final String COLUMN_ID = "id";
private static final String DATABASE_NAME = "forensicsproject";
//====================================================
private static final String TABLE_CONTACTS = "contacts";
private static final String CONTACTS_COLUMN_NAME = "name";
private static final String CONTACTS_COLUMN_PHONENUMBER = "phone_number";
//====================================================
private static final String TABLE_MESSAGES = "messages";
private static final String MESSAGES_COLUMN_PERSON = "person";
private static final String MESSAGES_COLUMN_BODY = "body";
private static final String MESSAGES_COLUMN_ADDRESS = "address";
private static final String MESSAGES_COLUMN_TYPE = "type";
//private static final MESSAGES_COLUMN_DATE = "date"; //TODO
public DBHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS + "("
+ COLUMN_ID + " INTEGER PRIMARY KEY," + CONTACTS_COLUMN_NAME + " TEXT,"
+ CONTACTS_COLUMN_PHONENUMBER + " TEXT" + ")";
db.execSQL(CREATE_CONTACTS_TABLE);
Log.e("CONTACT", "CREATED");
String CREATE_MESSAGES_TABLE = "CREATE TABLE" + TABLE_MESSAGES + "("
+ COLUMN_ID + " INTEGER PIMRARY KEY," + MESSAGES_COLUMN_PERSON + " TEXT,"
+ MESSAGES_COLUMN_BODY + " TEXT," + MESSAGES_COLUMN_ADDRESS + " TEXT,"
+ MESSAGES_COLUMN_TYPE + " TEXT" + ")";
db.execSQL(CREATE_MESSAGES_TABLE);
Log.e("MESSAGES", "CREATED");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACTS + TABLE_MESSAGES);
onCreate(db);
}
public void addContact(Contact contact) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(CONTACTS_COLUMN_NAME, contact.getName());
values.put(CONTACTS_COLUMN_PHONENUMBER, contact.getPhoneNumber());
db.insert(TABLE_CONTACTS, null, values);
Log.e("CONTACTS", "INSERTED");
db.close();
}
public void getMessages(Messages messages) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(MESSAGES_COLUMN_PERSON, messages.getPerson());
values.put(MESSAGES_COLUMN_BODY, messages.getBody());
values.put(MESSAGES_COLUMN_ADDRESS, messages.getAddress());
values.put(MESSAGES_COLUMN_TYPE, messages.getType());
db.insert(TABLE_MESSAGES, null, values);
Log.e("MESSAGES", "INSERTED");
db.close();
}
}
Upvotes: 0
Views: 72
Reputation: 65
from @Rotwang
String CREATE_MESSAGES_TABLE = "CREATE TABLE" + TABLE_MESSAGES + "(" + COLUMN_ID + " INTEGER PIMRARY KEY," + MESSAGES_COLUMN_PERSON + " TEXT," + MESSAGES_COLUMN_BODY + " TEXT," + MESSAGES_COLUMN_ADDRESS + " TEXT," + MESSAGES_COLUMN_TYPE + " TEXT" + ")";
I guess you meant PRIMARY
Upvotes: 0
Reputation: 14274
You have a couple of typos in your code:
String CREATE_MESSAGES_TABLE = "CREATE TABLE" + TABLE_MESSAGES + "("
+ COLUMN_ID + " INTEGER PIMRARY KEY," + MESSAGES_COLUMN_PERSON + " TEXT,"
+ MESSAGES_COLUMN_BODY + " TEXT," + MESSAGES_COLUMN_ADDRESS + " TEXT,"
+ MESSAGES_COLUMN_TYPE + " TEXT" + ")";
As someone already pointed out, you need to make it the PRIMARY
KEY
but you also need to add a space to the "CREATE TABLE" +
in order to make it a proper SQL statement "CREATE TABLE " +
once the table name is concatenated.
Upvotes: 2