无法使用4列SQLite Android Studio创建表

时间:2015-07-14 06:08:03

标签: android database sqlite

我是Android编程中非常愚蠢的新手, 在这里,我想在Android工作室制作一个Cashflow笔记应用程序。 在这种情况下,我想创建一个表调用Category_Table与4列名为CategId,CategName,Note和Currency。 我从谷歌和其他地方做了教程。但直到现在我仍然有这个问题。 当我想将一些数据插入到Category_Table中时,它表示在Android Studio报告中没有名为Currency的列。 我不知道该怎么做,我谷歌搜索它,但仍然无法解决这个问题。 请给我一个可以理解为新手程序员的答案。 谢谢。 NB。这是我的代码: Add_Category代码:

    public class AddCategory extends ActionBarActivity {
    private static Button BtnIAdd;
    private static Button BtnICancel;
    EditText txtcategname, txtnote;
    Spinner selectCurrency;
    ArrayAdapter<CharSequence> adapterCurrency;
    DatabaseHelper myDB;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_category);
        myDB = new DatabaseHelper(this);
        txtcategname = (EditText)findViewById(R.id.editText);
        txtnote = (EditText)findViewById(R.id.editText2);
        BtnICancel = (Button)findViewById(R.id.btnCancel);
        BtnIAdd = (Button)findViewById(R.id.btnAdd);

        //spinner
        selectCurrency = (Spinner) findViewById(R.id.spin_selectCurrency);
        adapterCurrency = ArrayAdapter.createFromResource(this, R.array.CurrencyName,android.R.layout.simple_spinner_item );
        adapterCurrency.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        selectCurrency.setAdapter(adapterCurrency);
        selectCurrency.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(getBaseContext(), parent.getItemAtPosition(position)+" selected",Toast.LENGTH_LONG).show();
                String currencyValue = String.valueOf(parent.getSelectedItem());
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        addCategData();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_add_category, menu);
        return true;
    }

    public void addCategData(){
        BtnIAdd.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(AddCategory.this,"Clicked", Toast.LENGTH_LONG).show();
                      boolean isInserted =   myDB.insertCategData(txtcategname.getText().toString(),
                                txtnote.getText().toString(), selectCurrency.getSelectedItem().toString());
                        if (isInserted == true)
                            Toast.makeText(AddCategory.this,"Inserted", Toast.LENGTH_LONG).show();
                        else
                            Toast.makeText(AddCategory.this,"Not Inserted", Toast.LENGTH_LONG).show();
                    }
                }
        );
        BtnICancel.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        finish();
                    }
                }
        );
    }



    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
}
}

这是DatabaseHelper Class的这个

public class DatabaseHelper extends SQLiteOpenHelper {
public static final String MyVillageSoftware = "MyVillageSoftware";
public static final String DATABASE_NAME = "Cashflow.db";
public static final String TABLE_Categ_NAME = "category_table";
public static final String COL1 = "CategId";
public static final String COL2 = "CategName";
public static final String COL3 = "Note";
public static final String COL4 = "Currency";



public DatabaseHelper(Context context) {
    super(context, DATABASE_NAME, null, 1);


}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("Drop Table" + TABLE_Categ_NAME);
    /*db.execSQL("Create table " + TABLE_Categ_NAME +
            "(CategID Integer PRIMARY KEY AUTOINCREMENT " +
            "CategName Text" +
            "Note Text" +
            "Currency Text)");*/

}

public boolean insertCategData(String categname, String note, String currency){
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(COL2, categname);
    contentValues.put(COL3, note);
    contentValues.put(COL4, currency);
    long result = db.insert(TABLE_Categ_NAME, null, contentValues);
     if (result == -1)
         return true;
     else
         return false;

}
public ArrayList<String>getAllCategory(){
    ArrayList<String> AllCategoryList = new ArrayList<String>();
    SQLiteDatabase db = this.getReadableDatabase();
    String selectCateg="Select * FROM " +TABLE_Categ_NAME;
    Cursor cursor = db.rawQuery(selectCateg, null);
    if(cursor.getCount()>0){
        while (cursor.moveToNext()){
            String categname=cursor.getString(cursor.getColumnIndex(COL2));
            AllCategoryList.add(COL2);

        }return AllCategoryList;
    }

    return AllCategoryList;

}


@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " +TABLE_Categ_NAME);
    onCreate(db);

之前感谢... :)

1 个答案:

答案 0 :(得分:1)

1)卸载应用程序并重新安装。

2)问题是&#34; Android Studio报告中没有名为Currency的列&#34; 这是因为在创建表后可能会添加列货币。所以你必须定义数据库版本(一个整数值,例如Ver = 1),并在数据库中进行更改时更改数据库的版本(增加int值,例如Ver = 2),这样它将调用 onUpgrade() 并删除表格并再次创建。这将解决您的问题。

编辑:你已经在超级(context,dbName,cursorFactory,dbVersion)中将数据库版本1作为默认值

public DatabaseHelper(Context context) {
    super(context, DATABASE_NAME, null, 1);
}