即使列在那里,数据库也会显示缺少列

时间:2017-04-02 21:09:04

标签: android database sqlite listview android-sqlite

编辑:我发现原因是我的数据库中没有列名为Col_Name的错误,但是我确实将列编码到数据库中,但它告诉我它不存在。

错误讯息: 错误代码:1(SQLITE_ERROR)                                                                        引起:SQL(查询)错误或缺少数据库。                                                                         (表Expense_Table没有名为Name(code 1)的列:,编译时:INSERT INTO Expense_Table(Am,Name)VALUES(?,?))

我有一个包含2列的数据库。我将输入保存到2列,然后将它们加载到列表视图中。我已经成功完成了1列,它完美无缺。当我添加第二列并加载应用程序时,我的Toast消息告诉我我的数据库是空的。有关为什么会发生这种情况的任何帮助?

我的主要活动(仅限相关部分):

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    listView = (ListView)findViewById(R.id.listView);
    if (android.os.Build.VERSION.SDK_INT >= 21) {
        Window window = this.getWindow();
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        window.setStatusBarColor(this.getResources().getColor(R.color.colorPrimary));
    }
    //Code for Ads
    mAdView = (AdView) findViewById(R.id.adView);
    AdRequest adRequest = new AdRequest.Builder().build();
    mAdView.loadAd(adRequest);

    nameList = new ArrayList<>();
    expenseList = new ArrayList<>();
    Row = new ArrayList<>();
    adapter = new CustomAdapter(this, Row);
    listView.setAdapter(adapter);

    //Get Database to populate Listview
    myDb = new DBHelper(this);
    Cursor data = myDb.getListContent();
    if(data.getCount() == 0){
        Toast.makeText(this, "Data NOT loaded", Toast.LENGTH_SHORT).show();
    } else{
        while(data.moveToNext()){
            String dbAmount = data.getString(1);
            String dbName = data.getString(2);
            ExRow exRow = new ExRow(dbAmount, dbName);
            expenseList.add(dbAmount);
            Row.add(exRow);
            adapter.notifyDataSetChanged();
            Toast.makeText(this, "Data loaded", Toast.LENGTH_SHORT).show();
        }
    }

这是我的用于检索和保存数据的数据库类:

private static final String DATABASE_NAME = "Expenses.db";
    public static final String TABLE_NAME = "Expense_Table";
    public static final String Col_ID = "id";
    public static final String Col_AMOUNT = "Am";
    public static final String Col_NAME = "Name";

   //public  static final String Col_Date = "date";



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

    }

    @Override
    //Set up database here
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("Create table " + TABLE_NAME +
                "(ID INTEGER PRIMARY KEY AUTOINCREMENT, AM TEXT, Name TEXT)");

    }

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

    }


public boolean saveData(String Am, String Name) {
   SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(Col_AMOUNT, Am);
    contentValues.put(Col_NAME, Name);


    long result = db.insert(TABLE_NAME, null, contentValues);
    if(result == -1){
        return false;
    }else {
        return true;
    }
}
public Cursor getListContent(){
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor data = db.rawQuery("SELECT * FROM " + TABLE_NAME, null);
    return data;
}

我的MainActivity还有更多内容可以将输入添加到数据库中。我认为粘贴代码太简单了。它只是saveData(String,String);

2 个答案:

答案 0 :(得分:0)

我找到了答案。这是因为我在创建数据库之后添加了该列。我更改了数据库的名称,但它确实有效。

答案 1 :(得分:-1)

我认为您应该使用do while循环来迭代数据库中的数据。

if (cursor.moveToFirst()) {
        do {
           String dbAmount = cursor.getString(1);
           String dbName = cursor.getString(2);
            // Adding your data to holder
            ExRow exRow = new ExRow(dbAmount, dbName);
        expenseList.add(dbAmount);
        Row.add(exRow);
        } while (cursor.moveToNext());
    }

也无需一直通知您的列表,因此请删除该代码。

快乐的编码。 :-)我希望它适合你。