SQLite - 在创建数据库时获取FC

时间:2011-04-20 01:32:01

标签: android database sqlite oncreate

无视这个问题,我发现了我的问题...只是编辑了问题以包含正确的代码,以防有人寻找正确的数据库样本:D

每次运行时我的应用程序都会关闭。创建数据库时只有FC,但如果我在创建数据库时注释该行,则应用运行正常。 (参见下面的代码供参考)

如果我导航到/data/data/com.mypackage/databases/,数据库实际上就在那里,它就会被创建,甚至是带有元组的表,所以正因为如此,我相信我的db类很好。我必须在主类中做错事(也许不是??)

主类,这是我实际创建数据库对象的地方

public class myClass extends MapActivity {

 DataHelper dbh; // declaring database

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
           // =======
           // more code here
           // =======
    this.dbh = new DataHelper(this); // this line gives my app an FC, if I comment this line my app runs fine
}
           // =======
           // more code here
           // =======
}

数据库类:

        public class DataHelper {
               private static final String DATABASE_NAME = "track.db";
               private static final int DATABASE_VERSION = 1;
               private static final String TABLE_NAME = "info";

               private Context context;
               private SQLiteDatabase db;

               private static final String
                      sportsType = "sType",
                      units = "units",
                      date = "date",
                      tTime = "tTime";

               private SQLiteStatement insertStmt;
               private static final String INSERT = "insert into " 
                  + TABLE_NAME + "(" + sType + ", " +
                                   units + ", " +
                               date + ", " +
                               tTime + ") values (?,?,?,?)";

               public DataHelper(Context context) {
                  this.context = context;
                  OpenHelper openHelper = new OpenHelper(this.context);
                  this.db = openHelper.getWritableDatabase();
                  this.insertStmt = this.db.compileStatement(INSERT);
               }

               public long insert(String sType, String units, String date, String tTime) {
                  this.insertStmt.bindString(1, sType);
                  this.insertStmt.bindString(2, units);
                  this.insertStmt.bindString(3, date);
                  this.insertStmt.bindString(4, tTime);
                  return this.insertStmt.executeInsert();
               }

               public void deleteAll() {
                  this.db.delete(TABLE_NAME, null, null);
               }

               public List<String> selectAll() {
                  List<String> list = new ArrayList<String>();
                  Cursor cursor = this.db.query(TABLE_NAME, new String[] { sType, 
                                                units,
                                                date,
                                                tTime },
                    null, null, null, null, null);
                  if (cursor.moveToFirst()) {
                     do {
                        list.add(cursor.getString(0));
                        list.add(cursor.getString(1));
                        list.add(cursor.getString(2));
                        list.add(cursor.getString(3));
                     } while (cursor.moveToNext());
                  }
                  if (cursor != null && !cursor.isClosed()) {
                     cursor.close();
                  }
                  return list;
               }

               private static class OpenHelper extends SQLiteOpenHelper {

                  OpenHelper(Context context) {
                     super(context, DATABASE_NAME, null, DATABASE_VERSION);
                  }

                  @Override
                  public void onCreate(SQLiteDatabase db) {
                     db.execSQL("CREATE TABLE " + TABLE_NAME + "(_id INTEGER PRIMARY KEY, " +
                                              sType + " TEXT, " +
                                              units + " TEXT, " +
                                            date + " TEXT, " +
                                            tTime + " TEXT)");
                  }

                  @Override
                  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
                     Log.w("Example", "Upgrading database, this will drop tables and recreate.");
                     db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
                     onCreate(db);
                  }
               }
            }

你们认为我做错了什么?

谢谢:)

0 个答案:

没有答案