Android-没有这样的列错误

时间:2016-03-16 20:44:30

标签: java android database android-fragments cursor

在我告诉我一个我的专栏不存在的地方突然出现错误。此列存在于我的数据库的Contract类中,但出于某种原因,光标无法识别。

logcat的

03-16 16:37:29.029 8240-8240/com.compscitutorials.basigarcia.ramfernoscout E/SQLiteLog: (1) no such column: TEAM_NUMBER
03-16 16:37:29.030 8240-8240/com.compscitutorials.basigarcia.ramfernoscout D/AndroidRuntime: Shutting down VM
03-16 16:37:29.030 8240-8240/com.compscitutorials.basigarcia.ramfernoscout E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                             Process: com.compscitutorials.basigarcia.ramfernoscout, PID: 8240
                                                                                             android.database.sqlite.SQLiteException: no such column: TEAM_NUMBER (code 1): , while compiling: SELECT TEAM_NUMBER, PORTCULLIS, CHEVAL_DE_FRISE, MOAT, RAMPARTS, DRAWBRIDGE, SALLY_PORT, ROCK_WALL, ROCK_TERRAIN, LOW_BAR FROM scout_table
                                                                                                 at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
                                                                                                 at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:887)
                                                                                                 at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:498)
                                                                                                 at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
                                                                                                 at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
                                                                                                 at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
                                                                                                 at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
                                                                                                 at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1316)
                                                                                                 at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1163)
                                                                                                 at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1034)
                                                                                                 at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1202)
                                                                                                 at com.compscitutorials.basigarcia.ramfernoscout.DatabaseHelper.getInformation(DatabaseHelper.java:63)
                                                                                                 at com.compscitutorials.basigarcia.ramfernoscout.ScoutFragment.onCreateView(ScoutFragment.java:40)
                                                                                                 at android.support.v4.app.Fragment.performCreateView(Fragment.java:1974)
                                                                                                 at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
                                                                                                 at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252)
                                                                                                 at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
                                                                                                 at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617)
                                                                                                 at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:517)
                                                                                                 at android.os.Handler.handleCallback(Handler.java:739)
                                                                                                 at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                                 at android.os.Looper.loop(Looper.java:148)
                                                                                                 at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

DatabaseHelper.java

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

public class DatabaseHelper extends SQLiteOpenHelper {
    public static final String DATABASE_NAME = "Scout.db";
    private static final int DATABASE_VERSION = 1;
    private static final String CREATE_QUERY = "CREATE TABLE " + DatabaseContract.NewDataInfo.TABLE_NAME + "(" + DatabaseContract.NewDataInfo.COL_NUMBER +
            " INTEGER," + DatabaseContract.NewDataInfo.COL_PORTCULLIS + " TEXT," + DatabaseContract.NewDataInfo.COL_CHEVAL_FRISE + " TEXT," +
            DatabaseContract.NewDataInfo.COL_MOAT + " TEXT," + DatabaseContract.NewDataInfo.COL_RAMPARTS + " TEXT," + DatabaseContract.NewDataInfo.COL_DRAWBRIDGE +
            " TEXT," + DatabaseContract.NewDataInfo.COL_SALLY_PORT + " TEXT," + DatabaseContract.NewDataInfo.COL_ROCK_WALL + " TEXT," +
            DatabaseContract.NewDataInfo.COL_ROCK_TERRAIN + " TEXT," + DatabaseContract.NewDataInfo.COL_LOW_BAR + " TEXT);";

    public DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        Log.e("DATABASE OPERATIONS", "Database created / opened ...");
    } //End of DatabaseHelper

    @Override
    public void onCreate(SQLiteDatabase db) {
        //Create Query
        db.execSQL(CREATE_QUERY);

        //Display Log message
        Log.e("DATABASE OPERATIONS", "Table created...");
    } //End of onCreate

    public void addInformation(String eNumber, String ePoticullis, String eChevalFrise, String eMoat, String eRamparts, String eDrawbridge, String eSallyPort,
                               String eRockWall, String eRockTerrain, String eLowBar, SQLiteDatabase db) {
        //Instantiate contentValues
        ContentValues contentValues = new ContentValues();

        //Insert all content values
        contentValues.put(DatabaseContract.NewDataInfo.COL_NUMBER, eNumber);
        contentValues.put(DatabaseContract.NewDataInfo.COL_PORTCULLIS, ePoticullis);
        contentValues.put(DatabaseContract.NewDataInfo.COL_CHEVAL_FRISE, eChevalFrise);
        contentValues.put(DatabaseContract.NewDataInfo.COL_MOAT, eMoat);
        contentValues.put(DatabaseContract.NewDataInfo.COL_RAMPARTS, eRamparts);
        contentValues.put(DatabaseContract.NewDataInfo.COL_DRAWBRIDGE, eDrawbridge);
        contentValues.put(DatabaseContract.NewDataInfo.COL_SALLY_PORT, eSallyPort);
        contentValues.put(DatabaseContract.NewDataInfo.COL_ROCK_WALL, eRockWall);
        contentValues.put(DatabaseContract.NewDataInfo.COL_ROCK_TERRAIN, eRockTerrain);
        contentValues.put(DatabaseContract.NewDataInfo.COL_LOW_BAR, eLowBar);

        //Insert content values into table
        db.insert(DatabaseContract.NewDataInfo.TABLE_NAME, null, contentValues);

        //Display log message
        Log.e("DATABASE OPERATIONS", "One row inserted...");
    } //End of addInformation

    public Cursor getInformation(SQLiteDatabase db){
        Cursor cursor;
        String[] projections = {DatabaseContract.NewDataInfo.COL_NUMBER, DatabaseContract.NewDataInfo.COL_PORTCULLIS,
                DatabaseContract.NewDataInfo.COL_CHEVAL_FRISE, DatabaseContract.NewDataInfo.COL_MOAT,DatabaseContract.NewDataInfo.COL_RAMPARTS,
                DatabaseContract.NewDataInfo.COL_DRAWBRIDGE, DatabaseContract.NewDataInfo.COL_SALLY_PORT, DatabaseContract.NewDataInfo.COL_ROCK_WALL,
                DatabaseContract.NewDataInfo.COL_ROCK_TERRAIN, DatabaseContract.NewDataInfo.COL_LOW_BAR};
        cursor = db.query(DatabaseContract.NewDataInfo.TABLE_NAME, projections, null, null, null, null, null);
        return cursor;
    } //End of getInformation

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    } //End of onUpgrade
} //End of class

ScoutFragment.java

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

/**
 * A simple {@link Fragment} subclass.
 */
public class ScoutFragment extends Fragment {

    FloatingActionButton addDataScout;
    ListView eListScoutInfo;
    SQLiteDatabase sqLiteDatabase;
    DatabaseHelper databaseHelper;
    Cursor cursor;
    ListScoutInfoAdapter listScoutInfoAdapter;

    public ScoutFragment() {
        // Required empty public constructor
    } //End of ScoutFragment

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_scout, null, false);
        view.setBackgroundColor(Color.WHITE);
        eListScoutInfo = (ListView) view.findViewById(R.id.listScoutInfo);
        listScoutInfoAdapter = new ListScoutInfoAdapter(getActivity().getApplicationContext(), R.layout.row_layout);
        eListScoutInfo.setAdapter(listScoutInfoAdapter);
        databaseHelper = new DatabaseHelper(getActivity().getApplicationContext());
        sqLiteDatabase = databaseHelper.getReadableDatabase();
        cursor = databaseHelper.getInformation(sqLiteDatabase);

        //Checks if information is available in cursor
        if(cursor.moveToFirst()){
            do {
                //Delcare all strings
                String teamNumber, portcullis, chevalFrise, moat, ramparts, drawbridge, sallyPort, rockWall, rockTerrain, lowBar;

                //Get strings from cursor
                teamNumber = cursor.getString(0);
                portcullis = cursor.getString(1);
                chevalFrise = cursor.getString(2);
                moat = cursor.getString(3);
                ramparts = cursor.getString(4);
                drawbridge = cursor.getString(5);
                sallyPort = cursor.getString(6);
                rockWall = cursor.getString(7);
                rockTerrain = cursor.getString(8);
                lowBar = cursor.getString(9);

                //Get methods from DatabaseProvider
                DatabaseProvider databaseProvider = new DatabaseProvider(teamNumber, portcullis, chevalFrise, moat, ramparts,
                        drawbridge, sallyPort, rockWall, rockTerrain, lowBar);

                //Pass objects to add method
                listScoutInfoAdapter.add(databaseProvider);
            } while (cursor.moveToNext());
        } //End of if statement

        //Setups Floating Action Button
        addDataScout = (FloatingActionButton) view.findViewById(R.id.fab);
        addDataScout.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                AddScoutDataFragment fragment = new AddScoutDataFragment();
                FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
                fragmentTransaction.setCustomAnimations(R.anim.enter_from_right, R.anim.exit_to_left, R.anim.enter_from_left, R.anim.exit_to_right);
                fragmentTransaction.replace(R.id.fragment_container, fragment);
                fragmentTransaction.commit();
            } //End of onClick
        }); //End of setOnClickListener
        return view;
    } //End of onCreateView
} //End of class

DatabaseContract.java

public class DatabaseContract {
    public static abstract class NewDataInfo {
        public static final String TABLE_NAME = "scout_table";
        public static final String COL_NUMBER = "TEAM_NUMBER";
        public static final String COL_PORTCULLIS = "PORTCULLIS";
        public static final String COL_CHEVAL_FRISE = "CHEVAL_DE_FRISE";
        public static final String COL_MOAT = "MOAT";
        public static final String COL_RAMPARTS = "RAMPARTS";
        public static final String COL_DRAWBRIDGE = "DRAWBRIDGE";
        public static final String COL_SALLY_PORT = "SALLY_PORT";
        public static final String COL_ROCK_WALL = "ROCK_WALL";
        public static final String COL_ROCK_TERRAIN = "ROCK_TERRAIN";
        public static final String COL_LOW_BAR = "LOW_BAR";
    } //End of NewDataInfo
} //End of class

1 个答案:

答案 0 :(得分:2)

如上所述,如果应用数据在没有此列之前创建数据库时仍应处于开发状态,则应清除应用数据,如果它在生产阶段,则应在OnUpgrad方法中进行此类升级。