I'm doing an SQL Data base with Player but there is an argument I can add. I copy you my class Player and my SQL class. I don't understand where is the problem. This is my class Player
public class Player implements Parcelable {
private int id;
protected String name;
protected boolean ismascsexe=true;
public Player(int id, String name, boolean ismascsexe) {
this.id = id;
this.name = name;
this.ismascsexe=ismascsexe;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public boolean isMascsexe() {
return ismascsexe;
}
public void setMascsexe(boolean ismascsexe) {
this.ismascsexe = ismascsexe;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(id);
dest.writeString(name);
dest.writeInt(ismascsexe ? 1 : 0);
}
public static final Parcelable.Creator<Player> CREATOR
= new Parcelable.Creator<Player>() {
public Player createFromParcel(Parcel in) {
return new Player(in);
}
public Player[] newArray(int size) {
return new Player[size];
}
};
private Player(Parcel in) {
id = in.readInt();
name=in.readString();
ismascsexe = (in.readInt() == 0) ? false : true;
}
}
And this is my class SQL lite :
public class DatabaseHandler extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "PlayerManager",
TABLE_PLAYER = "players",
KEY_ID = "id",
KEY_NAME = "name",
KEY_BOOL = "sex";
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + TABLE_PLAYER + "("+KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT ," + KEY_NAME + " TEXT ," + KEY_BOOL + " TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_PLAYER);
onCreate(db);
}
public void createPlayer(Player player) {
SQLiteDatabase db = getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, player.getName());
values.put(KEY_BOOL, String.valueOf(player.isMascsexe()));
db.insert(TABLE_PLAYER, null, values);
db.close();
}
public Player getPlayer(int id) {
SQLiteDatabase db = getReadableDatabase();
Cursor cursor = db.query(TABLE_PLAYER, new String[] { KEY_ID, KEY_NAME, KEY_BOOL }, KEY_ID + "=?", new String[] { String.valueOf(id) }, null, null, null, null );
if (cursor != null)
cursor.moveToFirst();
Player player = new Player(Integer.parseInt(cursor.getString(0)), cursor.getString(1), Boolean.parseBoolean(cursor.getString(2))); //Integer.parseInt(cursor.getString(2)));
db.close();
cursor.close();
return player;
}
public void deleteContact(Player player) {
SQLiteDatabase db = getWritableDatabase();
db.delete(TABLE_PLAYER, KEY_ID + "=?", new String[] { String.valueOf(player.getId()) });
db.close();
}
public int getPlayerCount() {
SQLiteDatabase db = getReadableDatabase();
Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_PLAYER, null);
int count = cursor.getCount();
db.close();
cursor.close();
return count;
}
public int updatePlayer(Player player) {
SQLiteDatabase db = getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, player.getName());
values.put(KEY_BOOL,String.valueOf(player.isMascsexe()));
int rowsAffected = db.update(TABLE_PLAYER, values, KEY_ID + "=?", new String[] { String.valueOf(player.getId()) });
db.close();
return rowsAffected;
}
public ArrayList<Player> getAllPlayer() {
ArrayList<Player> players = new ArrayList<Player>();
SQLiteDatabase db = getWritableDatabase();
Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_PLAYER, null);
if (cursor.moveToFirst()) {
do {
players.add(new Player(Integer.parseInt(cursor.getString(0)), cursor.getString(1), Boolean.parseBoolean(cursor.getString(2))));//Integer.parseInt(cursor.getString(2))));
}
while (cursor.moveToNext());
}
cursor.close();
db.close();
return players;
}
This is the Log where there is the
android.database.sqlite.SQLiteException: table players has no column named sex (code 1): , while compiling: INSERT INTO players(sex,name) VALUES (?,?)
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1013)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:624)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1467)
at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1339)
at com.example.guillaume.widgetsaavances.DatabaseHandler.createPlayer(DatabaseHandler.java:56)
at com.example.guillaume.widgetsaavances.MainActivity$1$2.onClick(MainActivity.java:131)
at android.view.View.performClick(View.java:4162)
at android.view.View$PerformClick.run(View.java:17082)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4867)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
at dalvik.system.NativeStart.main(Native Method)
Thank you for helping me :) Bye
Aucun commentaire:
Enregistrer un commentaire