表没有创建SQLite

时间:2016-11-25 19:04:18

标签: android

好的,所以我在创建附加表时遇到了问题,最初我创建了一个名为Contacts的表,它运行良好。

然后我将另一个表Message集成到我要创建的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(); 

   }

  }

2 个答案:

答案 0 :(得分: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" + ")";

正如某人已经指出的那样,您需要将其设为 PRIMARY KEY,但您还需要为"CREATE TABLE" +添加空格,以便

表格连接后,使其成为正确的SQL语句"CREATE TABLE " +

答案 1 :(得分: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