2012-04-03 16 views
2

アンドロイドのSQLiteテーブルに値をロードしようとしています。私のアプリケーションは壊れ続けますが、それはテーブルが正しく作成されていないと信じているNullPointerException以外の特定のエラーをスローしません。AndroidでSQLiteテーブルを作成する

public class MySQLiteHelper{ 


     private static final String DATABASE_NAME = "breadcrumbs.db"; 
     private static final int DATABASE_VERSION = 1; 
     public static final String TABLE_BREADCRUMBS = "breadcrumbs"; 
     private static Context context; 
     static SQLiteDatabase db; 
     private static SQLiteStatement insertStmt; 
     private static final String INSERT ="insert into " + TABLE_BREADCRUMBS + " (time,lat,lon) values (?,?,?)"; 


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

     } 

     public static long insert(long time,int lat,int lon) { 
      insertStmt.bindLong(1, time); 
      insertStmt.bindLong(2, lat); 
      insertStmt.bindLong(3, lon); 
      return insertStmt.executeInsert(); 
     } 

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

     public List<String[]> selectAll() 
     { 
      List<String[]> list = new ArrayList<String[]>(); 
      Cursor cursor = db.query(TABLE_BREADCRUMBS, new String[] 
        { "id","time","lat","lon"}, null, null, null, null, null); 
      int x=0; 
      if (cursor.moveToFirst()) { 
       do { 
        String[] b1=new String[]{cursor.getString(0),cursor.getString(1),cursor.getString(2), 
    cursor.getString(3)}; 
        list.add(b1); 
        x=x+1; 
       } while (cursor.moveToNext()); 
      } 
      if (cursor != null && !cursor.isClosed()) { 
       cursor.close(); 
      } 
      cursor.close(); 
      return list; 
     } 

     public void delete(int rowId) { 
      db.delete(TABLE_BREADCRUMBS, null, null); 
     } 


    private static class OpenHelper extends SQLiteOpenHelper { 
      OpenHelper(Context context) { 
       super(context, DATABASE_NAME, null, DATABASE_VERSION); 
      } 
      public void onCreate(SQLiteDatabase db) { 
       db.execSQL("CREATE TABLE " + TABLE_BREADCRUMBS + " (id INTEGER PRIMARY KEY, tim REAL, lat REAL, long REAL);"); 
       //db.execSQL("CREATE TABLE " + TABLE_BREADCRUMBS + " (id INTEGER PRIMARY KEY AUTOINCREMENT, time REAL, lat REAL, long REAL);"); 
      } 

      @Override 
      public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
      { 
       db.execSQL("DROP TABLE IF EXISTS " + TABLE_BREADCRUMBS); 
       onCreate(db); 


      }  
    } 

そして、私は別のクラスでメソッドを呼び出しています:おそらく、私は何とかOpenHelperを呼び出す必要が

@Override 
      public void onLocationChanged(Location location) { 
       int lat = (int) (location.getLatitude() * 1E6); 
       int lng = (int) (location.getLongitude() * 1E6); 
       long time = (int)(location.getTime() * 1E6); 
       GeoPoint point = new GeoPoint(lat, lng); 
       createMarker(); 
       MySQLiteHelper.insert(time,lat,lng); 
       mapController.animateTo(point); // mapController.setCenter(point); 

      } 
} 

+0

また、logcat –

答えて

3

あなたのクラスを拡張する必要がありますSQLiteOpenHelper私は信じています。

5

SQLiteOpenHelper &オーバーライド二つの方法onCreate()代わりに "時間" のカラム名 "ティム" と一致するINSERT文を変更することにより、固定onUpgrade()

You can follow this really helpful tutorial

+0

を置く必要があります。このチュートリアルを使用してSMSのような巨大なテキストを保存しようとしましたか? – Maha

関連する問題