2012-11-25 70 views
0

IM開發一個應用程序,我也碰到這樣的問題,我所做的一切和,但仍然沒有解決,它包含一個error = "column 'name' does not exist" 如果可能的話給我一些提示,以解決這個問題 我刪除電話中的數據庫也, 請幫助我提前感謝 。列「名」不存在

public class DataBaseBON { 
     String TAG; 
     private Context ourContext; 
     private dbhelper ourhelper; 
     private SQLiteDatabase ourdatabase; 
     public final static String KEY_ID= "_id"; 
     public final static String KEY_NUMBER="blockednumbers"; 
     public final static String N_NAME="name"; 
     public final static String DATABASE_NAME="databasebon.db";   
     public final static String DATABASE_TABLE="tabledb"; 
     public final static int DATABASE_VERSION=2; 
     private static final String DATABASE_CREATE = "create table " 
       + DATABASE_TABLE + "(" + KEY_ID 
       + " integer primary key autoincrement, " 
       + N_NAME + " text not null," 
       + KEY_NUMBER 
       + " text not null);"; 


     public class dbhelper extends SQLiteOpenHelper{ 

      public dbhelper(Context context) { //? 
      super(context, DATABASE_NAME, null, DATABASE_VERSION); 

     } 

     @Override 
     public void onCreate(SQLiteDatabase database) { 
      database.execSQL(DATABASE_CREATE); 
     } 

     @Override 
     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
      Log.w(dbhelper.class.getName(), 
        "Upgrading database from version " + oldVersion + " to " 
          + newVersion + ", which will destroy all old data"); 
      db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE); 
      onCreate(db); 
     } 

     } 

     public DataBaseBON(Context c){ 

      ourContext =c ; 
     } 

     public DataBaseBON open() throws SQLException{ 
      ourhelper = new dbhelper(ourContext); 
      ourdatabase = ourhelper.getWritableDatabase(); 
      ourdatabase=ourhelper.getReadableDatabase(); 
      return this; 
     } 

     public void close(){ 
      ourhelper.close(); 
     } 

     public long creatEntry(String inputnumber , String name) { 
     // TODO Auto-generated method stub 
     ContentValues cv= new ContentValues(); 
     cv.put(KEY_NUMBER, inputnumber); 
     cv.put(N_NAME, name); 
     Log.v(inputnumber, "adding to Database"); 
     return ourdatabase.insert(DATABASE_TABLE, null, cv); 

    } 


     public Cursor getcursor() { 

     String[] columns = new String[]{ N_NAME, KEY_NUMBER }; 
     Cursor c=ourdatabase.query(DATABASE_TABLE, columns, null, null, null, null, null); 
     return c;} 


    public String[] getcloumns() { 

     String[] columns = new String[]{ N_NAME, KEY_NUMBER }; 

     return columns ; 
    } 

    public int[] getto() { 
     String TAG = null; 
     int[] to = new int[] { android.R.id.text1, android.R.id.text2 }; 
     Log.v(TAG, "get to is ok"); 
     return to ; 
    } 
    public String[] getdata3(String[] result) { 
     String TAG = null; 
     String[] columns = new String[]{ N_NAME, KEY_NUMBER }; 
     Cursor c=ourdatabase.query(DATABASE_TABLE, columns, null, null, null, null, null); 
     int iRow = c.getColumnIndex(N_NAME); 
     int iName = c.getColumnIndex(KEY_NUMBER); 
     for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){ 
     @SuppressWarnings("unused") 
     String[] result1= new String[] {c.getString(iName),c.getString(iRow)}; 
     Log.v(TAG, "is ok"); 
     } 

     return result; 

      } 

錯誤:

11-25 23:49:33.633: E/AndroidRuntime(4072): java.lang.RuntimeException: Unable to startactivity 
ComponentInfo{vahid.engineer.com/vahid.engineer.com.BlackNumbersBlockDb}: 
      java.lang.IllegalArgumentException: column 'name' does not exist 
+0

您的表是否正在創建?你檢查了嗎? – Swayam

+0

嘗試sqlite瀏覽器,並驗證您的表正在創建生活http://sqlitebrowser.sourceforge.net/ –

+0

當我運行它沒有列「名稱」它的工作在我的手機,我可以看到數據庫,但當我嘗試添加名稱我面臨這個問題,我改變了數據庫版本爲3,現在我有新的錯誤是AndroidRuntime(5003):引起:java.lang.IllegalArgumentException:列_id'不存在 – developer

回答

2

您需要在dbhelper

this.db = this.getWritableDatabase(); 

否則的onCreate不會被調用這條線。請注意,你需要一個「數據庫DB」變量太多

http://www.finalconcept.com.au/article/view/android-creating-a-sqlite-database

+0

感謝您的回答,但我之前在此部分中做過: public DataBaseBON open()throws SQLException {0} {0} {0}我們的helper = new dbhelper(ourContext); ourdatabase = ourhelper.getWritableDatabase(); ourdatabase = ourhelper.getReadableDatabase(); 返回此; } – developer

0

來得有點晚,但總好過從來沒有我有這樣的問題,我使用的內容提供商,所以當你在投影表的屬性的任何屬性不投影將不會顯示在基本uri上,以糾正該問題投影表的所有屬性projectionMap.put(Tablename.Attribute1, Tablename.Attribute1) ;