为什么Android Studio Keep Saying没有标题列?

时间:2015-07-27 19:05:59

标签: java android sqlite

无论我多少次查看过我的代码,我都会收到LogCat错误

 Process: com.zito.zitolab4, PID: 2357
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.zito.zitolab4/com.zito.zitolab4.ZitoLab4Data}: android.database.sqlite.SQLiteException: no such column: title (code 1): , while compiling: SELECT id, title, genre, releaseYear FROM movies

基于LogCat,我认为它将在Data.Java类中,但我到处都是,我只是没有设置它。我会很感激任何建议。

由于

Data.Java

package com.zito.zitolab4;

import android.app.ProgressDialog;
import android.database.Cursor;
import android.os.AsyncTask;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;



public class ZitoLab4Data extends ActionBarActivity {

    TextView idView;
    EditText titleBox, genreBox, yearBox;
    Button addMovieButton;
    TableLayout movieTable;
    private ZitoDataController sqlCon;
    private ProgressDialog progressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zito_lab4_data);

        sqlCon = new ZitoDataController(this);

        titleBox = (EditText) findViewById(R.id.movieTitle);
        genreBox = (EditText) findViewById(R.id.movieGenre);
        yearBox =  (EditText) findViewById(R.id.releaseYear);
        addMovieButton = (Button) findViewById(R.id.addMovieButton);
        movieTable = (TableLayout) findViewById(R.id.movieTable);

        BuildTable();

        addMovieButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new MyAsync().execute();
            }
        });
    }

    @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_zito_lab4_data, menu);
        return true;
    }

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

    private void BuildTable() {
        sqlCon.open();
        Cursor dbCursor = sqlCon.readEntry();

        int movies = dbCursor.getCount();
        int fields = dbCursor.getColumnCount();

        // 1 row for each person in the database table
        for (int i = 0; i < movies; i++) {
            TableRow row = new TableRow(this);
            row.setLayoutParams(new TableRow.LayoutParams(
                    TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));

            // create a TextView for each field in the record
            for (int j = 0; j < fields; j++) {
                TextView TableDataTextView = new TextView(this);
                TableDataTextView.setLayoutParams(new TableRow.LayoutParams(
                        TableRow.LayoutParams.WRAP_CONTENT,
                        TableRow.LayoutParams.WRAP_CONTENT));
                TableDataTextView.setTextSize(16);
                TableDataTextView.setPadding(0, 5, 0, 5);
                TableDataTextView.setText(dbCursor.getString(j));
                row.addView(TableDataTextView);
            }
            dbCursor.moveToNext();
            movieTable.addView(row);
        }
        sqlCon.close();
    }

    private class MyAsync extends AsyncTask<Void, Void, Void> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            movieTable.removeAllViews();

            progressDialog = new ProgressDialog(ZitoLab4Data.this);
            progressDialog.setTitle("Please Wait...");
            progressDialog.setMessage("Loading...");
            progressDialog.setCancelable(false);
            progressDialog.show();
        }

        @Override
        protected Void doInBackground(Void... params) {
            String title = titleBox.getText().toString();
            String genre = genreBox.getText().toString();
            int year = Integer.parseInt(yearBox.getText().toString());

            sqlCon.open();
            sqlCon.insertData(title, genre, year);
            return  null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            BuildTable();
            progressDialog.dismiss();
            titleBox.setText("");
            genreBox.setText("");
            yearBox.setText("");
            titleBox.requestFocus();
        }
    }
}

DataModel.Java

package com.zito.zitolab4;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class ZitoDataModel extends SQLiteOpenHelper {

    private  static final String DATABASE_NAME = "ZitoLab4DB.db";
    private static final int DATABASE_VERSION = 1;
    public static final String TABLE_MOVIES = "movies";

    public static final String MOVIE_ID = "id";
    public static final String MOVIE_TITLE = "title";
    public static final String MOVIE_GENRE = "genre";
    public static final String MOVIE_YEAR = "releaseYear";

    public ZitoDataModel(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }
    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE " + TABLE_MOVIES + "(" + MOVIE_ID + " INTEGER PRIMARY KEY," +
                MOVIE_TITLE + " TEXT," + MOVIE_GENRE + " STRING, " + MOVIE_YEAR + " INTEGER)");
    }

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

    public void addMovie(String title, String genre, int year) {
        ContentValues values = new ContentValues();
        values.put(MOVIE_TITLE, title);
        values.put(MOVIE_GENRE, genre);
        values.put(MOVIE_YEAR, year);

        SQLiteDatabase db = this.getWritableDatabase();
        db.insert(TABLE_MOVIES, null, values);
        db.close();
    }
    public Cursor readEntry(){
        SQLiteDatabase db = this.getWritableDatabase();
        String[] allColumns = new String[]{ZitoDataModel.MOVIE_ID, ZitoDataModel.MOVIE_TITLE,
                ZitoDataModel.MOVIE_GENRE, ZitoDataModel.MOVIE_YEAR};
        Cursor dbCursor = db.query(ZitoDataModel.TABLE_MOVIES, allColumns,
                null, null, null, null, null);
        if (dbCursor !=null) {
            dbCursor.moveToFirst();
        }
        return dbCursor;
    }

}

DataController.Java

package com.zito.zitolab4;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class ZitoDataController {

    private ZitoDataModel dataModel;
    private Context movieContext;
    private SQLiteDatabase database;

    public ZitoDataController(Context context) {
        movieContext = context;
    }

    public ZitoDataController open() throws SQLException {
        dataModel = new ZitoDataModel(movieContext);
        database = dataModel.getWritableDatabase();
        return this;
    }

    public void close()
    {
        dataModel.close();
    }

    public void insertData(String title, String genre, int year) {
        ContentValues values = new ContentValues();
        values.put(ZitoDataModel.MOVIE_TITLE, title);
        values.put(ZitoDataModel.MOVIE_GENRE, genre);
        values.put(ZitoDataModel.MOVIE_YEAR, year);
        database.insert(ZitoDataModel.TABLE_MOVIES, null, values);
    }

    public Cursor readEntry() {
        String[] allColumns = new String[]{ZitoDataModel.MOVIE_ID, ZitoDataModel.MOVIE_TITLE,
                ZitoDataModel.MOVIE_GENRE, ZitoDataModel.MOVIE_YEAR};
        Cursor dataCursor = database.query(ZitoDataModel.TABLE_MOVIES,
                allColumns, null, null, null, null, null);
        if (dataCursor != null) {
            dataCursor.moveToFirst();
        }
        return dataCursor;
    }
}

1 个答案:

答案 0 :(得分:1)

您可能稍后(在第一次运行之后)修改了表格。

卸载并重新安装您的应用。
或者,或者,增加此常量的值:DATABASE_VERSION

在第一种情况下,数据库将正常创建(第一次) 第二种情况导致onUpgrade()方法触发,然后调用

  db.execSQL("DROP TABLE IF EXISTS  " + TABLE_MOVIES);
  onCreate(db);

所以,事实上,删除现有的表然后重新创建它。