【问题标题】:Database says missing column even though column is there [duplicate]数据库说缺少列,即使列存在[重复]
【发布时间】:2017-04-02 21:09:04
【问题描述】:

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

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

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

我的 MainActivity(仅相关部分):

@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);

【问题讨论】:

    标签: android database sqlite listview android-sqlite


    【解决方案1】:

    我找到了答案。这是因为我在创建数据库后稍后添加了该列。我更改了数据库的名称,它可以工作。

    【讨论】:

      【解决方案2】:

      我认为您应该使用 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());
          }
      

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

      快乐的编码。 :-) 我希望它对你有用。

      【讨论】:

      • 谢谢,但它并没有解决问题:/ 如果我设置 dbName = "randomString" 并注释掉 contentvalue.put(Col_Name, Name),那么它就可以工作并加载数据库并放入“ randomString" 进入文本视图。但是当我尝试使用输入时,它不起作用。
      • 您的错误是在数组列表中添加任何值之前设置适配器。因此,您只需先尝试填充您的数组列表,然后尝试将其添加到您的适配器。
      • 我找到了问题的原因,但没有解决办法。如果您仍然需要帮助,请查看已编辑的问题。
      • @DeveloperManiax 原来的while循环其实更好。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-25
      • 2017-09-04
      • 2022-01-25
      • 2016-03-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多