2016-04-07 7 views
0

を挿入しながら、以下のラジオとedittext.Theは、私はすべての入力を取得し、私のメインの活動コードです。以下はsqliteの例外私は長い時間のためにこれをしようとしていると私、私の脳が疲れてalready.Iはスピナーを介してユーザー入力を取得しようとしていますです

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

おかげで多くのことを言って例外をスロー実行中変数名をboundに変更しましたが、今はエラーが表示されません。しかし、私は別のものを持っています。

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

「制限」はSELECTステートメントによって返されるデータ量を制限するために使用されるSQLiteキーワード。そういうわけで、例外を取得しています。列名と競合しています。たとえば、カラム名をmylimitに変更すると、それが動作します。 – saeed

+0

ありがとうございました!それは完璧な答えでした。しかし、投稿を編集して投稿した別のエラーがありました。 – Shrei

+0

あなたのデータベースには、エラー – saeed

答えて

0

"limit"は、SELECTステートメントによって返されるデータ量を制限するために使用されるSQLiteキーワードです。そういうわけで、例外を取得しています。列名と競合しています。例えば、mylimitに列名を変更し、それが

を動作し、2番目のエラーは、後でデータベース内の列名「作物」を添加してもよいです。あなたはdatabase_version

0

Limitそれを変更すると、それがうまく動作するはずですしてみてください、SQLの予約語です。あなたはそれを変更したくない 場合は、使用することができます。

`"Limit"`  

`[Limit]` 

`Limit` 
ここ

さらに詳しい情報:https://www.sqlite.org/lang_keywords.html

0

1.Limitを変更することにより、それを修正することができ、私は何か他のものにerror.Change上記の変数名を得た理由SQLite.Thatのキーワードがあるです。 2.最初は3つのカラムだけでテーブルを作成しましたが、その後2つのカラムを追加しました。そのような場合、データベースを再作成できるようにクリアデータを作成する必要があります。作成するプロシージャは最初の実行でのみ発生し、データベースのバージョンが変更されていれば更新を取得できます。これはonUpgradeメソッドに移ります。Go Here

関連する問題