This is my first post, for my first attempt at an Android app (other than tuts)...
I am trying to create a database with a table containing a username and employee number, but it doesn't seemt o be working. I am not incredibly adept with the Eclipse debugger, so can't give any errors, other then it reports 'success = false'.
Here are some snippets of the code (I included all bit I though were relevant):
Using java Syntax Highlighting
- private static final String DATABASE_NAME = "Company.db";
- private static final int DATABASE_VERSION = 1;
- private static final String TABLE_USERS = "tblUsers";
- public static final String KEY_ID = "_id";
- public static final String KEY_USER = "Username";
- public static final int COLUMN_USER = 1;
- public static final String KEY_EMPLOYEE_NUMBER = "EmployeeNumber";
- public static final int COLUMN_EMPLOYEE_NUMBER = 2;
- private SQLiteDatabase db;
- private final Context context;
- private DBOpenHelper dbHelper;
- public DBAdapter(Context _context) {
- this.context = _context;
- dbHelper = new DBOpenHelper(context, DATABASE_NAME, null, DATABASE_VERSION);
- }
- //OpenHelper that decides whether the db needs to be created or upgraded
- //NEED TO FIX THE UPGRADE...
- private static class DBOpenHelper extends SQLiteOpenHelper {
- //sql statement to create a new db
- private static final String DATABASE_CREATE = "create table " +
- TABLE_USERS + " (" + KEY_ID + " integer primary key autoincrement, " +
- KEY_USER + " text not null, " + KEY_EMPLOYEE_NUMBER + " long";
- public DBOpenHelper(Context context, String name,
- CursorFactory factory, int version) {
- super(context, name, factory, version);
- }
- @Override
- public void onCreate(SQLiteDatabase _db) {
- //create the db
- _db.execSQL(DATABASE_CREATE);
- }
- @Override
- public void onUpgrade(SQLiteDatabase _db, int _oldVersion,
- int _newVersion) {
- Log.w("DBAdapter", "Upgrading...");
- //drop the old table
- _db.execSQL("DROP TABLE IF EXISTS " + TABLE_USERS);
- onCreate(_db);
- }
- }
- //method to open the database (or to try to...)
- public void open() throws SQLiteException {
- try {
- db = dbHelper.getWritableDatabase();
- } catch (SQLiteException e) {
- db = dbHelper.getReadableDatabase();
- }
- }
Parsed in 0.037 seconds, using GeSHi 1.0.8.4
During the onCreate, it is unsuccessful and in the open method, the .getWriteableDatabase is failing (obviously cos I haven't created a database...)
Any help with this would be most appreciated
Thanks in advance
Neil

