2013-05-11 98 views
0

我试图做一个插入到这个数据库,我会得到一个错误。任何想法,我会做错什么?这是一个空的异常错误,当我尝试和加载这个片段。SQLite数据库问题

助手类:

@Override 
public void onClick(View v) { 
    sqlDate = (EditText) getView().findViewById(R.id.dateselected); 
    sqlTemperature = (EditText) getView().findViewById(R.id.tempvalue); 
    sqlFertile = (Switch) getView().findViewById(R.id.fertileswitch); 
    sqlNotes = (EditText) getView().findViewById(R.id.chartingnote); 

    switch (v.getId()) { 
     case R.id.chartingupdate: 

      boolean success = true; 

      try{ 
      String date = sqlDate.getText().toString(); 
      String temperature = sqlTemperature.getText().toString(); 
      String fertile = sqlFertile.getText().toString(); 
      String notes = sqlNotes.getText().toString(); 

      SQLHelper entry = new SQLHelper(getActivity()); 

      entry.open(); 
      entry.createEntry(date, temperature, fertile, notes); 
      entry.close(); 
      } catch (Exception e) { 
       success = false; 

       String err_msg = e.toString(); 
       Dialog d = new Dialog(getActivity()); 
       d.setTitle("Error Inserting"); 
       TextView tv = new TextView(getActivity()); 
       tv.setText(err_msg); 
       d.setContentView(tv); 
       d.show(); 

      } finally { 
       if (success) { 
        Context context = getActivity(); 
        Toast.makeText(context, "Charting Successful", Toast.LENGTH_LONG).show(); 
       } else { 
        Context context = getActivity(); 
        Toast.makeText(context, "Charting Unsuccessful", Toast.LENGTH_LONG).show(); 
       } 
      } 

      break; 
     case R.id.pickdate: 
      Context context2 = getActivity(); 
      Toast.makeText(context2, "Hello toast 2!", Toast.LENGTH_LONG).show(); 
      break; 
    } 

} 

编辑::

public SQLHelper open() throws SQLException{ 
    ourHelper = new DbHelper(ourContext); 
    ourDatabase = ourHelper.getWritableDatabase(); 
    return this; 
} 

错误说: 05-12 19:56:23.218:在另一个类中

@Override 
    public void onCreate(SQLiteDatabase db) { 
     db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" + 
            KEY_ROWID + " INTEGER AUTOINCREMENT, " + 
            KEY_CHARTING_DATE + " TEXT NOT NULL, " + 
            KEY_NAME + " TEXT, " + 
            KEY_CHARTING_TEMPERATURE + " INTEGER, " + 
            KEY_CHARTING_STAMPS + " INTEGER, " + 
            KEY_CHARTING_FERTILE + " TEXT, " + 
            KEY_CHARTING_NOTES + " TEXT, " + 
            KEY_CHARTING_PROC + ", " + 
            "PRIMARY KEY (" + KEY_ROWID + ", " + KEY_CHARTING_DATE + ")); " 
       ); 
    } 

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

public long createEntry(String date, String temperature, String fertile, String notes) { 
    ContentValues cv = new ContentValues(); 
    cv.put(KEY_CHARTING_DATE, date); 
    cv.put(KEY_CHARTING_TEMPERATURE, temperature); 
    cv.put(KEY_CHARTING_FERTILE, fertile); 
    cv.put(KEY_CHARTING_NOTES, notes); 
    return ourDatabase.insert(DATABASE_TABLE, null, cv); 

} 

更新按钮E/SQLiteLog(791):(1)在“AUTOINCREMENT”附近:语法错误

问题是我的表格创建.... PK是在两列的。一个AUTOINCREMENT和另一个日期字段。由于某种原因,它不喜欢它。如果我将PK更改为只有那一个AUTOINCREMENT列,它会工作......猜猜我必须将其设置为日期字段。

+0

这将有助于 – 2013-05-11 17:46:44

+1

'ourDatabase'是否已正确初始化? – laalto 2013-05-11 17:49:12

+0

请发布您的日志,以便我们可以清楚了解您的实际错误。 – Rekha 2013-05-11 18:02:00

回答

1

需要声明KEY_ROWID如果您发布您的堆栈跟踪为主要

KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " 
0

你不提供,你初始化ourDatabase,但如果你dind't做正确的代码,它必须是:

SQLiteDatabse ourDatabase = getWritableDatabase(); 

还与所有字段发送到调试器检查createEntry不为空(date, temperature, fertile, notes

+0

见编辑...它有公开的 – jcaruso 2013-05-12 19:32:59

+0

P.S.为什么它们都必须不为零?在数据库定义中它只指定一个不为null。 – jcaruso 2013-05-12 19:34:16