I'm trying to create a simple Contacts application where I use SQLite DB.
I created to make my own DBHelper class:
public class DBHelper {
public static final String KEY_NAME = "name";
public static final String KEY_ADDRESS = "address";
public static final String KEY_MOBILE = "mobile";
public static final String KEY_HOME = "home";
public static final String KEY_ROW_ID = "_id";
public static final String PROJECTION[] = {
KEY_ROW_ID,
KEY_NAME,
KEY_ADDRESS,
KEY_MOBILE,
KEY_HOME
};
/* The table and database names */
private static final String TABLE_NAME = "mycontacts";
private static final String DATABASE_NAME = "contactDB";
/*SQL code for creating the table*/
private static final String TABLE_CREATE=
"create table "+ TABLE_NAME + "("+ KEY_ROW_ID
+" integer primary key autoincrement,"+
KEY_NAME +" text not null,"+
KEY_ADDRESS + " text not null,"+
KEY_MOBILE + " text,"+
KEY_HOME + " text)";
private static final int DATABASE_VERSION = 1;
private SQLiteDatabase db;
public DBHelper(Context ctx){
db = ctx.openOrCreateDatabase(DATABASE_NAME, SQLiteDatabase.CREATE_IF_NECESSARY, null);
db.execSQL(TABLE_CREATE);
}
...
`enter code here`}
In the constructor of the class, as you can see, I create/open the db, after which I make CREATE_TABLE
query to it in order to create mycontacts
table.
When I run the app (on Android Studio's emulator) I get the following runtime error:
Caused by: android.database.sqlite.SQLiteException: table mycontacts already exists (code 1): , while compiling: create table mycontacts(_id integer primary key autoincrement,name text not null,address text not null,mobile text,home text)
It says that the table has already been created, despite the fact that I put the creation query right after the db's creation. How is that possible?
Aucun commentaire:
Enregistrer un commentaire