2013-01-06 92 views
0

我將數據插入到數據庫中,但由於表不會在我的數據庫中創建,因此可以插入它。我可以能夠創建數據庫,但不能創建表表未在數據庫中創建

sqlite returned: error code = 1, msg = no such table: contactTable 
Error inserting phone=123-456-789 Id=1 [email protected] 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

嘗試卸載該應用程序,然後重新安裝一次。 – Luksprog

+0

嘗試實現Android Sqlite數據庫像這樣這是錯誤的方式http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/或http://www.vogella.com/articles/AndroidSQLite/ article.html –

回答

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

} 
+0

仍然遇到同樣的問題 –

+0

你需要先刪除這個檢查checkDataBase()' –

+0

再次得到同樣的問題。這是創建表的問題。在創建表的字符串中是否有任何問題? –