gpt4 book ai didi

java - Android Sqlite db.insert 带 null

转载 作者:行者123 更新时间:2023-12-01 10:59:34 43 4
gpt4 key购买 nike

问题是当我去插入数据库sqlite时,因为当我返回所有“人”的数据时,只插入id_persons,所有其他字段都是空:(。

这是我的 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;
}

}

人()

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;
}

只插入id_person字段,其他字段为空。

最佳答案

你的 do-while 循环位于 getAllPersons()仅捕获游标中每一行的 id 列。 Person中的所有其他字段保留默认值,即 null。

关于java - Android Sqlite db.insert 带 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33442208/

43 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com