samedi 1 août 2015

Why is my SQLite still not working in my fragments after i add a collumn

I am new to coding, I tried to add a new column using sqlite but it does not work now,but it work before with one string column can someone tell me what's wrong with my code now it wont save and display because of no link column in the logs ,when I reinstalled the app and change the version someone mind helping a poor soul out?

History Fragment

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
import android.database.Cursor;


public class HistoryFragment extends Fragment {
EditText name;
EditText link;
TextView sqlDisplay;
MyDBHandler dbHandler;

public HistoryFragment() {


}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_history, container, false);
    link = (EditText) rootView.findViewById(R.id.link);
    name = (EditText) rootView.findViewById(R.id.name);
    sqlDisplay = (TextView) rootView.findViewById(R.id.sqlDisplay);
    dbHandler = new MyDBHandler(this.getActivity(),null , null, null, 1);
    printDatabase();
    return rootView;




}

@Override
public void onViewCreated(View rootView, Bundle savedInstanceState) {
    rootView.findViewById(R.id.saveButton).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            saveOnClicked(v);
        }
    });
    rootView.findViewById(R.id.deleteButton).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            deleteOnClicked(v);
        }
    });
}
//Add a product to the database

public void saveOnClicked(View view) {
    favourites favourites = new favourites(name.getText().toString(),link.getText().toString());

    dbHandler.addFavourites(favourites);
    printDatabase();
}

//Delete items
public void deleteOnClicked(View view) {
    String inputTextName = name.getText().toString();
    String inputTextLink = link.getText().toString();
    dbHandler.deleteFavourites(inputTextName,inputTextLink );
    printDatabase();
}

//Print the database
public void printDatabase() {
    String dbString = dbHandler.databaseToString();
    sqlDisplay.setText(dbString);
    name.setText("") ; link.setText("");
}
}

Favourites.java

public class favourites {
private int _id;
private String _videoname;
private String _link;

public favourites(){

}

public favourites (String videoname,String link){
    this._videoname = videoname;
    this._link = link;
}
public void set_id(int _id) {
    this._id = _id;
}
public void set_videoname(String _videoname) {
    this._videoname = _videoname;
}
public String get_videoname() {
    return _videoname;
}
public int get_id() {
    return _id;
}





public String get_link() {
    return _link;
}

public void set_link(String _link) {
    this._link = _link;
}
}

MyDBHandler

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

public class MyDBHandler extends SQLiteOpenHelper {

public static final int DATABASE_VERSION = 3;
public static final String DATABASE_NAME = "favourites.db";
public static final String TABLE_FAVOURITES = "favourites";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_VIDEONAME = "videoname";
public static final String COLUMN_LINK = "link";

public MyDBHandler(Context context, String name, String link, SQLiteDatabase.CursorFactory factory, int version) {
    super(context, DATABASE_NAME, factory, DATABASE_VERSION);
}



@Override
public void onCreate(SQLiteDatabase db) {
    String query = "CREATE TABLE " + TABLE_FAVOURITES + "(" +
            COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
            COLUMN_VIDEONAME + " TEXT " + COLUMN_LINK + " TEXT " +
            ");";
    db.execSQL(query);

}

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

public void addFavourites(favourites favourites) {
    ContentValues values = new ContentValues();
    values.put(COLUMN_VIDEONAME, favourites.get_videoname());
    values.put(COLUMN_LINK, favourites.get_link());
    SQLiteDatabase db = getWritableDatabase();
    db.insert(TABLE_FAVOURITES, null,values);
    db.close();
}

public void deleteFavourites(String videoName, String link) {
    SQLiteDatabase db = getWritableDatabase();

    db.execSQL("DELETE FROM " + TABLE_FAVOURITES + " WHERE " + COLUMN_VIDEONAME + "=\"" + videoName + "\" "
                    + COLUMN_LINK + "=\"" + link + "\" ;"
    );
}

public String databaseToString() {
    String dbString = "";
    SQLiteDatabase db = getWritableDatabase();
    String query = "SELECT * FROM " + TABLE_FAVOURITES + " WHERE 1";

    //Cursor points to a location in your results
    Cursor c = db.rawQuery(query, null);
    //Move to the first row in your results
    c.moveToFirst();

    //Position after the last row means the end of the results
    while (!c.isAfterLast()) {
        if (c.getString(c.getColumnIndex("videoname")) != null) {
            dbString += c.getString(c.getColumnIndex("videoname"));
            dbString += "\n";

        }
        c.moveToNext();
    }
    db.close();
    return dbString;
}

}

My logs ><

07-31 16:41:26.234  24237-24237/com.example.navigationdrawerexample E/SQLiteLog﹕ (1) table favourites has no column named link
07-31 16:41:26.274  24237-24237/com.example.navigationdrawerexample E/SQLiteDatabase﹕ Error inserting videoname=g link=g
    android.database.sqlite.SQLiteException: table favourites has no column named link (code 1): , while compiling: INSERT INTO favourites(videoname,link) VALUES (?,?)
            at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
            at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1093)
            at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:670)
            at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
            at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:59)
            at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
            at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1593)
            at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1465)
            at com.example.onTV.MyDBHandler.addFavourites(MyDBHandler.java:46)
            at com.example.onTV.HistoryFragment.saveOnClicked(HistoryFragment.java:60)
            at com.example.onTV.HistoryFragment$1.onClick(HistoryFragment.java:45)
            at android.view.View.performClick(View.java:5191)
            at android.view.View$PerformClick.run(View.java:20916)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:145)
            at android.app.ActivityThread.main(ActivityThread.java:5944)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
07-31 16:41:27.714  24237-24237/com.example.navigationdrawerexample D/ViewRootImpl﹕ ViewPostImeInputStage ACTION_DOWN
07

Aucun commentaire:

Enregistrer un commentaire