I'm getting the above error, and I really cant find what I did wrong. I was following the video: https://www.youtube.com/watch?v=p8TaTgr4uKM I looked at android.database.sqlite.SQLiteException: table X has no column named Y: , while compiling: INSERT INTO but I couldn't find an error. I'm afraid that onCreate of RunsHistoryDataBase isn't called cause I've put there Log.d and in insertData and the Log.d of onCreate didn't show up and the Log.d of insertData did show up. AnyWay I can't insert data to the DB and I really don't understand why.. any help please?
these is the code of the SQLiteDB: public class RunsHistoryDataBase extends 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 here is the code of the fragment which inserts data to the db:
public class HistoryFragment extends 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);
}
}