2015-02-07 206 views
2

我做了一個三列的android數據庫,但是當我嘗試保存它給了我這個錯誤。Android SQLite數據庫錯誤插入列

這是我的logcat。

02-07 06:02:04.762  877-877/tubapps.datepickerdb E/SQLiteLog﹕ (1) table incomes has no column named date 
02-07 06:02:04.832  877-877/tubapps.datepickerdb E/SQLiteDatabase﹕ Error inserting amount=123 date=4/2/2015 payer=McDonald's 
    android.database.sqlite.SQLiteException: table incomes has no column named date (code 1): , while compiling: INSERT INTO incomes(amount,date,payer) VALUES (?,?,?) 
      at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method) 
      at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882) 
      at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493) 
      at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588) 
      at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58) 
      at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31) 
      at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1467) 
      at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1339) 
      at tubapps.datepickerdb.DBHelper.insertIncome(DBHelper.java:59) 
      at tubapps.datepickerdb.IncomeActivity.run(IncomeActivity.java:188) 
      at java.lang.reflect.Method.invokeNative(Native Method) 
      at java.lang.reflect.Method.invoke(Method.java:511) 
      at android.view.View$1.onClick(View.java:3586) 
      at android.view.View.performClick(View.java:4084) 
      at android.view.View$PerformClick.run(View.java:16966) 
      at android.os.Handler.handleCallback(Handler.java:615) 
      at android.os.Handler.dispatchMessage(Handler.java:92) 
      at android.os.Looper.loop(Looper.java:137) 
      at android.app.ActivityThread.main(ActivityThread.java:4745) 
      at java.lang.reflect.Method.invokeNative(Native Method) 
      at java.lang.reflect.Method.invoke(Method.java:511) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
      at dalvik.system.NativeStart.main(Native Method) 

這是我的DBHelper

public class DBHelper extends SQLiteOpenHelper { 

    public static final String DATABASE_NAME = "MyDBName.db"; 
    public static final String INCOME_TABLE_NAME = "incomes"; 
    public static final String INCOME_COLUMN_ID = "id"; 
    public static final String INCOME_COLUMN_AMOUNT = "amount"; 
    public static final String INCOME_COLUMN_PAYER = "payer"; 
    public static final String INCOME_COLUMN_DATE = "date"; 


    private HashMap hp; 

    public DBHelper(Context context) 
    { 
     super(context, DATABASE_NAME , null, 1); 
    } 

    @Override 
    public void onCreate(SQLiteDatabase db) { 
     // TODO Auto-generated method stub 
     db.execSQL(
       "create table incomes " + 
         "(id integer primary key, amount text, payer text, date text)" 
     ); 
    } 

    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     // TODO Auto-generated method stub 
     db.execSQL("DROP TABLE IF EXISTS incomes"); 
     onCreate(db); 
    } 

    public boolean insertIncome (String amount, String payer, String date) 
    { 
     SQLiteDatabase db = this.getWritableDatabase(); 
     ContentValues contentValues = new ContentValues(); 

     contentValues.put("amount", amount); 
     contentValues.put("payer", payer); 
     contentValues.put("date", date); 

     db.insert("incomes", null, contentValues); 
     return true; 
    } 
    public Cursor getData(int id){ 
     SQLiteDatabase db = this.getReadableDatabase(); 
     Cursor res = db.rawQuery("select * from incomes where id="+id+"", null); 
     return res; 
    } 
    public int numberOfRows(){ 
     SQLiteDatabase db = this.getReadableDatabase(); 
     int numRows = (int) DatabaseUtils.queryNumEntries(db, INCOME_TABLE_NAME); 
     return numRows; 
    } 
    public boolean updateIncome (Integer id, String amount, String payer, String date) 
    { 
     SQLiteDatabase db = this.getWritableDatabase(); 
     ContentValues contentValues = new ContentValues(); 
     contentValues.put("amount", amount); 
     contentValues.put("payer", payer); 
     contentValues.put("date", date); 
     db.update("incomes", contentValues, "id = ? ", new String[] { Integer.toString(id) }); 
     return true; 
    } 

    public Integer deleteIncome (Integer id) 
    { 
     SQLiteDatabase db = this.getWritableDatabase(); 
     return db.delete("incomes", 
       "id = ? ", 
       new String[] { Integer.toString(id) }); 
    } 
    public ArrayList getAllIncomes() 
    { 
     ArrayList array_list = new ArrayList(); 
     //hp = new HashMap(); 
     SQLiteDatabase db = this.getReadableDatabase(); 
     Cursor res = db.rawQuery("select * from incomes", null); 
     res.moveToFirst(); 
     while(res.isAfterLast() == false){ 
      array_list.add(res.getString(res.getColumnIndex(INCOME_COLUMN_PAYER))); 
      res.moveToNext(); 
     } 
     return array_list; 
    } 

} 

這是我的活動。

public class IncomeActivity extends ActionBarActivity { 

    int from_Where_I_Am_Coming = 0; 
    private DBHelper mydb; 
    TextView payer; 
    TextView amount; 
    TextView date; 
    int id_To_Update = 0; 

    private DatePicker datePicker; 
    private Calendar calendar; 
    private TextView dateView; 
    private int year, month, day; 



    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_income); 

     payer = (TextView) findViewById(R.id.input_payer); 
     amount = (TextView) findViewById(R.id.input_amount); 
     date = (TextView) findViewById(R.id.input_date); 

     dateView = (TextView) findViewById(R.id.input_date); 
     calendar = Calendar.getInstance(); 
     year = calendar.get(Calendar.YEAR); 
     month = calendar.get(Calendar.MONTH); 
     day = calendar.get(Calendar.DAY_OF_MONTH); 
     showDate(year, month+1, day); 



     mydb = new DBHelper(this); 

     Bundle extras = getIntent().getExtras(); 
     if (extras != null) { 
      int Value = extras.getInt("id"); 
      if (Value > 0) { 
       //means this is the view part not the add contact part. 
       Cursor rs = mydb.getData(Value); 
       id_To_Update = Value; 
       rs.moveToFirst(); 
       String amo = rs.getString(rs.getColumnIndex(DBHelper.INCOME_COLUMN_AMOUNT)); 
       String pyr = rs.getString(rs.getColumnIndex(DBHelper.INCOME_COLUMN_PAYER)); 
       String dat = rs.getString(rs.getColumnIndex(DBHelper.INCOME_COLUMN_DATE)); 
       if (!rs.isClosed()) { 
        rs.close(); 
       } 
       Button save = (Button) findViewById(R.id.btn_save); 
       save.setVisibility(View.INVISIBLE); 

       Button cancel = (Button) findViewById(R.id.btn_cnc); 
       cancel.setVisibility(View.INVISIBLE); 

       amount.setText((CharSequence) amo); 
       amount.setFocusable(false); 
       amount.setClickable(false); 

       payer.setText((CharSequence) pyr); 
       payer.setFocusable(false); 
       payer.setClickable(false); 

       date.setText((CharSequence) dat); 
       date.setFocusable(false); 
       date.setClickable(false); 

      } 
     } 
    } 


    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     Bundle extras = getIntent().getExtras(); 
     if (extras != null) { 
      int Value = extras.getInt("id"); 
      if (Value > 0) { 
       getMenuInflater().inflate(R.menu.menu_income, menu); 
      } else { 
       getMenuInflater().inflate(R.menu.menu_main, menu); 
      } 
     } 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     switch (item.getItemId()) { 
      case R.id.Edit_Income: 
       Button save = (Button) findViewById(R.id.btn_save); 
       save.setVisibility(View.VISIBLE); 
       Button cancel = (Button) findViewById(R.id.btn_cnc); 
       cancel.setVisibility(View.VISIBLE); 

       amount.setEnabled(true); 
       amount.setFocusableInTouchMode(true); 
       amount.setClickable(true); 

       payer.setEnabled(true); 
       payer.setFocusableInTouchMode(true); 
       payer.setClickable(true); 

       date.setEnabled(true); 
       date.setFocusableInTouchMode(true); 
       date.setClickable(true); 

       return true; 
      case R.id.Delete_Income: 

       AlertDialog.Builder builder = new AlertDialog.Builder(this); 
       builder.setMessage(R.string.deleteIncome) 
         .setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialog, int id) { 
           mydb.deleteIncome(id_To_Update); 
           Toast.makeText(getApplicationContext(), "Deleted Successfully", Toast.LENGTH_SHORT).show(); 
           Intent intent = new Intent(getApplicationContext(), tubapps.datepickerdb.MainActivity.class); 
           startActivity(intent); 
          } 
         }) 
         .setNegativeButton(R.string.no, new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialog, int id) { 
           // User cancelled the dialog 
          } 
         }); 
       AlertDialog d = builder.create(); 
       d.setTitle("Are you sure"); 
       d.show(); 

       return true; 

      default: 
       return super.onOptionsItemSelected(item); 
     } 
    } 

    public void run(View view) { 
     Bundle extras = getIntent().getExtras(); 
     if (extras != null) { 
      int Value = extras.getInt("id"); 
      if (Value > 0) { 
       if (mydb.updateIncome(id_To_Update, amount.getText().toString(), payer.getText().toString(), date.getText().toString())) { 
        Intent intent = new Intent(getApplicationContext(), tubapps.datepickerdb.MainActivity.class); 
        startActivity(intent); 
       } else { 
        Toast.makeText(getApplicationContext(), "not Updated", Toast.LENGTH_SHORT).show(); 
       } 
      } else { 
       if (mydb.insertIncome(amount.getText().toString(), payer.getText().toString(), date.getText().toString())) { 
       } else { 
       } 
       Intent intent = new Intent(getApplicationContext(), tubapps.datepickerdb.MainActivity.class); 
       startActivity(intent); 
      } 
     } 
    } 

    @SuppressWarnings("deprecation") 
    public void setDate(View view) { 
     showDialog(999); 
    } 

    @Override 
    protected Dialog onCreateDialog(int id) { 
     // TODO Auto-generated method stub 
     if (id == 999) { 
      return new DatePickerDialog(this, myDateListener, year, month, day); 
     } 
     return null; 
    } 

    private DatePickerDialog.OnDateSetListener myDateListener 
      = new DatePickerDialog.OnDateSetListener() { 

     @Override 
     public void onDateSet(DatePicker arg0, int arg1, int arg2, int arg3) { 
      // TODO Auto-generated method stub 
      // arg1 = year 
      // arg2 = month 
      // arg3 = day 
      showDate(arg1, arg2+1, arg3); 
     } 
    }; 

    private void showDate(int year, int month, int day) { 
     dateView.setText(new StringBuilder().append(day).append("/") 
       .append(month).append("/").append(year)); 
    } 

} 

有人知道爲什麼我得到這個錯誤,因爲我不明白爲什麼。我已經插入了該列。

+1

首先檢查您的列名稱是否正確?如果是,那麼卸載你的應用程序並清理你的項目並重新運行。 – Piyush 2015-02-07 04:24:03

回答

1

很可能date列後面加incomes表查詢。

所以嘗試通過不同版本:

super(context, DATABASE_NAME , null, 2); 
+0

@GeorgeZoiade:歡迎喬治。我很高興我能夠幫助:) – 2015-02-07 04:41:30