数据未插入其中一个表SQLite中

时间:2014-08-19 20:12:39

标签: android sql database multiple-columns

在我的应用程序中,我需要两个dataBase表,除了" TAGS"之外还有不同的数据。 我有"days table""setting table"我只想添加到表数据而不创建新的DBhandler。

这是我的代码?我做错了什么?请帮帮我......

DbHandler:

public class DbHandler  {

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

    public static final String TABLE_DAYS = "days";
    public static final String TABLE_SETTING = "setting";

    public static final String ID = "_id";
    public static final String TAGS = "tags";

    //DATE
    public static final String DATE_DAY = "day";
    public static final String DATE_MONTH = "month";
    public static final String DATE_YEAR = "year"; 


    //SETTING
    public static final String BASIC_HOUR = "basicHour";
    public static final String BASIC_SEC = "basicSec";
    public static final String EXTRA0 = "extra0";
    public static final String EXTRA1 = "extra1";
    public static final String EXTRA2 = "extra2";



    private static final String CREATE_TABLE_DAYS = "CREATE TABLE "
            + TABLE_DAY + "(" 
            + ID + " INTEGER PRIMARY KEY," 
            + TAGS + " TEXT,"
            + DATE_DAY + " INTEGER,"
            + DATE_MONTH + " INTEGER,"
            + DATE_YEAR + " INTEGER)";


    private static final String CREATE_TABLE_SETTING = "CREATE TABLE "
            + TABLE_SETTING + "(" 
            + ID + " INTEGER PRIMARY KEY," 
            + TAGS   + " TEXT,"
            + BASIC_HOUR + " INTEGER,"
            + BASIC_SEC + " INTEGER,"
            + EXTRA0 + " REAL,"
            + EXTRA1 + " REAL,"
            + EXTRA2 + " REAL)";



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


//INSERT TO DAY TABLE
    public void addDay(Clock clock){
        try {
            open();
            ContentValues cv = new ContentValues();
            cv.put(WORK_NAME, clock.getWorkName());
            cv.put(DATE_DAY, clock.getDateDay());
            cv.put(DATE_MONTH, clock.getDateMonth());
            cv.put(DATE_YEAR, clock.getDateYear());

            myDb.insert(TABLE_DAYS, null, cv); 
        } catch (Exception e) {
            // TODO: handle exception
        }finally{
            close();
        }


    }


        //GET FROM DAY TABLE
    public ArrayList<Clock> getDay(String byTag){
        try {
            open();
            ArrayList<Clock> list = new ArrayList<Clock>();
            Cursor c = null;
            c = myDb.query(TABLE_DAY, null,"tags = ?", new String[] {byTag}, null, null, null);
            while (c.moveToNext()) {
                Clock clock = new Clock(
                        c.getInt(0),
                        c.getString(1),
                        c.getInt(2), 
                        c.getInt(3),
                        c.getInt(4),
                        c.getInt(5));

                list.add(clock);        
            }       
            return list;
        } catch (Exception e) {
            return null;
        }finally{
            close();
        }
    }




    //ADD TO SETTING TABLE
    public void addSetting(Clock clock){
        try {
            open();
            ContentValues cv = new ContentValues();
            cv.put(TAGS, clock.getWorkName());
            cv.put(BASIC_HOUR, clock.getBasicHour());
            cv.put(BASIC_SEC, clock.getBasicSec());
            cv.put(EXTRA0, clock.getExtra0());
            cv.put(EXTRA1, clock.getExtra1());
            cv.put(EXTRA2, clock.getExtra2());

            myDb.insert(TABLE_SETTING, null, cv); 
        } catch (Exception e) {
            // TODO: handle exception
        }finally{
            close();
        }


    }

//GET FROM SETTING TABLE
    public ArrayList<Clock> getSetting(String byTags){
        try {
            open();
            ArrayList<Clock> list = new ArrayList<Clock>();

            Cursor c = myDb.query(TABLE_SETTING, null,"tags= ?", new String[] {byTags}, null, null, null);
            while (c.moveToNext()) {
                Clock clock = new Clock(
                        c.getInt(0),
                        c.getString(1),
                        c.getFloat(2), 
                        c.getFloat(3),
                        c.getInt(4),
                        c.getInt(5), 
                        c.getInt(6));


                list.add(clock);        
            }       
            return list;
        } catch (Exception e) {
            return null;
        }finally{
            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_TABLE_DAYS);
            db.execSQL(CREATE_TABLE_SETTING);
        }

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

}

我的Clock.class:

public class Clock {

    protected  int id = 0;
    protected  String tags;
    protected  int dateDay;
    protected  int dateMonth;
    protected int dateYear;

    protected  int basicHour ; 
    protected  int basicSec ;  
    protected   float extra0 ; 
    protected  float  extra1; 
    protected  float extra2 ; 

    //the constructor for table 1
    public Clock(int id, String tags, int dateDay, int dateMonth, int dateYear,
            int basicHour) {
        super();
        this.id = id;
        this.tags = tags;
        this.dateDay = dateDay;
        this.dateMonth = dateMonth;
        this.dateYear = dateYear;
        this.basicHour = basicHour;
    }
    //the constructor for table 2
    public Clock(int id, String tags, int basicHour, int basicSec,
            float extra0, float extra1, float extra2) {
        super();
        this.id = id;
        this.tags = tags;
        this.basicHour = basicHour;
        this.basicSec = basicSec;
        this.extra0 = extra0;
        this.extra1 = extra1;
        this.extra2 = extra2;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getTags() {
        return tags;
    }
    public void setTags(String tags) {
        this.tags = tags;
    }
    public int getDateDay() {
        return dateDay;
    }
    public void setDateDay(int dateDay) {
        this.dateDay = dateDay;
    }
    public int getDateMonth() {
        return dateMonth;
    }
    public void setDateMonth(int dateMonth) {
        this.dateMonth = dateMonth;
    }
    public int getDateYear() {
        return dateYear;
    }
    public void setDateYear(int dateYear) {
        this.dateYear = dateYear;
    }
    public int getBasicHour() {
        return basicHour;
    }
    public void setBasicHour(int basicHour) {
        this.basicHour = basicHour;
    }
    public int getBasicSec() {
        return basicSec;
    }
    public void setBasicSec(int basicSec) {
        this.basicSec = basicSec;
    }
    public float getExtra0() {
        return extra0;
    }
    public void setExtra0(float extra0) {
        this.extra0 = extra0;
    }
    public float getExtra1() {
        return extra1;
    }
    public void setExtra1(float extra1) {
        this.extra1 = extra1;
    }
    public float getExtra2() {
        return extra2;
    }
    public void setExtra2(float extra2) {
        this.extra2 = extra2;
    }

只有在我传递一个特定字符串时才有效:

DbHandler hand = new DbHandler(this);
hand.addSetting("setting");

hand.addDay("setting");

1 个答案:

答案 0 :(得分:0)

修改

我认为ID不是自动递增的,这就是它没有插入数据库的原因所以将它添加到你的表查询字符串中就像下面提到的那样

private static final String CREATE_TABLE_DAYS = "CREATE TABLE "
            + TABLE_DAY + "(" 
            + ID + " INTEGER PRIMARY KEY AUTOINCREMENT ," 
            + TAGS + " TEXT,"
            + DATE_DAY + " INTEGER,"
            + DATE_MONTH + " INTEGER,"
            + DATE_YEAR + " INTEGER)";


    private static final String CREATE_TABLE_SETTING = "CREATE TABLE "
            + TABLE_SETTING + "(" 
            + ID + " INTEGER PRIMARY KEY AUTOINCREMENT ," 
            + TAGS   + " TEXT,"
            + BASIC_HOUR + " INTEGER,"
            + BASIC_SEC + " INTEGER,"
            + EXTRA0 + " REAL,"
            + EXTRA1 + " REAL,"
            + EXTRA2 + " REAL)";

也看到这个例子。

   public class SessionDescriptionsDatabaseOpenHelper extends SQLiteOpenHelper {

第一张表的字段

   private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "database.db";
    private static final String TABLE_SESSIONDESCRIPTIONS = "session_descriptions";
    private static final String COLUMN_ID = "_id";
    private static final String COLUMN_TITLE = "title";
    private static final String COLUMN_DESCRIPTION = "description";

第二张表的字段

    private static final String TABLE_SESSIONLEADERS = "session_leaders";
    private static final String COLUMN_NAME = "category";
    private static final String COLUMN_ORGANIZATION = "summary";
    private static final String COLUMN_PICURL = "google";

构造

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

第一个表创建字符串

  public static final String DATABASE_CREATE_TABLE1 = "create table "
                    + TABLE_SESSIONDESCRIPTIONS
                    + "("
                    + COLUMN_ID + " integer primary key autoincrement, "
                    + COLUMN_TITLE + " text not null, "
                    + COLUMN_DESCRIPTION + " text not null"
                    + ");";

第二个表创建字符串

       public static final String DATABASE_CREATE_TABLE2 = "create table "
                + TABLE_SESSIONLEADERS
                + "("
                + COLUMN_ID + " integer primary key autoincrement, "
                + COLUMN_NAME + " text not null, "
                + COLUMN_ORGANIZATION + " text not null,"
                + COLUMN_TITLE + " text not null,"
                + COLUMN_DESCRIPTION + " text not null, "
                + COLUMN_PICURL + " text not null"
                + ");";

表创建OnCreate()方法上的SQL语句

    @Override
    public void onCreate(SQLiteDatabase db) {

        db.execSQL(DATABASE_CREATE_TABLE1); // first table creation
        db.execSQL(DATABASE_CREATE_TABLE2); // second table creation

    }