mercredi 30 décembre 2015

My emulator log doesn't recognize that I added a third column to my SQL database

I started working on app recently, to give myself a crash course in coding so I can give app and game development a whirl. The code below is what I wrote to create a database with three columns - an ID column, an email column, and a password column. (Full disclosure, i was using tutorials online to write this and am adding my own to it as practice).
I ran the full program on my phone, with the ability to add an delete from the database with only one column and it worked, but when I added code for the the third column, everything stopped working. Did I make a hideous mistake, or am I just missing a line somewhere? Thank you for reading this far.

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


public class EmailDBHandler extends SQLiteOpenHelper{

private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "loginEntries.db";
public static final String TABLE_LOGINENTRIES = "loginEntries";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_EMAILADDRESS = "emailAddress";

/*this is where I initialized the column "password" */

public static final String COLUMN_PASSWORD = "password";


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


@Override
public void onCreate(SQLiteDatabase db) {
    String query = "CREATE TABLE " + TABLE_LOGINENTRIES + "(" +
            COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
            COLUMN_EMAILADDRESS + " TEXT " +

/this part of the code shows that I added the column to my table/

            COLUMN_PASSWORD + " TEXT " + ");";
    db.execSQL(query);
}

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

//add new row to Database

public void addEntry(LoginEntries entry){
    ContentValues values = new ContentValues();
    values.put(COLUMN_EMAILADDRESS,entry.get_emailAddress());
    values.put(COLUMN_PASSWORD,entry.get_password());
    SQLiteDatabase db = getWritableDatabase();
    db.insert(TABLE_LOGINENTRIES, null, values);
    db.close();
}


//delete items from database

public void deleteEmailEntry(String emailEntry){
    SQLiteDatabase db = getWritableDatabase();
    db.execSQL("DELETE FROM " + TABLE_LOGINENTRIES + " WHERE " + COLUMN_EMAILADDRESS + "=\"" +
            emailEntry + "\";");
}


public void deletePasswordEntry(String passwordEntry){
    SQLiteDatabase db = getWritableDatabase();
    db.execSQL("DELETE FROM " + TABLE_LOGINENTRIES + " WHERE " + COLUMN_PASSWORD + "=\"" +
            passwordEntry + "\";");
}

//Print database as a string
public String databaseToString(){
    String dbString = "";
    SQLiteDatabase db = getWritableDatabase();
    String query = "SELECT * FROM " + TABLE_LOGINENTRIES + " WHERE 1";

    //cursor point to a location in your results
    Cursor c = db.rawQuery(query, null);
    c.moveToFirst();
    while(!c.isAfterLast()){
        if(c.getString(c.getColumnIndex(COLUMN_EMAILADDRESS)) !=null){
            dbString += c.getString(c.getColumnIndex(COLUMN_EMAILADDRESS));
            dbString += "\n";
        }c.moveToNext();
    }
    db.close();
    return dbString;
}
public String databaseTwoString(){
    String dbStringTwo = "";
    SQLiteDatabase db = getWritableDatabase();
    String query = "SELECT * FROM " + TABLE_LOGINENTRIES + " WHERE 1";

    //cursor point to a location in your results
    Cursor c = db.rawQuery(query, null);
    c.moveToFirst();
    while(!c.isAfterLast()){
        if(c.getString(c.getColumnIndex(COLUMN_PASSWORD)) != null){
            dbStringTwo += c.getString(c.getColumnIndex(COLUMN_PASSWORD));
            dbStringTwo += "\n";
        }
        c.moveToNext();
    }
    db.close();
    return dbStringTwo;
}
}

/The code below is the declaration of the items in my database/

public class LoginEntries {
private int _id;
private String _emailAddress;
private String _password;

public LoginEntries(){}

public LoginEntries(String emailAddress, String password){
    this._emailAddress = emailAddress;
    this._password = password;
}
public void set_id(int _id) {
    this._id = _id;
}

public void set_emailAddress(String _emailAddress) {this._emailAddress = _emailAddress;}

public void set_password(String _password) {this._password = _password;}

public int get_id() {
    return _id;
}

public String get_emailAddress() {return _emailAddress;}

public String get_password() {  return _password;}
}

Aucun commentaire:

Enregistrer un commentaire