android.database.sqlite.SQLiteException:table TABLE_NAME没有名为X的列(代码1):,编译时:INSERT INTO

时间:2017-01-12 08:22:05

标签: android sqlite android-sqlite

我收到了上述错误,我真的无法找到我做错了什么。 我正在关注视频: https://www.youtube.com/watch?v=p8TaTgr4uKM 我看了android.database.sqlite.SQLiteException: table X has no column named Y: , while compiling: INSERT INTO 但我找不到错误。 我担心onCreate of RunsHistoryDataBase没有被调用因为我把它放在了Log.d和insertData中并且onCreate的Log.d没有显示出来并且insertData的Log.d确实显示出来了。 AnyWay我无法将数据插入数据库,我真的不明白为什么.. 有什么帮助吗?

这些是SQLiteDB的代码: 公共类RunsHistoryDataBase扩展了SQLiteOpenHelper {

public static final String dataBaseName = "RunsHistory.db";
public static final String tableName = "RunsHistoryTable";
public static final String colDate = "Date";
public static final String colPartnersName = "PartnersName";
public static final String colDuration = "Duration";
public static final String colDistance = "Distance";

public RunsHistoryDataBase(Context context) {
    // create the data base and the table.
    super(context, dataBaseName, null, 1);
}

@Override
public void onCreate(SQLiteDatabase db) {
    String createDataBase = "CREATE TABLE "+ tableName
            + "("
            + colDate +" TEXT PRIMARY KEY, "
            + colPartnersName + " TEXT, "
            + colDuration + " INTEGER, "
            + colDistance + " INTEGER);";
    db.execSQL(createDataBase);
}

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

public boolean insertNewHistoryRun(Date date, String partnersName, int duration, int distance){

    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues contentValues = new ContentValues();
    contentValues.put(colDistance, distance);
    contentValues.put(colDuration, duration);
    contentValues.put(colDate, getFixedDateForPrint(date.toString()));
    contentValues.put(colPartnersName, partnersName);

    long result = db.insert(tableName, null, contentValues);

    if(result == -1){
        Log.d("result == -1","");
        return false;
    }
    return true;
}

public Cursor getAllData(){
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor res = db.rawQuery("select * from "+tableName, null);
    return res;
}

}

这里的Ande是将数据插入db:

的片段代码

公共类HistoryFragment扩展了Fragment {

private String mParam1;
private String mParam2;
Button insertDataBut;
Button getDataBut;
Button updateDataBut;
EditText date_editText;
EditText distance_history_editText;
EditText partners_name_editText;
EditText duration_editText_history;
RunsHistoryDataBase myDB;
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
View view;


private OnHistoryInteractionListener mListener;

public HistoryFragment() {
    // Required empty public constructor
}

public static HistoryFragment newInstance(String param1, String param2) {
    HistoryFragment fragment = new HistoryFragment();
    Bundle args = new Bundle();
    args.putString(ARG_PARAM1, param1);
    args.putString(ARG_PARAM2, param2);
    fragment.setArguments(args);
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
        mParam1 = getArguments().getString(ARG_PARAM1);
        mParam2 = getArguments().getString(ARG_PARAM2);
    }
    myDB = new RunsHistoryDataBase(getContext());
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    view = inflater.inflate(R.layout.fragment_history, container, false);

    insertDataBut = (Button) view.findViewById(R.id.insertNewButton);
    getDataBut = (Button) view.findViewById(R.id.getDataButton);
    updateDataBut = (Button) view.findViewById(R.id.updateButton);

    date_editText = (EditText) view.findViewById(R.id.date_editText);
    distance_history_editText = (EditText) view.findViewById(R.id.distance_history_editText);
    partners_name_editText = (EditText) view.findViewById(R.id.partners_name_editText);
    duration_editText_history = (EditText) view.findViewById(R.id.duration_editText_history);
    AddData();
    viewAll();
    return view;
}

public void AddData() {
    insertDataBut.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Date date = new Date();
            boolean ifInserted = myDB.insertNewHistoryRun(date, partners_name_editText.getText().toString(),
                    Integer.parseInt(duration_editText_history.getText().toString()), Integer.parseInt(distance_history_editText.getText().toString()));
            if (ifInserted) {
                Toast.makeText(getContext(), "data is inserted", Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(getContext(), "is not inserted", Toast.LENGTH_LONG).show();
            }
        }
    });
}

public void viewAll() {
    getDataBut.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Cursor res = myDB.getAllData();
            if (res.getCount() == 0) {
                showMessage("No History", "Empty");
                return;
            }
            StringBuffer buffer = new StringBuffer();
            while (res.moveToNext()) {
                buffer.append("Date :" + res.getString(0) + "\n");
                buffer.append("Partners Name :" + res.getString(1) + "\n");
                buffer.append("Duration :" + res.getString(2) + "\n");
                buffer.append("Distance :" + res.getString(3) + "\n");
            }
            showMessage("Data", buffer.toString());

        }
    });
}

// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
    if (mListener != null) {
        mListener.onHistoryInteraction(uri);
    }
}

public void showMessage(String title, String message) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
    builder.setCancelable(true);
    builder.setTitle(title);
    builder.setMessage(message);
    builder.show();
}

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    if (context instanceof OnHistoryInteractionListener) {
        mListener = (OnHistoryInteractionListener) context;
    } else {
        throw new RuntimeException(context.toString()
                + " must implement OnFragmentInteractionListener");
    }
}

@Override
public void onDestroy() {
    super.onDestroy();
    myDB.close();
}

@Override
public void onDetach() {
    super.onDetach();
    mListener = null;
}

public interface OnHistoryInteractionListener {
    void onHistoryInteraction(Uri uri);
}

}

0 个答案:

没有答案
相关问题