表未在数据库中创建

时间:2013-01-06 06:49:43

标签: android database sqlite

我正在将数据插入数据库,但我可以插入它,因为表格不会在我的数据库中创建。我能够创建数据库但无法创建表

sqlite returned: error code = 1, msg = no such table: contactTable
Error inserting phone=123-456-789 Id=1 email=xyz@GMAIL.COM key=0r1-2C4E4A3C524A2C523442 name=XYZ ABC

我的代码如下:

private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "contactbackup.sqlite";
private static final String TABLE_NAME = "contactTable";
private static final String CONTACT_ID = "Id";
private static final String CONTACT_NAME = "name";
private static final String CONTACT_PHONE = "phone";
private static final String CONTACT_EMAIL = "email";
private static final String CONTACT_KEY = "key";

public DBmanagaer(Context con) {
    super(con, DATABASE_NAME, null, DATABASE_VERSION);
    // TODO Auto-generated constructor stub

}

@Override
public void onCreate(SQLiteDatabase db) {
    // TODO Auto-generated method stub
    String CONTACT_TABLE_CREATE = "CREATE TABLE "
        + TABLE_NAME.trim() + "(" + CONTACT_ID.trim()
        + " INTEGER PRIMARY KEY , " + CONTACT_NAME.trim() + " TEXT , "
        + CONTACT_PHONE.trim() + " TEXT , " + CONTACT_EMAIL.trim()
        + " TEXT , " + CONTACT_KEY.trim() + " TEXT " + ");";
    if (checkDataBase()) {
    } else {
        try {
            db.execSQL(CONTACT_TABLE_CREATE);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

private boolean checkDataBase() {
    // TODO Auto-generated method stub
    File dbFile = new File(DB_PATH + DATABASE_NAME);
    return dbFile.exists();
}

public void adddata(contactDto con) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(CONTACT_ID, Integer.parseInt(con.ID));
    values.put(CONTACT_NAME, con.name);
    values.put(CONTACT_PHONE, con.contactphone);
    values.put(CONTACT_EMAIL, con.email);
    values.put(CONTACT_KEY, con.key);
    db.insert(TABLE_NAME, null, values);
    db.close(); // Closing database connection
}

public ArrayList<contactDto> getAlldata() {
    String selectQuery = "SELECT  * FROM " + TABLE_NAME;

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);
    if (cursor.moveToFirst()) {
        do {
            contactDto con = new contactDto();
            con.ID = cursor.getString(0);
            con.name = cursor.getString(1);
            con.contactphone = cursor.getString(2);
            con.email = cursor.getString(3);
            con.key = cursor.getString(4);
            conList.add(con);
        } while (cursor.moveToNext());
    }
    cursor.close();
    db.close();
    // return contact list
    return conList;
}

我收到以下行中的错误:

db.insert(TABLE_NAME, null, values);

我已经完成了一些教程,这些教程都是一样的。我不知道我哪里出错了。

1 个答案:

答案 0 :(得分:3)

使用此字符串创建表格,在此字符串中,我刚刚删除了查询末尾的分号。

@Override
public void onCreate(SQLiteDatabase db) {
    // TODO Auto-generated method stub
    String CONTACT_TABLE_CREATE = "CREATE TABLE "
        + TABLE_NAME.trim() + "(" + CONTACT_ID.trim()
        + " INTEGER PRIMARY KEY , " + CONTACT_NAME.trim() + " TEXT , "
        + CONTACT_PHONE.trim() + " TEXT , " + CONTACT_EMAIL.trim()
        + " TEXT , " + CONTACT_KEY.trim() + " TEXT " + ")";


        try {
            db.execSQL(CONTACT_TABLE_CREATE);
        } catch (SQLException e) {
            e.printStackTrace();
        }

}