【问题标题】:Data Not Inserted in One of the table SQLite数据未插入到 SQLite 表之一中
【发布时间】:2014-08-19 20:12:38
【问题描述】:

在我的应用程序中,我需要两个具有不同数据的数据库表,“TAGS”除外。 我有一个 "days table""setting table" 我只想添加到表数据而不创建新的 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");

【问题讨论】:

  • 什么是乘法表?
  • 这意味着我在一个数据库文件上有两个或多个表
  • db.execSQL(CREATE_TABLE_WORKs); 更改为db.execSQL(CREATE_TABLE_DAYS);
  • 对不起..这是一个错误,我将代码简化为问题。但这不是我的代码错误。我解决了我的问题..

标签: android sql database multiple-columns


【解决方案1】:

编辑

我认为 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

    }

【讨论】:

  • 你应该添加一个解释,而不是仅仅提供代码。
  • 太棒了!但我的代码完美地创建了两个表。但我无法在“SETTING_TABLE”中插入任何内容...
  • addSetting(Clock clock)的方法你做过一步调试吗?
  • 我尝试添加字符串值,而不是使用变量创建的时钟类。它突然起作用了!但是为什么我的时钟课没有呢?我编辑了我的问题并添加了“时钟”类,请看一下。
猜你喜欢
  • 1970-01-01
  • 2015-05-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-05
  • 1970-01-01
  • 2021-01-05
  • 1970-01-01
相关资源
最近更新 更多