vendredi 30 octobre 2015

Android Sqlite db.insert with null

The problem is when I go to insert to the database sqlite, because when i return the data of all "persons", only insert the id_persons and all of others fields are null :(.

This is my DatabaseHelper.java:

public class DatabaseHelper extends SQLiteOpenHelper {

// Table Names
    private static final String TABLE_PERSONS = "persons";
// PERSON Table column names
    private static final String KEY_ID_PERSON = "id_person";
    private static final String KEY_FIRST_NAME = "first_name";
    private static final String KEY_LAST_NAME = "last_name";
    private static final String KEY_PHONE1 = "phone1";
    private static final String KEY_PHONE2 = "phone2";
    private static final String KEY_GENRE = "genre";
    private static final String KEY_LEVEL = "level";
    private static final String KEY_EMAIL = "email";

// Persons table create statement
    private static final String CREATE_TABLE_PERSONS = "CREATE TABLE "
            + TABLE_PERSONS + "(" 
            + KEY_ID_PERSON + " INTEGER PRIMARY KEY,"
            + KEY_FIRST_NAME + " TEXT NOT NULL,"
            + KEY_LAST_NAME  + " TEXT NOT NULL,"
            + KEY_PHONE1 + " TEXT NOT NULL,"
            + KEY_PHONE2  + " TEXT NOT NULL,"
            + KEY_GENRE + " INTEGER NOT NULL,"
            + KEY_LEVEL  + " INTEGER NOT NULL,"
            + KEY_EMAIL + " TEXT NOT NULL" + ")";

    public DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

        if (!db.isReadOnly()) {
            // Enable foreign key constraints
            db.execSQL("PRAGMA foreign_keys=ON;");
        }
        // creating required tables
        db.execSQL(CREATE_TABLE_PERSONS);
    }

    public void createPerson(Person person, long[] persons_ids) {

            SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(KEY_ID_PERSON, person.getId_person());
        values.put(KEY_FIRST_NAME, person.getFirst_name());
        values.put(KEY_LAST_NAME, person.getLast_name());
        values.put(KEY_PHONE1, person.getPhone1());
        values.put(KEY_PHONE2, person.getPhone2());
        values.put(KEY_GENRE, person.getGenre());
        values.put(KEY_LEVEL, person.getLevel());
        values.put(KEY_EMAIL, person.getEmail());

// insert row
        long person_id = db.insert(TABLE_PERSONS, null, values);

        }

    public ArrayList<Person> getAllPersons() {
        ArrayList<Person> persons = new ArrayList<Person>();
        String selectQuery = "SELECT  * FROM " + TABLE_PERSONS;

        Log.e(LOG, selectQuery);

        SQLiteDatabase db = this.getReadableDatabase();
        Cursor c = db.rawQuery(selectQuery, null);

        // looping through all rows and adding to list
        if (c.moveToFirst()) {
            do {
                Person p = new Person();
                p.setId_person(c.getInt((c.getColumnIndex(KEY_ID_PERSON))));

                // adding to list
                persons.add(p);
            } while (c.moveToNext());
        }

        return persons;
    }

    }

Person()

public class Person {

    private int id_person;
    private String first_name;
    private String last_name;
    private String phone1;
    private String phone2;
    private int genre;
    private int level;
    private String email;

    public Person(){


    }

    public Person(int id_person, String first_name, String last_name,
            String phone1, String phone2, int genre, int level, String email) {
        super();
        this.id_person = id_person;
        this.first_name = first_name;
        this.last_name = last_name;
        this.phone1 = phone1;
        this.phone2 = phone2;
        this.genre = genre;
        this.level = level;
        this.email = email;
    }

    public int getId_person() {
        return id_person;
    }

    public void setId_person(int id_person) {
        this.id_person = id_person;
    }

    public String getFirst_name() {
        return first_name;
    }

    public void setFirst_name(String first_name) {
        this.first_name = first_name;
    }

    public String getLast_name() {
        return last_name;
    }

    public void setLast_name(String last_name) {
        this.last_name = last_name;
    }

    public String getPhone1() {
        return phone1;
    }

    public void setPhone1(String phone1) {
        this.phone1 = phone1;
    }

    public String getPhone2() {
        return phone2;
    }

    public void setPhone2(String phone2) {
        this.phone2 = phone2;
    }

    public int getGenre() {
        return genre;
    }

    public void setGenre(int genre) {
        this.genre = genre;
    }

    public int getLevel() {
        return level;
    }

    public void setLevel(int level) {
        this.level = level;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

Only insert id_person field, others fields are null.

Aucun commentaire:

Enregistrer un commentaire