2016-11-25 158 views
0

好吧,所以我有一個額外的表的創建問題,最初我創建了一個名爲Contacts的表,它工作正常。表沒有被創建SQLite

然後,我在我的DBHandler內創建了一個額外的表消息到我的onCreate方法中,這是我開始遇到問題時。

首先,logcat只會拒絕提供一個錯誤,說明它爲什麼不起作用,然後我將程序從我的avd中刪除,以開始新的數據庫文件夾,以防萬一這是問題,現在它在我的表消息附近發出語法錯誤,我也改變了我的DBVersion號。

這是我的DBHandler,我相信這個問題發生在onCreate方法中,並帶有Create messages表。

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(); 

    } 

    } 
+1

顯示您的logcat請 – Jordan

+0

嗨請檢查這[鏈接](http://www.androhub.com/android-sqlite-database/)。 –

+1

'字符串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」+「)」;'我猜你的意思是** PRIMARY **。 –

回答

2

你有一對夫婦在你的代碼輸入錯誤的:

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" + ")"; 

正如有人已經指出,你需要使其成爲PRIMARYKEY但是,您還需要爲"CREATE TABLE" +添加一個空格,以便在表名串聯後使其成爲適當的SQL語句"CREATE TABLE " +

0

從@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" + ")";

我猜你的意思PRIMARY