无法使用SqliteOpenHelper创建表

时间:2016-08-28 18:02:13

标签: android android-sqlite sqliteopenhelper

public Database(@NonNull Context _context, @NonNull String _tableName){
    super(_context, DB_NAME, null, DB_VERSION);
    this.mContext = _context;
    this.tableName = _tableName;
   this.db_create =  "CREATE TABLE " + tableName + "(" + COLUMN_ID + " INTEGER NOT NULL PRIMARY KEY, " +
              COLUMN_DATA + " BLOB )";
}


@Override
public void onCreate(SQLiteDatabase db) {
    try{
        Log.d("tableCreate", "onCreate: created");
        db.execSQL(db_create);
    }catch(SQLiteException ex){

        ex.printStackTrace();
        Log.d("TableCreation", "onCreate: cannot create");

    }
}
public long insert(Model model){
    SQLiteDatabase db = getWritableDatabase();
    long id = -1;

    try{
        if(maxData > 0 || maxData <= counter){
            ContentValues value = new ContentValues();
            value.put(COLUMN_ID, model.getCode());
            value.put(COLUMN_DATA, model.toJson().getBytes());
            counter += 1;
            id = db.insert(tableName, null, value); //exception is thrown here.
        }
    }catch(SQLiteException ex){
        ex.printStackTrace();
    }finally{
        if(db != null)
            db.close();
    }

    return id;
   }
}

我有一个扩展SQLiteHelper的Database类。每当我尝试在表中插入一个项目时,总会抛出异常。错误消息

no such table: university_basic
08-28 23:26:23.036 30754-30786/com.ujjwal.univhub E/SQLiteDatabase: Error inserting ...
android.database.sqlite.SQLiteException: no such table: university_basic (code 1): , while compiling: INSERT INTO university_basic(id,model) VALUES (?,?)

从文档和其他在线资源中我发现在调用onCreate()getWriteableDatabase()并且表不存在时调用getReadableDatabase()方法。我在getWriteableDatabase()方法中调用insert(),如果不存在或打开数据库,可能应该创建表。但每次都会抛出错误消息。数据库实例是在我的应用程序的一个Activity中创建的。

Connectivity.OfflineDataLoader offlineLoader = connectivity.new OfflineDataLoader(this,
            "university_basic", 30);
connectivity.enableOfflineData(offlineLoader);

OfflineLoader的构造函数之一是

 public OfflineDataLoader(@NonNull  Context _context, String tableName,int maxData){
            this.context = _context;
            this.tableName = (tableName != null)? tableName : "university_basic";
            this.database = new Database(_context, this.tableName);
            this.database.setMaxData(maxData);
  }

为什么这里没有创建表?

2 个答案:

答案 0 :(得分:0)

在主活动中,通过调用构造函数启动数据库帮助程序类。

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);       
    mHelper = new MyDbHelper(this); }

现在在onCreate下的数据库管理器类中添加代码来创建数据库。

public class MyDbHelper extends SQLiteOpenHelper {
private static final String DB_NAME = "mydb";
private static final int DB_VERSION = 1;
public static final String TABLE_NAME = "people";
public static final String COL_NAME = "pName";
public static final String COL_DATE = "pDate";
private static final String STRING_CREATE = "CREATE TABLE "
        + TABLE_NAME + " (_id INTEGER PRIMARY KEY AUTOINCREMENT, "
        + COL_NAME + " TEXT, " + COL_DATE + " DATE);";


public MyDbHelper(Context context) { super(context, DB_NAME, null, DB_VERSION);}

@Override
public void onCreate(SQLiteDatabase db) {
    //Create the database table
    db.execSQL(STRING_CREATE);
    //You may also load initial values into the database here
    ContentValues cv = new ContentValues(2);
    cv.put(COL_NAME, "John Doe");
    //Create a formatter for SQL date format
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    cv.put(COL_DATE, dateFormat.format(new Date())); //Insert 'now' as the date
    db.insert(TABLE_NAME, null, cv);
  }

}

答案 1 :(得分:0)

问题在于数据库退出但没有表。所以卸载并重新安装应用程序解决了这个问题。