SQLITE - Android /显示数据错误

时间:2016-05-30 11:58:25

标签: android sqlite

我使用手动方法打开我的数据库,然后在第一次我可以在列表上显示数据,现在,当我更改表时,它不起作用,我找不到错误:这是我的DB表:

使用来自DBhelper的getalllines显示的主要代码:

obj = (ListView) findViewById(R.id.listView1);

        List<line> array_list = db.getAlllines();

        ArrayAdapter<line> arrayAdapter = new ArrayAdapter<line>(this, android.R.layout.simple_list_item_1, array_list);

        obj.setAdapter(arrayAdapter);

我的方法Getalllines:

public List<line> getAlllines() {
            List<line> lines = new LinkedList<line>();

            String query = "SELECT  * FROM " + TABLE_LINE;

            SQLiteDatabase db = this.getWritableDatabase();
            Cursor cursor = db.rawQuery(query, null);

            // 3. go over each row, build book and add it to list
            line linee = null;
            if (cursor.moveToFirst()) {
                do {
                    linee = new line();
                    linee.setId(Integer.parseInt(cursor.getString(0)));
                    linee.setLinename(cursor.getString(1));
                    linee.setFromgare(cursor.getString(2));
                    linee.setTogare(cursor.getString(3));

                    lines.add(linee);
                } while (cursor.moveToNext());
            }

            Log.d("getAlllines()", lines.toString());

            return lines;
        }

如果您需要知道我如何创建表格:

  @Override
        public void onCreate(SQLiteDatabase db) {
            // SQL statement to create book table
            String CREATE_LINES_TABLE = "CREATE TABLE lines ( " +
                    "_id    INTEGER NOT NULL," + 
                    "linename   VARCHAR," +
                    "fromgare   VARCHAR," +
                    "togare VARCHAR," +
                    "PRIMARY KEY(_id))";

            // create books table
            db.execSQL(CREATE_LINES_TABLE);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            // Drop older books table if existed
            db.execSQL("DROP TABLE IF EXISTS lines");

            // create fresh books table
            this.onCreate(db);
        }

1 个答案:

答案 0 :(得分:0)

在你的:

 public void onCreate(SQLiteDatabase db) {
            // SQL statement to create book table
            String CREATE_LINES_TABLE = "CREATE TABLE lines ( " +
                    "_id    INTEGER NOT NULL," + 
                    "linename   VARCHAR," +
                    "fromgare   VARCHAR," +
                    "togare VARCHAR," +
                    "PRIMARY KEY(_id))";

            // create books table
            db.execSQL(CREATE_LINES_TABLE);
        }

你必须删除:id

中的“_”