2013-01-06 69 views
0

我想用SQLliteDatabase做一個基本的數據庫,但它一直給我這個錯誤。我一直在卡住很長一段時間,我猜我犯的錯誤很簡單,但我找不到它。 如果有人能夠幫助這將是偉大沒有這樣的表

01-06 15:07:47.885: I/Database(1824): sqlite returned: error code = 1, msg = no such table: webpages 
01-06 15:07:47.904: E/Database(1824): Error inserting id=1 phone=10001 name=Ivan 
01-06 15:07:47.904: E/Database(1824): android.database.sqlite.SQLiteException: no such table: webpages: , while compiling: INSERT INTO webpages(id, phone, name) VALUES(?, ?, ?); 

這裏是我的代碼使用

public class DatabaseHandler extends SQLiteOpenHelper { 
    /** 
    * The name of the database 
    */ 
    private static final String DATABASE_NAME = "Weboff.db"; 

    /** 
    * The name of the (only) table. 
    */ 
    private static final String TABLE_NAME = "webpages"; 

    /** 
    * The name of the first column (ID) 
    */ 
    private static final String COL_ID = "id"; 

    /** 
    * The name of the second column (NAME) 
    */ 
    private static final String COL_NAME = "name"; 

    /** 
    * The name of the third column (PHONENUMBER) 
    */ 
    private static final String COL_PHONE = "phone"; 

    /** 
    * A constructor which builds a DatabaseHandler object. Note that calling 
    * the constructor does not create a database. This does not happen until 
    * the first call to getReadableDatabase() or getWriteableDatabase() 
    * 
    * @param context 
    *   In this case, a reference to LecturerActivity 
    */ 
    public DatabaseHandler(Context context) { 
     super(context, DATABASE_NAME, null, 1); 
    } 

    /** 
    * This method is called when the database is created for the first time. 
    * This is where the creation of tables and the initial population of the 
    * tables should happen. 
    */ 
    @Override 
    public void onCreate(SQLiteDatabase db) { 
     String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_NAME + "(" 
       + COL_ID + " INTEGER PRIMARY KEY," + COL_NAME + " TEXT," 
       + COL_PHONE + " TEXT" + ")"; 
     db.execSQL(CREATE_CONTACTS_TABLE); 
    } 

    /** 
    * Called when the database needs to be upgraded. Only relevant when you 
    * have multiple versions of the database scheme in play. 
    * 
    */ 
    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldNum, int newNum) { 
     // Drop older table if exist and create fresh 
     db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME); 
     onCreate(db); 
    } 

    /** 
    * Use this method to add a Lecturer to the database. 
    * 
    * @param lecturer 
    *   the Lecturer you want to add 
    */ 
    public void addLecturer(Lecturer lecturer) { 
     SQLiteDatabase db = this.getWritableDatabase(); 
     ContentValues values = new ContentValues(); 
     values.put(COL_ID, lecturer.getID()); 
     values.put(COL_NAME, lecturer.getName()); 
     values.put(COL_PHONE, lecturer.getPhoneNumber()); 
     db.insert(TABLE_NAME, null, values); 
     db.close(); 
    } 

    /** 
    * Use this method to get all of the Lecturers in the database. 
    * 
    * @return a list of Lecturer objects, one per row 
    */ 
    public List<Lecturer> getAll() { 
     List<Lecturer> list = new ArrayList<Lecturer>(); 
     String selectQuery = "SELECT * FROM " + TABLE_NAME; 
     SQLiteDatabase db = this.getReadableDatabase(); 
     Cursor cursor = db.rawQuery(selectQuery, null); 
     if (cursor.moveToFirst()) { 
      do { 
       Lecturer lecturer = new Lecturer(cursor.getInt(0), 
         cursor.getString(1), cursor.getString(2)); 
       list.add(lecturer); 
      } while (cursor.moveToNext()); 
     } 
     return list; 
    } 

    /** 
    * Use this method to remove all of the Lecturers from the database. This is 
    * useful when experimenting. After dropping all tables, the initial state 
    * of the database is re-created. 
    */ 
    public void removeAll() { 
     SQLiteDatabase db = this.getWritableDatabase(); 
     db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME); 
     onCreate(db); 
    } 

    /** 
    * This method removes one lecturer from the database. 
    * 
    * @param lecturer 
    *   the Lecturer to remove 
    */ 
    public void deleteLecturer(Lecturer lecturer) { 
     SQLiteDatabase db = this.getWritableDatabase(); 
     db.delete(TABLE_NAME, COL_ID + " = ?", 
       new String[] { String.valueOf(lecturer.getID()) }); 
     db.close(); 
    } 

    /** 
    * This method updates the data stored in the database for one Lecturer. 
    * 
    * @param lecturer 
    *   the Lecturer to update 
    * @return the number of rows affected 
    */ 
    public int updateLecturer(Lecturer lecturer) { 
     SQLiteDatabase db = this.getWritableDatabase(); 
     ContentValues values = new ContentValues(); 
     values.put(COL_NAME, lecturer.getName()); 
     values.put(COL_PHONE, lecturer.getPhoneNumber()); 
     return db.update(TABLE_NAME, values, COL_ID + " = ?", 
       new String[] { String.valueOf(lecturer.getID()) }); 
    } 

    /** 
    * This method gets a single Lecturer from the database, using the ID field 
    * as a key 
    * ; 
    * @param id 
    *   the ID of the Lecturer we want 
    * @return a Lecturer object 
    */ 
    public Lecturer getLecturer(int id) { 
     SQLiteDatabase db = this.getReadableDatabase(); 
     Cursor cursor = db.query(TABLE_NAME, new String[] { COL_ID, COL_NAME, 
       COL_PHONE }, COL_ID + "=?", 
       new String[] { String.valueOf(id) }, null, null, null, null); 
     if (cursor != null) { 
      cursor.moveToFirst(); 
     } 
     Lecturer lecturer = new Lecturer(Integer.parseInt(cursor.getString(0)), 
       cursor.getString(1), cursor.getString(2)); 
     return lecturer; 
    } 
} 



public class Lecturer { 

    /** 
    * The ID assigned to the lecturer. Starts at 1. 
    */ 
    private int ID; 

    /** 
    * The full name of the lecturer. 
    */ 
    private String name; 

    /** 
    * The internal phone number of the lecturer. 4 digits. 
    */ 
    private String phoneNumber; 


    /** Builds a new Lecturer object 
    * 
    * @param ID The ID assigned to the lecturer. 
    * @param name The full name of the lecturer 
    * @param phoneNumber The internal phone number of the lecturer 
    */ 
    public Lecturer(int ID, String name, String phoneNumber){ 
     this.ID = ID; 
     this.name = name; 
     this.phoneNumber = phoneNumber; 
    } 

    /** 
    * Get the full name of the lecturer 
    * 
    * @return the lecturers full name 
    */ 
    public String getName(){ 
     return this.name; 
    } 

    /** 
    * Set the name of the lecturer 
    * 
    * @param name the lecturers full name 
    */ 
    public void setName(String name){ 
     this.name = name; 
    } 

    /** 
    * Get the phone number of the lecturer 
    * 
    * @return the lecturers full name 
    */ 
    public String getPhoneNumber(){ 
     return this.phoneNumber; 
    } 

    /** 
    * Set the phone number of the lecturer 
    * 
    * @param name the lecturers phone number 
    */ 
    public void setPhoneNumber(String phoneNumber){ 
     this.phoneNumber = phoneNumber; 
    } 

    /** 
    * Get the ID of the lecturer 
    * 
    * @return the lecturers ID 
    */ 
    public int getID() { 
     return ID; 
    } 

    /** 
    * Set the ID of the lecturer 
    * 
    * @param name the lecturers ID 
    */ 
    public void setID(int iD) { 
     ID = iD; 
    } 
} 




And in the onCreate method 

public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.home); 


     regularExpresionStrings(); 


     dh = new DatabaseHandler(this); 
     dh.addLecturer(new Lecturer(1, "Ivan", "10001")); 

      Log.d("Database: ", "Listing all lecturers.."); 
      List<Lecturer> list = dh.getAll(); 
      for (Lecturer lr : list) { 
       String log = "ID:" + lr.getID() +" Name: " + lr.getName() + " Phone: " + lr.getPhoneNumber(); 
       Log.d("Database: ", log); 
      } 
+0

只是一個建議。自從您最初創建數據庫文件後,您是否更改了代碼?也許代碼現在是正確的,但你仍然在使用沒有該表創建的數據庫。擺脫文件,並從頭開始嘗試。 –

+0

將'')「'改爲'」);「'CREATE_CONTACTS_TABLE String」因爲沒有創建表 –

+0

此外,嘗試檢索數據庫文件並用SQLite查看器程序打開它,並找出其中的實際表。 –

回答

1

如果使用Cursor,你的表必須有column = "_id",不"id"

2

闡述,光標需要一個_id,您可能不需要將「id」更改爲「_id」,只需使用查詢ID爲_id即可。

String [] columns = new String [] {「id AS _id」};

這將使查詢查找「id」爲「_id」