我在MainActivity中有这个
db = new Database(this);
还有Database.java:
public class Database extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "days.db";
public static final String TABLE_NAME = "days";
public static final String COLUMN_1 = "date";
public static final String COLUMN_2 = "blob";
private Context context;
public Database(Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table IF NOT EXISTS " + TABLE_NAME + " (" + COLUMN_1 + " TEXT, " + COLUMN_2 + " BLOB)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
onCreate(db);
}
}
但是,如果我在SQLite DB浏览器中打开数据库,则该数据库为空。未创建表。谁能复制粘贴我的代码并加以修复?
答案 0 :(得分:2)
尝试
SQLiteDatabase db = new Database(this).getWritableDatabase();
getWritableDatabase ()
调用此方法时,将调用所需的方法,例如onCreate(SQLiteDatabase),onUpgrade(SQLiteDatabase,int,int)和onOpen(SQLiteDatabase),并创建表。
答案 1 :(得分:0)
尝试一下:我在这里放了一个简单的数据库,因为它对我有用
public class Database extends SQLiteOpenHelper {
private Context context1;
public Database(Context context) {
super(context, "CREATECONTACT", null, 1);
this.context1 = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
String query = "CREATE TABLE createContact ( \n" +
" ID INTEGER PRIMARY KEY AUTOINCREMENT\n" +
" NOT NULL\n" +
" UNIQUE,\n" +
" Name VARCHAR( 50 ) NOT NULL,\n" +
" Phone VARCHAR NOT NULL,\n" +
" typeofPhone VARCHAR NOT NULL,\n" +
" Email VARCHAR NOT NULL,\n" +
" typeofEmail VARCHAR NOT NULL,\n" +
" FAVE VARCHAR NOT NULL,\n" +
" RED INTEGER,\n" +
" GREEN INTEGER,\n" +
" BLUE INTEGER \n" +
");\n";
db.execSQL(query);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS createContact");
onCreate(db);
}