【问题标题】:Android SQLite Database insertion/select not workingAndroid SQLite 数据库插入/选择不起作用
【发布时间】:2011-12-16 12:23:06
【问题描述】:

谁能看看我附加的代码并告诉我为什么我没有从数据库中插入或选择任何值。我什至不知道我没有做什么,因为我没有收到任何错误。我所知道的是,当我运行选择查询时,游标不包含任何内容。

谢谢

SQLiteDatabase db = new DatabseHelper(this).getWritableDatabase();
          //  db.execSQL(mDATABASE_CREATE);
            String id = "1";
            String feedback = "this is some feedback";
            String expiredAt = "12:00";
            String priority = "1";
            String read = "false";
            String sql =   "INSERT INTO feedbackTable (_id, feedback, expiredAt, priority, read) VALUES (\""+id+"\", \""+feedback+"\", \""+expiredAt+"\", \""+priority+"\", \""+read+"\")"; 
            db.rawQuery(sql, null);
            Cursor result = db.rawQuery("SELECT * FROM feedbackTable", null);
            result.moveToFirst();

            String rfeedback="";
            while(!result.isAfterLast()){
                rfeedback=result.getString(1);
            }

        Log.e("returned feedback", "inserted feedback "+rfeedback);
            db.close();

修改上述代码后的第二个问题:

SQLiteDatabase db = new DatabseHelper(this).getWritableDatabase();
            String id = "1";
            String feedback = "this is some feedback";
            String expiredAt = "12:00";
            String priority = "1";
            String read = "false";
            String sql =   "INSERT INTO feedbackTable (_id, feedback, expiredAt, priority, read) VALUES (\""+id+"\", \""+feedback+"\", \""+expiredAt+"\", \""+priority+"\", \""+read+"\");"; 


            ContentValues values = new ContentValues(5);
            values.put("_id", "1");
            values.put("feedback", "some feedback");
            values.put("expiredAt", "12:00");
            values.put("priority", "1");
            values.put("read", "false");
            db.insert("feedbackTable", "_id", values);
            Cursor result = db.rawQuery("SELECT * FROM feedbackTable", null);
            //db.execSQL(sql);
            //insert(feedbackTable, nullColumnHack, values)
            //.rawQuery(sql, null);
            result.moveToFirst();

            String rfeedback="";
            while(!result.isAfterLast()){
                rfeedback=result.getString(1);
            }

            Log.e("returned feedback", "inserted feedback "+rfeedback);
            db.close();

同时使用 execSQL 和 db.insert 我的应用程序只是挂起等待响应。我从来没有到达上面的 Log.e,因为这个动作发生在绘制屏幕之前的 oncreate 方法中,所以我只看到一个空白屏幕。知道为什么吗?

【问题讨论】:

  • 查看 [this][1] 帖子。尝试使用 db.insert() 而不是 db.rawQuery()。 [1]:stackoverflow.com/questions/2616130/…
  • 是的,我明白了——我应该使用过 execSQL 或 db.insert。我都试过了,现在两个都有新问题,所以肯定还有第二个问题。
  • 去掉多余的引号。您不需要将“值”插入字符串。要么根本不包围,要么使用单引号 ''

标签: android sqlite select insert


【解决方案1】:

我怀疑您需要移动光标。您的代码似乎卡在了 while (!result.isAfterLast()) 中。除非你 result.moveToNext() (或以其他方式推进光标)它永远不会在 last 之后,while 语句将无限循环。事实上,如果您希望只有一行,您可以完全消除 while 循环。

【讨论】:

  • 非常感谢!那让我发疯了。我还要感谢 Jack 的帮助。两个菜鸟失误!
猜你喜欢
  • 2014-04-21
  • 2018-02-19
  • 2015-01-24
  • 1970-01-01
  • 1970-01-01
  • 2016-03-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多