2012-07-22 48 views
2

即時通訊一個新的android,我想獲取數據在db到textview。android.database.sqlite.SQLiteException:沒有這樣的列:personname:,而編譯:選擇id,personname FROM numbersTable

但它有錯誤我不能糾正這個問題。

請幫幫我。

這是DB類

public class blockornot1 { 
    String TAG; 
private dbHelper1 ourHelper; 
private final Context ourContext; 
private SQLiteDatabase ourDatabese; 

    public final static String DATABASE_NAME1="databasename.db"; 
    public final String DATABASE_TABLE="numbersTable"; 
    public final String KEY_NAME="personname"; 
    public final String KEY_ID="id"; 
    public final int DATABASE_VERSION =2; 


    public class dbHelper1 extends SQLiteOpenHelper{ 

    public dbHelper1 (Context context) { 
      super (context,DATABASE_NAME1,null,DATABASE_VERSION); 
      } 

    @Override 
    public void onCreate(SQLiteDatabase db) { 
    db.execSQL("CREATE TABLE "+ DATABASE_TABLE+ "("+KEY_ID+ " INTEGER PRIMARY KEY AUTOINCREMENT, "    
     + KEY_NAME + "TEXT NOT NULL);"); 
     Log.v(TAG, "creat"); 
    } 

    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE); 
     onCreate(db); 

    } 

    }`` 

    public blockornot1(Context c){ 

     ourContext = c; 
    } 

    public blockornot1 open() throws SQLException{ 
     ourHelper= new dbHelper1(ourContext); 
     ourDatabese= ourHelper.getWritableDatabase(); 
     return this; 
    } 

    public void close(){ 
     ourHelper.close(); 

    } 

public long creatEntry(String inputtext) { 
    // TODO Auto-generated method stub 
    ContentValues cv= new ContentValues(); 
    cv.put(KEY_NAME, inputtext); 
    Log.v(inputtext, "add to database"); 
    return ourDatabese.insert(DATABASE_TABLE, null, cv); 

} 

public String getData() { 
    // TODO Auto-generated method stub 
    String[] columns = new String[]{ KEY_ID, KEY_NAME }; 
    Cursor c=ourDatabese.query(DATABASE_TABLE, columns, null, null, null, null, null); 
    String result = ""; 
    int iRow = c.getColumnIndex(KEY_ID); 
    int iName = c.getColumnIndex(KEY_NAME); 
    for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){ 

     result= result + c.getString(iRow) + " " + c.getString(iName) + "/n"; 
    } 
    return result; 
    } 


    } 

,這是我的主類

 public class Mainclass extends Activity implements OnClickListener { 
     EditText et; 
    String TAG; 
    public void onCreate(Bundle savedInstanceState) { 
     Log.v(TAG,"onCreate"); 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     et=(EditText)findViewById(R.id.edittext1); 
     Button bt=(Button)findViewById(R.id.button1); 
     Button bt1=(Button)findViewById(R.id.button2view); 
     bt.setOnClickListener(this); 
     bt1.setOnClickListener(this); 

    } 

     public void onClick(View v) { 

      switch (v.getId()) { 
      case R.id.button1: 
       boolean dididwork =true; 
       try{ 
       String inputtext=et.getText().toString(); 
       blockornot1 entry= new blockornot1(Mainclass.this); 
       entry.open(); 
       entry.creatEntry(inputtext); 
       entry.close(); 
       Log.v(TAG," okkkkkkk hastaaaaaaa"); 
       break;} 
       catch (Exception e){ 
        String error=e.toString(); 
        Dialog d= new Dialog(this); 
        d.setTitle("Heack ya!"); 
          TextView t= new TextView(this); 
          t.setText(error); 
          d.setContentView(t); 
          d.show(); 
        dididwork=false; 

       }finally { 
       if(dididwork){ 
        Dialog d= new Dialog(this); 
        d.setTitle("Heack ya!"); 
          TextView t= new TextView(this); 
          t.setText("success"); 
          d.setContentView(t); 
          d.show(); 

       } 

       } 

      case R.id.button2view: 
       Intent i = new Intent("android.intent.action.SQLVIEW"); 
       startActivity(i); 
       break; 

      } 




        } 

}

這個類是用於顯示數據庫中的TextView

public class ShowDb extends Activity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.sqlview); 
    TextView tv=(TextView)findViewById(R.id.textViewsqlview); 
    blockornot1 info= new blockornot1(this); 
    info.open(); 
    String data= info.getData(); 
    info.close(); 
    tv.setText(data); 
} 

}

回答

4

我猜你在某些時候改變了表的create語句,但是直到你遞增數據庫版本爲止,你的應用程序不會自動實現任何改變。嘗試將DATABASE_VERSION設置爲3

加成

如果你要在你的數據庫中的信息綁定到ListView,微調等,那麼你應該改變KEY_ID_id。 (Android是講究的主鍵列的名稱。)

+0

爲了更好地理解OP,當舊的數據庫版本不匹配新的數據庫版本時,會激發'onUpgrade'方法,這在您更改數據庫結構時非常有用。您可以編寫用於將舊數據庫中的舊數據傳輸到新版本的代碼。由於你只是下降和調用創建這將確保數據庫具有最新的結構。 – 2012-07-22 23:08:49

+0

感謝您提供有用的信息,我按照您所說的做了,但仍然存在問題,新錯誤: 引起:java.lang.IllegalArgumentException:列'_id'不存在 – developer 2012-11-25 18:14:29

+0

您有兩種選擇:1)將KEY_ID更改爲''_id「'(而不是''id」')並通過設置'DATABASE_VERSION = 4'來重建數據庫。 **或** 2)在此行中添加''as _id「':'String [] columns = new String [] {KEY_ID as _id,KEY_NAME};'。 – Sam 2012-11-25 18:19:19

0

你有兩個選擇:

1)更改KEY_ID_id(不id),並通過設置DATABASE_VERSION = 4重建數據庫。

或者

2)添加as _id這一行:String[] columns = new String[]{ KEY_ID as _id, KEY_NAME };

我現在有同樣的問題,以顯示我的數據庫中的信息到一個ListView,所以我遵循'山姆'給出的這些指示,這真的解決了我的問題。非常感謝Sam。

相關問題