0

我正在将数据插入数据库,但由于表不会在我的数据库中创建,我可以插入它。我可以创建数据库但无法创建表

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

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

4

1 回答 1

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

}
于 2013-01-06T06:56:14.183 回答