插入时出现sqlite异常

sqlite exception while inserting

我已经尝试了很长时间,我的大脑很累 already.I 我正在尝试通过微调器、收音机和 edittext.The 获取用户输入,以下是我的主要 activity我获得所有输入的代码。

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 class)

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 (?,?,?,?,?)

非常感谢您 answers.I 将变量名称更改为绑定,现在它不显示 error.But 我有一个不同的。

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 (?,?,?,?,?)

Limit 是一个 SQL 保留字,尝试更改它应该可以正常工作。 如果你不想改变它,你可以使用:

`"Limit"`    

`[Limit]`

`Limit`

更多信息在这里:https://www.sqlite.org/lang_keywords.html

您不能将 "LIMIT" 用作 sqlite 中的列名称以获取更多信息,请访问 http://www.sqlite.org/lang_keywords.html

查看您的查询和异常消息,"limit" 是一个 SQLite 关键字,用于限制 SELECT 语句返回的数据量。这就是您获得异常的原因。它与您的列名发生冲突。例如把列名改成mylimit就可以了

第二个错误可能是您稍后在数据库中添加了列名 "crop"。您可以通过更改 database_version

来修复它

1.Limit 是 SQLite.That 中的关键字,这就是为什么我将上面的 error.Change 变量名称改为其他名称的原因。 2.I 最初只用 3 个 columns.But 创建了 table 然后我添加了 2 columns.In 这种情况你可能需要做一个清除数据以便再次创建数据库。创建过程仅在第一次执行时发生,如果数据库版本发生变化,它可以获得更新,这将转到 onUpgrade method.Read Saeed 在下面的评论