2016-04-07 118 views
0

我一直在嘗試這個很長一段時間,我的大腦已經疲憊不堪。我試圖通過微調,無線電和edittext獲得用戶輸入。以下是我的主要活動代碼,其中獲取所有輸入。sqlite異常插入時

public class SQLiteActivity extends Activity implements AdapterView.OnItemSelectedListener, RadioGroup.OnCheckedChangeListener { 

private String crop; 
private String flowrate; 
private String soil; 
private String threshold; 
private String limit; 
SqliteHelper sqliteHelper; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_shared); 
    crop = ""; 
    flowrate = ""; 
    soil = ""; 
    threshold=""; 
    limit=""; 
    Spinner spinnerZodiac = (Spinner) findViewById(R.id.spinnerZodiac); 
    spinnerZodiac.setOnItemSelectedListener(this); 
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, 
      R.array.soil, android.R.layout.simple_spinner_item); 
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
    spinnerZodiac.setAdapter(adapter); 

    RadioGroup radioGroupGender = (RadioGroup) findViewById(R.id.radioGroupFlowrate); 
    radioGroupGender.setOnCheckedChangeListener(this); 

    sqliteHelper = new SqliteHelper(this); 
} 

@Override 
public void onCheckedChanged(RadioGroup radioGroup, int i) { 
    int radioButtonId = radioGroup.getCheckedRadioButtonId(); 
    RadioButton radioButton = (RadioButton)radioGroup.findViewById(radioButtonId); 
    flowrate = radioButton.getText().toString(); 
} 
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 
    soil = parent.getItemAtPosition(position).toString(); 
} 

public void onNothingSelected(AdapterView<?> parent) { 
    // An interface callback 
} 

public void save(View view) { 
    crop = ((EditText)findViewById(R.id.txtCrop)).getText().toString(); 
    threshold = ((EditText)findViewById(R.id.txtThreshold)).getText().toString().trim(); 
    limit= ((EditText)findViewById(R.id.txtLimit)).getText().toString().trim(); 
    if (crop.isEmpty()){ 
     Toast.makeText(getApplicationContext(), "Crop type cannot be empty!", Toast.LENGTH_LONG).show(); 
     return; 
    } 
    boolean result = sqliteHelper.saveUser(crop, flowrate, soil,threshold,limit); 
    if (result){ 
     Toast.makeText(getApplicationContext(), "You have been saved!", Toast.LENGTH_LONG).show(); 
    } else { 
     Toast.makeText(getApplicationContext(), "Failed to save!", Toast.LENGTH_LONG).show(); 
    } 
} 
public void retrieve(View view) { 
    crop = ((EditText)findViewById(R.id.txtCrop)).getText().toString(); 
    Cursor cursor = sqliteHelper.getUser(crop); 
    if (cursor.getCount() != 0) { 
     cursor.moveToFirst(); 
     crop = cursor.getString(cursor.getColumnIndex("crop")); 
     flowrate= cursor.getString(cursor.getColumnIndex("flowrate")); 
     soil = cursor.getString(cursor.getColumnIndex("soil")); 
     threshold=cursor.getString(cursor.getColumnIndex("threshold")); 
     limit=cursor.getString(cursor.getColumnIndex("limit")); 
     if (!cursor.isClosed()) { 
      cursor.close(); 
     } 
     setUI(); 
    } 
} 
protected void setUI(){ 
    ((TextView)findViewById(R.id.txtCrop)).setText(crop); 
    if (flowrate.equals("50")){ 
     ((RadioButton)findViewById(R.id.radMale)).setChecked(true); 
    } else if (flowrate.equals("60")){ 
     ((RadioButton)findViewById(R.id.radFemale)).setChecked(true); 
    } 
    Resources resource = getResources(); 
    String[] zodiacArray = resource.getStringArray(R.array.soil); 

    for(int i = 0; i < zodiacArray.length; i++){ 
     if(zodiacArray[i].equals(soil)){ 
      ((Spinner)findViewById(R.id.spinnerZodiac)).setSelection(i); 
     } 
    } 
} 
@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    getMenuInflater().inflate(R.menu.sqlite, menu); 
    return true; 
} 
@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    int id = item.getItemId(); 
    if (id == R.id.action_settings) { 
     return true; 
    } 
    return super.onOptionsItemSelected(item); 
} 

下面是保存在SQLite數據庫中輸入的內容的功能(sqlitehelper類)

public boolean saveDetail(String crop, String flowrate,String soil,String threshold,String limit) 
{ 
    Cursor cursor = getUser(crop); 
    SQLiteDatabase db = this.getWritableDatabase(); 
    ContentValues contentValues = new ContentValues(); 
    contentValues.put("flowrate", flowrate); 
    contentValues.put("soil", soil); 
    contentValues.put("threshold", threshold); 
    contentValues.put("limit", limit); 
    long result; 
    if (cursor.getCount() == 0) { 
     contentValues.put("crop", crop); 
     result = db.insert("configuration", null, contentValues); 
    } else { 
     result = db.update("configuration", contentValues, "crop=?", new String[] { crop }); 
    } 
    if (result == -1) { 
     return false; 
    } else { 
     return true; 
    } 
} 
public Cursor getDetail(String crop){ 
    SQLiteDatabase db = this.getReadableDatabase(); 
    String sql = "SELECT * FROM configuration WHERE crop=?"; 
    return db.rawQuery(sql, new String[] { crop }); 
} 

但在執行它拋出一個異常說

Error inserting crop=tea threshold=10 soil=Alluvial limit=20 flowrate=15 
                    android.database.sqlite.SQLiteException: near "limit": syntax error (code 1): , while compiling: INSERT INTO configuration(crop,threshold,soil,limit,flowrate) VALUES (?,?,?,?,?) 

非常感謝您答案。我將變量名更改爲綁定,現在它不顯示該錯誤。但我得到了另一個。

Error inserting crop=un threshold=11 bound=111 soil=Alluvial flowrate=50 
                     android.database.sqlite.SQLiteException: table configuration has no column named threshold (code 1): , while compiling: INSERT INTO configuration(crop,threshold,bound,soil,flowrate) VALUES (?,?,?,?,?) 
+0

查看您的查詢和異常消息,「limit」是用於限制SELECT語句返回的數據量的SQLite關鍵字。這就是爲什麼你得到一個例外。它與您的專欄名稱發生衝突。例如,將列名更改爲mylimit,它將起作用。 – saeed

+0

謝謝Saeed!這是一個完美的答案。但是,我通過編輯帖子發現了另一個錯誤。 – Shrei

+0

在你的數據庫沒有這樣的colom名稱作物是錯誤 – saeed

回答

0

查看您的查詢和異常消息,「limit」是用於限制SELECT語句返回的數據量的SQLite關鍵字。這就是爲什麼你得到一個例外。它與您的專欄名稱發生衝突。例如,將列名更改爲mylimit,並且它將起作用

第二個錯誤可能是您稍後在數據庫中添加了列名「crop」。你可以通過更改來修復它database_version

0

1.Limit是SQLite中的一個關鍵字。這就是我得到上述錯誤的原因。將變量名更改爲其他名稱。 2.我首先創建了表格,只有3列。但是我添加了2列。在這種情況下,您可能需要做一個清晰的數據,以便可以重新創建數據庫。創建過程僅在第一次執行時發生,如果數據庫版本發生更改,它可以得到更新,並轉到onUpgrade方法。閱讀Saeed的評論Go Here