Create problems...

Problems with WiFi, SQLite ,Bluetooth, WiMax, Proxies, etc...

Create problems...

Postby Skeniver » Wed Jul 29, 2009 1:44 pm

Hi there

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):

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.       private static final String DATABASE_NAME = "Company.db";
  2.  
  3.         private static final int DATABASE_VERSION = 1;
  4.  
  5.         private static final String TABLE_USERS = "tblUsers";
  6.  
  7.        
  8.  
  9.         public static final String KEY_ID = "_id";
  10.  
  11.         public static final String KEY_USER = "Username";
  12.  
  13.         public static final int COLUMN_USER = 1;
  14.  
  15.         public static final String KEY_EMPLOYEE_NUMBER = "EmployeeNumber";
  16.  
  17.         public static final int COLUMN_EMPLOYEE_NUMBER = 2;
  18.  
  19.        
  20.  
  21.         private SQLiteDatabase db;
  22.  
  23.         private final Context context;
  24.  
  25.         private DBOpenHelper dbHelper;
  26.  
  27.        
  28.  
  29.         public DBAdapter(Context _context) {
  30.  
  31.                 this.context = _context;
  32.  
  33.                 dbHelper = new DBOpenHelper(context, DATABASE_NAME, null, DATABASE_VERSION);
  34.  
  35.         }
  36.  
  37.        
  38.  
  39.         //OpenHelper that decides whether the db needs to be created or upgraded
  40.  
  41.         //NEED TO FIX THE UPGRADE...
  42.  
  43.         private static class DBOpenHelper extends SQLiteOpenHelper {
  44.  
  45.                 //sql statement to create a new db
  46.  
  47.                 private static final String DATABASE_CREATE = "create table " +
  48.  
  49.                                 TABLE_USERS + " (" + KEY_ID + " integer primary key autoincrement, " +
  50.  
  51.                                 KEY_USER + " text not null, " + KEY_EMPLOYEE_NUMBER + " long";
  52.  
  53.                
  54.  
  55.                 public DBOpenHelper(Context context, String name,
  56.  
  57.                                 CursorFactory factory, int version) {
  58.  
  59.                         super(context, name, factory, version);
  60.  
  61.                 }
  62.  
  63.  
  64.  
  65.                 @Override
  66.  
  67.                 public void onCreate(SQLiteDatabase _db) {
  68.  
  69.                         //create the db
  70.  
  71.                         _db.execSQL(DATABASE_CREATE);
  72.  
  73.                 }
  74.  
  75.  
  76.  
  77.                 @Override
  78.  
  79.                 public void onUpgrade(SQLiteDatabase _db, int _oldVersion,
  80.  
  81.                                 int _newVersion) {
  82.  
  83.                         Log.w("DBAdapter", "Upgrading...");
  84.  
  85.                         //drop the old table
  86.  
  87.                         _db.execSQL("DROP TABLE IF EXISTS " + TABLE_USERS);
  88.  
  89.                         onCreate(_db);
  90.  
  91.                 }
  92.  
  93.         }
  94.  
  95.        
  96.  
  97.         //method to open the database (or to try to...)
  98.  
  99.         public void open() throws SQLiteException {
  100.  
  101.                 try {
  102.  
  103.                         db = dbHelper.getWritableDatabase();
  104.  
  105.                 } catch (SQLiteException e) {
  106.  
  107.                         db = dbHelper.getReadableDatabase();
  108.  
  109.                 }
  110.  
  111.         }
  112.  
  113.  
Parsed in 0.039 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
User avatar
Skeniver
Senior Developer
Senior Developer
 
Posts: 106
Joined: Tue Feb 03, 2009 3:51 pm
Location: Canterbury, UK

Top

Postby Skeniver » Wed Jul 29, 2009 2:01 pm

I think it is giving this error during the onCreate for the database. I also changed long to integer in the DATABASE_CREATE (I think SQLite doesn't have long...)

/data/data/com.skeniver.Company1/databases/Company.db SQLiteDatabase created and never closed

But I am no closer to solving... I was also wondering, if I open the DB in Activity A and then open Activity B (to create a new user), do I have to close the connection and then open it again?

Thanks again!
User avatar
Skeniver
Senior Developer
Senior Developer
 
Posts: 106
Joined: Tue Feb 03, 2009 3:51 pm
Location: Canterbury, UK

Top

Return to Networking & Database Problems

Who is online

Users browsing this forum: Google [Bot] and 8 guests