不能创建表具有4列的SQLite的Andr​​oid工作室 [英] Can't Create Table With 4 Column SQLite Android Studio

查看:257
本文介绍了不能创建表具有4列的SQLite的Andr​​oid工作室的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Android的编程非常非常愚蠢的新手,
在这里,我想在机器人工作室现金流笔记应用程序。
在这种情况下,我想创建一个名为CategId,CategName,票据及货币4列的表调用Category_Table。
我一直在做从谷歌等extacly教程。但是直到现在我仍然有这个问题。
当我要插入一些数据Category_Table,它说没有货币在Android中Studio报表命名列。
我不知道该怎么办,我已经Google上搜寻它,但不能仍然解决这个问题。
请给我一个答案,我可以理解作为一个新手程序员。
谢谢。
NB。这里是我的code:
Add_Category code:

i am a very very dumb newbie in Android Programming, here i want to make a Cashflow note app in android studio. In this case i want to create a table call Category_Table with 4 column called CategId, CategName, Note and Currency. I have doing the tutorial from google and etc extacly. But till now i still have this problem. When i want to insert some data into Category_Table, it says No Column named Currency in Android Studio report. I dont know what to do, i have googling it but still cant solve this problem. Please gimme an answer that i can understand as a newbie programmer. Thanks. NB. Here is my code: Add_Category code :

    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类

And this one for 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);

由于前...:)

thanks before... :)

推荐答案

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

1) Uninstall the App and Install again.

2)问题的没有名为Android Studio中报告货币列的,这是因为列的货币可能会创建的表后加入。所以,你必须定义数据库版本(一个整数值,如版本= 1),并更改数据库的版本,只要你做数据库的变动(增加了int值如版本= 2),所以它会调用的 onUpgrade() 的和删除表,并重新创建它。这将解决您的问题。

2) issue is "No Column named Currency in Android Studio report" this is because column currency might be added after table created. so for that you have to define Database Version(an integer value e.g. Ver=1) and change the version of DB whenever you do changes in database(increase that int value e.g. Ver=2) so it will call onUpgrade() and drop your tables and create it again. this will solve your problem.

编辑:你给该数据库第1版默认超(背景下,DBNAME,cursorFactory,dbVersion)

You have given that Database version 1 as default in super(context,dbName,cursorFactory,dbVersion)

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

这篇关于不能创建表具有4列的SQLite的Andr​​oid工作室的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆