Android 插入时发生sqlite异常

Android 插入时发生sqlite异常,android,android-sqlite,android-contentprovider,Android,Android Sqlite,Android Contentprovider,我已经尝试了很长时间,我的大脑已经很累了。我正在尝试通过微调器、收音机和编辑文本获取用户输入。以下是我的主要活动代码,我从中获取所有输入 public class SQLiteActivity extends Activity implements AdapterView.OnItemSelectedListener, RadioGroup.OnCheckedChangeListener { private String crop; private String flowrate; priva

我已经尝试了很长时间,我的大脑已经很累了。我正在尝试通过微调器、收音机和编辑文本获取用户输入。以下是我的主要活动代码,我从中获取所有输入

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);
}
在执行时,它会抛出一个异常

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

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

`"Limit"`    

`[Limit]`

`Limit`

此处的更多信息:

您不能在sqlite中使用“LIMIT”作为列名。有关更多信息,请访问查看您的查询和异常消息,“LIMIT”是一个sqlite关键字,用于限制SELECT语句返回的数据量。这就是为什么你会得到一个例外。它与您的列名冲突。例如,将列名更改为mylimit,它将起作用


第二个错误可能是您稍后在数据库中添加了名为“crop”的列。您可以通过更改
数据库\u版本来修复它。Limit是SQLite中的一个关键字。这就是我出现上述错误的原因。请将变量名称更改为其他名称。

2.我最初创建的表只有3列。但后来我添加了2列。在这种情况下,您可能需要清除数据,以便重新创建数据库。创建过程只在第一次执行时发生,如果数据库版本发生更改,它可以得到更新,这将转到onUpgrade方法。阅读下面Saeed的评论,查看您的查询和异常消息,“limit”是一个SQLite关键字,用于限制SELECT语句返回的数据量。这就是为什么你会得到一个例外。它与您的列名冲突。例如,将列名更改为mylimit,它将起作用。谢谢Saeed!这是一个完美的答案。但是,我有另一个错误,我已经发布编辑后。在您的数据库中没有这样的科隆名称作物,这是错误可能是您添加了一些列名称“作物”在数据库稍后。我同意Ken Wolf,你应该考虑卸载和重新安装你的应用程序。一种更好的方法是,在onUpdate方法中删除并重新创建所有表,并在每次更改模式时增加db版本。或者您可以使用私有静态final int数据库_VERSION=1;增加你的版本+1这非常有用!我改变了我的数据库版本,它工作得很好!非常感谢@saeed!
`"Limit"`    

`[Limit]`

`Limit`