【发布时间】: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