vendredi 23 janvier 2015

Why does't the onCreate() in MySQLiteHelper(extends SQLiteOpenHelper)execute?

I have codes whit sq-lite,but there are some errors.I've a worked several days,but it doesn't work. I write the MySQLiteHelper class(extends SQLiteOpenHelper).



public class MySQLiteHelper extends SQLiteOpenHelper {


public static final String CLASSIFICATION = "CREATE TABLE IF NOT EXISTS classification(classification_id Integer primary key AUTOINCREMENT,classify VARCHAR)";
public static final String ACCOUNT = "CREATE TABLE IF NOT EXISTS account(account_id Integer primary key AUTOINCREMENT,username VARCHAR,password VARCHAR,classification_id Integer REFERENCES classification(classification_id))";
public MySQLiteHelper(Context context, String name, SQLiteDatabase.CursorFactory factory,
int version){
super(context,name,factory,version);

}

@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CLASSIFICATION);
db.execSQL(ACCOUNT);
}

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

}


}


I write the MySQLiteDatabase class.



public class MySQLiteDatabase {
private static MySQLiteDatabase instance;
private SQLiteDatabase sqLiteDatabase;
private MySQLiteDatabase(Context context){
MySQLiteHelper helper = new MySQLiteHelper(context,"test.db",null,1);
sqLiteDatabase = helper.getWritableDatabase();
sqLiteDatabase.execSQL("INSERT INTO classification(classification_id,classify) VALUES(1,'QQ')");
}
public static MySQLiteDatabase getInstance(Context context){
if(null == instance){
instance = new MySQLiteDatabase(context);
}
return instance;
}


} But I write the code in the onCreate() in SampleActivity.



MySQLiteDatabase my = MySQLiteDatabase.getInstance(SampleActivity.this);


the onCreate() in MySQLiteHelper does't execute?


In the other way,I write the follow code in the onCreate() in Sample Activity.The onCreate() in MySQLiteHelper execute.



MySQLiteHelper helper = new MySQLiteHelper(context,"test.db",null,1);


I have try my best to solve it,but I can't.Please help me.


Aucun commentaire:

Enregistrer un commentaire