无法插入带有类扩展的Sql表

时间:2014-08-20 01:04:04

标签: android abstract-class extends sqliteopenhelper

我先前问过这个问题,但我没有回答,因为我不清楚。 我创建了一个模型类" People"使用一些变量和另一个类来扩展People类。 当我试图插入表格时,似乎只有一个表是空的。这是我的榜样,因为我不善于解释......

并且代码有一个文本:

People.Class

public abstract class People {

int id;
String tags;
String names;
String age;
String street;

public People(int id, String tags) {
    super();
    this.id = id;
    this.tags = tags;
}

public People(int id, String names, String age, String street) {
    super();
    this.id = id;
    this.names = names;
    this.age = age;
    this.street = street;
}

public int getId() {
    return id;
}
public String getTags() {
    return tags;
}
public String getNames() {
    return names;
}
public String getAge() {
    return age;
}
public String getStreet() {
    return street;
}

}

SomeClass.Class

public class SomeClass extends People {

    public SomeClass(int id, String tags) {
        super(id, tags);
    }

    public SomeClass(int id, String names, String age, String street) {
        super(id, names, age, street);
    }

}

Dbahndler.Class

public class DbHandler  {

    public static final String DB_NAME = "myShifts.db";
    public static final int VERSION = 1;

    public static final String TABLE1 = "theTab1";
    public static final String TABLE2= "theTab2";

    public static final String ID = "_id";
    public static final String NAMES = "names";
    public static final String TAGS = "tags";
    public static final String AGES = "ages";
    public static final String STREET = "street";


    private static final String CREATE_TABLE1= "CREATE TABLE "
            + TABLE1 + "(" 
            + ID + " INTEGER PRIMARY KEY,"
            +TAGS + " TEXT)";

    private static final String CREATE_TABLE2 = "CREATE TABLE "
            + TABLE2 + "(" 
            + ID + " INTEGER PRIMARY KEY," 
            +NAMES + " TEXT,"
            +AGES+ " TEXT,"
            +STREET + " TEXT)";



    Dbhelper helper;
    SQLiteDatabase myDb;
    Context context;

    public DbHandler(Context context) {
        this.context = context;
        helper = new Dbhelper(context);
    }

    public DbHandler open(){
        myDb = helper.getWritableDatabase();
        return this;
    }
    public void close(){
        helper.close();
    }


    public void add1(People people){
            open();
            ContentValues cv = new ContentValues();
            cv.put(TAGS, people.getTags());
            myDb.insert(TABLE1, null, cv);
            close();
    }
    public void add2(People people){
            open();
            ContentValues cv = new ContentValues();
            cv.put(NAMES, people.getNames());
            cv.put(AGES, people.getAge());
            cv.put(STREET, people.getStreet());
            myDb.insert(TABLE2, null, cv);
            close();
    }





    public class Dbhelper extends SQLiteOpenHelper{

        public Dbhelper(Context context) {
            super(context, DB_NAME, null, VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL(CREATE_TABLE1);
            db.execSQL(CREATE_TABLE2);
        }

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


}

MainActivity.Class

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        DbHandler hand = new DbHandler(this);
        hand.add1(new SomeClass(0, "theTags"));
        hand.add2(new SomeClass(0, "dave", "22", "90210"));


    }

http://www.speedyshare.com/baFkE/Test.7z

0 个答案:

没有答案