2010-09-11 83 views
1

我有兩個類,我的主類和另一個用於處理sqlite。我遇到的問題是當它到達主類的db.open()時崩潰,我不知道爲什麼。Android的SQLite開放數據庫崩潰

主類:

public class RingerSchedule extends Activity 
{ 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main);   
     DBAdapter db = new DBAdapter(this); 
     db.open(); //crash! 
     //more stuff 
    } 
} 

源碼類:

public class DBAdapter 
{ 
    public static final String KEY_ROWID = "id"; 
    public static final String KEY_TO = "to"; 
    public static final String KEY_FROM = "from"; 
    public static final String KEY_DAY = "day";  
    public static final String KEY_FUNCTION = "function";  
    private static final String TAG = "DBAdapter"; 

    private static final String DATABASE_NAME = "myDB"; 
    private static final String DATABASE_TABLE = "schedules"; 
    private static final int DATABASE_VERSION = 1; 

    private final Context context; 

    private DatabaseHelper DBHelper; 
    private SQLiteDatabase db; 

    public DBAdapter(Context ctx) 
    { 
     this.context = ctx; 
     DBHelper = new DatabaseHelper(context); 
    } 

    private static class DatabaseHelper extends SQLiteOpenHelper 
    { 
     DatabaseHelper(Context context) 
     { 
      super(context, DATABASE_NAME, null, DATABASE_VERSION); 
     } 

     @Override 
     public void onCreate(SQLiteDatabase db) 
     { 
      db.execSQL("CREATE TABLE " 
       + DATABASE_TABLE 
       + " (id INT PRIMARY KEY AUTOINCREMENT, to TEXT," 
       + " from TEXT, day TEXT" 
       + " function TEXT);"); 
     } 
     @Override 
     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
     { 
      Log.w(TAG, "Upgrading database from version " + oldVersion + " to " + newVersion + ", which will destroy all old data"); 
      db.execSQL("DROP TABLE IF EXISTS titles"); 
      onCreate(db); 
     } 
    }  

    //opens the database 
    public DBAdapter open() throws SQLException 
    { 
     this.db = DBHelper.getWritableDatabase(); 
     return this; 
    } 

    //closes the database 
    public void close() 
    { 
     DBHelper.close(); 
    } 

    //insert a schedule into the database 
    public long insertSchedule(String from, String to, String day, String function) 
    { 
     ContentValues initialValues = new ContentValues(); 
     initialValues.put(KEY_TO, to); 
     initialValues.put(KEY_FROM, from); 
     initialValues.put(KEY_DAY, day); 
     initialValues.put(KEY_FUNCTION, function); 
     return db.insert(DATABASE_TABLE, null, initialValues); 
    } 

    //deletes a particular schedule 
    public boolean deleteSchedule(long Id) 
    { 
     return db.delete(DATABASE_TABLE, KEY_ROWID + "=" + Id, null) > 0; 
    } 

    //retrieves all the schedules 
    public Cursor getAllSchedules() 
    { 
     return db.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_TO, KEY_FROM, KEY_DAY, KEY_FUNCTION}, null, null, null, null, null, null); 
    } 

    //updates a schedule 
    public boolean updateSchedule(long Id, String to, String from, String day, String function) 
    { 
     ContentValues args = new ContentValues(); 
     args.put(KEY_TO, to); 
     args.put(KEY_FROM, from); 
     args.put(KEY_DAY, day); 
     args.put(KEY_FUNCTION, function); 
     return db.update(DATABASE_TABLE, args, KEY_ROWID + "=" + Id, null) > 0; 
    } 
} 
+0

你能詳細解釋崩潰嗎?它是一個系統重新啓動,異常(如果是這樣,什麼是堆棧跟蹤)等等。你可以看看任何日誌或任何 – 2010-09-11 18:05:05

+0

Im有點新的android開發,所以我不知道如何獲取日誌或stacktrace。 – Woody 2010-09-11 20:37:42

+0

嘗試打開一個終端,並寫在那裏:adb logcat – 2010-09-11 22:20:15

回答

1

你的地方關閉數據庫的主?如果不是,請嘗試關閉它。 :)

0

當您創建表試着從INT改變id類型INTEGER,我不認爲SQLite的識別命令INT

+1

它確實承認它!它認爲任何數據類型都是'INT',其中'INT'是'INTEGER'。 http://www.sqlite.org/datatype3.html#affname – Miikka 2011-11-20 08:26:04