Android DB上的多个表 - 没有这样的表

时间:2013-04-10 20:27:53

标签: android sqlite multiple-tables

有一个奇怪的问题似乎很常见,但我无法找到合适的解决方案。也许你们可以帮我弄清楚我哪里出错了。

这是我的Common DB适配器类

package com.example.mobilebillforecaster;

import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class CommonDBclass {

public static final String DATABASE_NAME = "mbfDB";

public static final int DATABASE_VERSION = 1;

public static final String CREATE_ETI_TABLE =

"CREATE TABLE etisalatdb (_id INTEGER PRIMARY KEY AUTOINCREMENT, "
        + EtiTariffDB.KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
        + EtiTariffDB.KEY_PACKNAME + " TEXT, " + EtiTariffDB.KEY_PACKRENT
        + " INTEGER, " + EtiTariffDB.KEY_SIMRATE + " INTEGER, "
        + EtiTariffDB.KEY_NATMINSBUN + " INTEGER, "
        + EtiTariffDB.KEY_NATMINSRATE + " INTEGER, "
        + EtiTariffDB.KEY_INTMINSBUN + " INTEGER, "
        + EtiTariffDB.KEY_INTMINSRATE + " INTEGER, "
        + EtiTariffDB.KEY_NATSMSBUN + " INTEGER, "
        + EtiTariffDB.KEY_NATSMSRATE + " INTEGER, "
        + EtiTariffDB.KEY_INTSMSBUN + " INTEGER, "
        + EtiTariffDB.KEY_INTSMSRATE + " INTEGER, "
        + EtiTariffDB.KEY_MMSBUN + " INTEGER, " + EtiTariffDB.KEY_MMSRATE
        + " INTEGER, " + EtiTariffDB.KEY_DATALBUN + " INTEGER, "
        + EtiTariffDB.KEY_DATALRATE + " INTEGER" + ");";

public static final String CREATE_DU_TABLE =

"CREATE TABLE dudatabase(_id INTEGER PRIMARY KEY AUTOINCREMENT, "
        + DuTariffDB.KEY_PACKNAME + " TEXT, " + DuTariffDB.KEY_PACKRENT
        + " INTEGER, " + DuTariffDB.KEY_NATMINSRATE + " INTEGER, "
        + DuTariffDB.KEY_NATMOBRATE + " INTEGER, "
        + DuTariffDB.KEY_INTMINSRATE + " INTEGER, "
        + DuTariffDB.KEY_NATSMSRATE + " INTEGER, "
        + DuTariffDB.KEY_INTSMSRATE + " INTEGER, "
        + DuTariffDB.KEY_DATALBUN + " INTEGER, " + DuTariffDB.KEY_DATALRATE
        + " INTEGER" + ");";

private final Context context;
private DatabaseHelper DBHelper;
private SQLiteDatabase db;

public CommonDBclass(Context ctx) {
    this.context = ctx;
    this.DBHelper = new DatabaseHelper(this.context);
}

private static class DatabaseHelper extends SQLiteOpenHelper {
    DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_ETI_TABLE);
        db.execSQL(CREATE_DU_TABLE);

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
}

public CommonDBclass open() throws SQLException {
    this.db = this.DBHelper.getWritableDatabase();

    return this;
}

public void close() {
    this.DBHelper.close();
}

}

,这是我的适配器之一

package com.example.mobilebillforecaster;

import java.util.ArrayList;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class EtiTariffDB {

public static final String KEY_ROWID = "_id";
public static final String KEY_PACKNAME = "pack_name";
public static final String KEY_PACKRENT = "pack_rent";
public static final String KEY_SIMRATE = "sim_rate";
public static final String KEY_NATMINSBUN = "natmins_bun";
public static final String KEY_NATMINSRATE = "natmins_rate";
public static final String KEY_INTMINSBUN = "intmins_bun";
public static final String KEY_INTMINSRATE = "intmins_rate";
public static final String KEY_NATSMSBUN = "natsms_bun";
public static final String KEY_NATSMSRATE = "natsms_rate";
public static final String KEY_INTSMSBUN = "intsms_bun";
public static final String KEY_INTSMSRATE = "intsms_rate";
public static final String KEY_MMSBUN = "mms_bun";
public static final String KEY_MMSRATE = "mms_rate";
public static final String KEY_DATALBUN = "datal_bun";
public static final String KEY_DATALRATE = "datal_rate";

private static final String DATABASE_TABLE = "etitarifftable";

private DbHelper ourHelper;
private final Context cctext;

private SQLiteDatabase ourDatabase;

private static class DbHelper extends SQLiteOpenHelper {

    DbHelper(Context ourContext) {
        super(ourContext, CommonDBclass.DATABASE_NAME, null,
                CommonDBclass.DATABASE_VERSION);

    }

    @Override
    public void onCreate(SQLiteDatabase db) {

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub

    }

}

public EtiTariffDB(Context ctx) {
    this.cctext = ctx;

}

public EtiTariffDB open() {
    this.ourHelper = new DbHelper(this.cctext);
    this.ourDatabase = this.ourHelper.getWritableDatabase();
    return this;
}

public void close() {
    this.ourHelper.close();
}

public long createEtiTariff(String packname, String packrent,
        String simrent, String natbun, String natrate, String intbundle,
        String intrate, String natsmsbun, String natsmsrate,
        String intsmsbun, String intsmsrate, String mmsbun, String mmsrate,
        String datalbun, String datalrate) {
    ContentValues cv = new ContentValues();
    cv.put(KEY_PACKNAME, packname);
    cv.put(KEY_PACKRENT, packrent);
    cv.put(KEY_SIMRATE, simrent);
    cv.put(KEY_NATMINSBUN, natbun);
    cv.put(KEY_NATMINSRATE, natrate);
    cv.put(KEY_INTMINSBUN, intbundle);
    cv.put(KEY_INTMINSRATE, intrate);
    cv.put(KEY_NATSMSBUN, natsmsbun);
    cv.put(KEY_NATSMSRATE, natsmsrate);
    cv.put(KEY_INTSMSBUN, intsmsbun);
    cv.put(KEY_INTSMSRATE, intsmsrate);
    cv.put(KEY_MMSBUN, mmsbun);
    cv.put(KEY_MMSRATE, mmsrate);
    cv.put(KEY_DATALBUN, datalbun);
    cv.put(KEY_DATALRATE, datalrate);

    return this.ourDatabase.insert(DATABASE_TABLE, null, cv);

}

这是我调用DB

的java文件的一部分
    EtiTariffDB etisalatDB = new EtiTariffDB(this);

    etisalatDB.open();

    etisalatDB.createEtiTariff(etipackname, etipackrent, etisimrent,
            etinatbun, etinatrate, etiintbundle, etiintrate,
            etinatsmsbundle, etinatsmsrate, etiintsmsbundle,
                            etiintsmsrate,etimmsbundle, etimmsrate, 
                            etidatalocalbundle, etidatalocalrate);

    etisalatDB.close();

我得到的错误是:

04-10 20:17:47.958: E/SQLiteLog(30526): (1) no such table: etitarifftable
04-10 20:17:48.078: E/SQLiteDatabase(30526): Error inserting natmins_rate=0.30 pack_name=Corporate package natsms_bun=200 mms_rate=0.45 mms_bun=- intsms_rate=0.60 natsms_rate=0.18 intsms_bun=- sim_rate=29 intmins_bun=100 datal_rate=0.1 datal_bun=2000 intmins_rate=1.10 natmins_bun=100 pack_rent=249
04-10 20:17:48.078: E/SQLiteDatabase(30526): android.database.sqlite.SQLiteException: no such table: etitarifftable (code 1): , while compiling: INSERT INTO etitarifftable(natmins_rate,pack_name,natsms_bun,mms_rate,mms_bun,intsms_rate,natsms_rate,intsms_bun,sim_rate,intmins_bun,datal_rate,datal_bun,intmins_rate,natmins_bun,pack_rent) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)

我读了很多帖子并经历了很多论坛。我用来创建它的例子可以在这里找到:

Multiple Table SQLite DB Adapter(s) in Android?

我检查并重新检查了我的编码。我怀疑我没有在create方法上调用commonDBclass,但我无法弄清楚如何。请帮忙。

感谢。

2 个答案:

答案 0 :(得分:1)

我马上发现了一些问题:

  1. 您根本没有String来创建名为etitarifftable的表。
  2. 您没有执行CREATE TABLE字符串。
  3. 您正在更改Java代码,但没有告诉SQLiteOpenHelper更新SQL架构。
  4. 表格只能有一个主键
  5. 简单的解决方案:

    1. 创建etitarifftable或在查询中使用相应的表名。
    2. 将相应的execSQL()语句添加到DbHelper#onCreate()

      @Override
      public void onCreate(SQLiteDatabase db) {
          db.execSQL(CREATE_ETI_TABLE);
          db.execSQL(CREATE_DU_TABLE);
      }
      
    3. 增量DATABASE_VERSION将调用onUpgrade()。最基本的形式onUpgrade()应如下所示:

      @Override
      public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
          db.execSQL("DROP TABLE IF EXISTS etisalatdb");
          db.execSQL("DROP TABLE IF EXISTS dudatabase");
          onCreate(db);
      }
      
    4. 从两个表中删除一个主键声明:

      "CREATE TABLE etisalatdb (EtiTariffDB.KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + ...
      
    5. 最后,我感觉还有更多的错误,但是你的LogCat会引导你直接找到它们。 (另外我不确定为什么你有两个不同的适配器......)

答案 1 :(得分:0)

您需要在插入记录之前创建表,您可以通过以下方式创建表:

class DbHelper  extends SQLiteOpenHelper {
    ...

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE " + DATABASE_TABLE + " {definition} ");
    }

    ...
}

它看起来不像你的CommonDBclass。我认为你需要重新思考和重组你的方式。 EtiTariffDB不应该是一个“数据库”它是一个表,你应该把它当作一个表。使用CommonDBclass获取EtiTariff表对象而不是相反。然后你可以做类似的事情:

DatabaseHelper db = new DatabaseHelper();
EtiTariffTable etiTariff = db.getEtiTariffTable();