2015-04-01 87 views
0

hye那裏,android studio中的sqlite連接sqlite

我的項目讀取sqlite數據庫中的latitute和longitute。首先,我不能連接到數據庫sqlite的連接。第二次連接成功後,我不知道如何循環這個谷歌地圖中的所有數據。在我的數據庫

例一組數據:

_id DateOc TimeOc位置經緯度

1 1961年5月11日上午04點半冷力,金馬侖高原,彭亨4.415895 101.383082

在此先感謝誰幫我, 我明白, 真誠, Hafizul禮

private GoogleMap mMap; // Might be null if Google Play services APK is not available. 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.activity_maps); 
    setUpMapIfNeeded(); 
} 

@Override 
protected void onResume() { 
    super.onResume(); 
    setUpMapIfNeeded(); 
} 

private void setUpMapIfNeeded() { 
    // Do a null check to confirm that we have not already instantiated the map. 
    if (mMap == null) { 
     // Try to obtain the map from the SupportMapFragment. 
     mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)) 
       .getMap(); 
     // Check if we were successful in obtaining the map. 
     if (mMap != null) { 
      setUpMap(); 
     } 
    } 
} 


private void setUpMap() { 

    int i = 1; 
    if(i==1){ 
     SQLiteDatabase myDB = null; 
     String TableName = "Landslide"; 

     //String Data=""; 

     myDB = this.openOrCreateDatabase("Landslide", MODE_PRIVATE, null); 

     Cursor c = myDB.rawQuery("SELECT * FROM " + TableName, null); 

     double Column1 = c.getColumnIndex("Latitude"); 
     double Column2 = c.getColumnIndex("Longitude"); 
     //int Column3 = c.getColumnIndex("Location"); 


     mMap.addMarker(new MarkerOptions().position(new LatLng(Column1 , Column2)).title("Zasss")); 
    } 

    //Loop through all Results 
    //do { 
    //Data =Data +a+"|"+b+"\n"; 
    // }while(c.moveToNext()); 
    //} 

    //double lat = 4.415895; 
    //double lng = 101.383082; 
    else 

     //mMap.addMarker(new MarkerOptions().position(new LatLng(Column1, Column2)).title("Column3")); 
     mMap.addMarker(new MarkerOptions().position(new LatLng(3.37376388889, 101.615391667)).title("Kg. Sri Serendah")); 
    } 
} 
+0

您是否創建了數據庫? – Keshav1234 2015-04-01 08:17:12

+0

是的。我創建了數據庫。我把數據/數據/ com.map /數據庫/ Landslide.sqlite – 2015-04-01 08:37:52

+0

不理解你,你不需要把你的數據庫那裏它自動去那裏,你有擴展SQLiteOpenHelper類嗎? – Keshav1234 2015-04-01 08:44:03

回答

1

你的東東d以創建數據庫文件,如下所示:

public class SQLiteDataBaseAdapter extends Activity { 

    SQLiteHelper helper; 
    SQLiteDatabase db, db1; 

    long id, id1, id2; 


    public SQLiteDataBaseAdapter(Context context) { 
     helper = new SQLiteHelper(context); 

    } 


    // Inserting Data into the DaTaBase 
    public long insertData(String task_name, String contact_name, String contact_number, String description, String remarks, 
          String date, String time, String est_comp_date, String est_comp_time, String act_comp_date, String act_comp_time, String notify_date, String notify_time) { 

     db = helper.getWritableDatabase(); 

     ContentValues contentValues = new ContentValues(); 

     contentValues.put(SQLiteHelper.TASK_NAME, task_name); 
     contentValues.put(SQLiteHelper.CONTACT_NAME, contact_name); 
     contentValues.put(SQLiteHelper.CONTACT_NUMBER, contact_number); 
     //contentValues.put(SQLiteHelper.CONTACT_EMAIL, contact_email); 
     contentValues.put(SQLiteHelper.DESCRIPTION, description); 
     contentValues.put(SQLiteHelper.REMARKS, remarks); 
     contentValues.put(SQLiteHelper.DATE, date); 
     contentValues.put(SQLiteHelper.TIME, time); 
     contentValues.put(SQLiteHelper.ESTIMATED_COMPLETION_DATE, est_comp_date); 
     contentValues.put(SQLiteHelper.ESTIMATED_COMPLETION_TIME, est_comp_time); 
     contentValues.put(SQLiteHelper.ACTUAL_COMPLETION_DATE, act_comp_date); 
     contentValues.put(SQLiteHelper.ACTUAL_COMPLETION_TIME, act_comp_time); 
     contentValues.put(SQLiteHelper.NOTIFY_DATE, notify_date); 
     contentValues.put(SQLiteHelper.NOTIFY_TIME, notify_time); 


     id = db.insert(SQLiteHelper.TABLE_NAME, null, contentValues); 


     Log.d("PaNa", "The value of Id is " + id); 

     db.close(); 
     return id; 
    } 




    public Cursor getAllData() { 
     db = helper.getWritableDatabase(); 
     String[] columns = {SQLiteHelper.UID, 
       SQLiteHelper.TASK_NAME, 
       SQLiteHelper.CONTACT_NAME, 
       SQLiteHelper.CONTACT_NUMBER, 
       //SQLiteHelper.CONTACT_EMAIL, 
       SQLiteHelper.DESCRIPTION, 
       SQLiteHelper.REMARKS, 
       SQLiteHelper.DATE, 
       SQLiteHelper.TIME, 
       SQLiteHelper.ESTIMATED_COMPLETION_DATE, 
       SQLiteHelper.ESTIMATED_COMPLETION_TIME, 
       SQLiteHelper.ACTUAL_COMPLETION_DATE, 
       SQLiteHelper.ACTUAL_COMPLETION_TIME, 
       SQLiteHelper.NOTIFY_DATE, 
       SQLiteHelper.NOTIFY_TIME}; 
     Cursor cursor = db.query(SQLiteHelper.TABLE_NAME, columns, null, null, 
       null, null, null); 
     StringBuffer buffer = new StringBuffer(); 
     while (cursor.moveToNext()) { 
      int cid = cursor.getInt(0); 
      String taskName = cursor.getString(1); 
      String est_comp_time = cursor.getString(6); 
      buffer.append(cid + " " + taskName + " " + est_comp_time + "\n"); 
     } 

     if (cursor != null) { 
      cursor.moveToFirst(); 
     } 

     return cursor; 


    } 



    //Obtain a single row 
    public Task getTask(String id) { 

     Log.d("Pana", "The value of id is " + String.valueOf(id)); 
     db = helper.getReadableDatabase(); 

     String[] columns = {SQLiteHelper.UID, 
       SQLiteHelper.TASK_NAME, SQLiteHelper.CONTACT_NAME, SQLiteHelper.CONTACT_NUMBER, SQLiteHelper.DESCRIPTION, SQLiteHelper.REMARKS, SQLiteHelper.DATE, SQLiteHelper.TIME, SQLiteHelper.ESTIMATED_COMPLETION_DATE, SQLiteHelper.ESTIMATED_COMPLETION_TIME, 

       SQLiteHelper.ACTUAL_COMPLETION_DATE, SQLiteHelper.ACTUAL_COMPLETION_TIME, SQLiteHelper.NOTIFY_DATE, SQLiteHelper.NOTIFY_TIME}; 

     Cursor cursor = db.query(SQLiteHelper.TABLE_NAME, columns, SQLiteHelper.UID + "= ?", 
       new String[]{String.valueOf(id)}, null, null, null, null); 


     if (cursor != null) 
      cursor.moveToFirst(); 

     Task task = new Task(cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4), cursor.getString(5), 
       cursor.getString(6), cursor.getString(7), cursor.getString(8), cursor.getString(9), cursor.getString(10), 
       cursor.getString(11), cursor.getString(12), cursor.getString(13)); 

     cursor.close(); 

     return task; 

    } 





} 




class SQLiteHelper extends SQLiteOpenHelper { 

    public static final String DATABASE_NAME = "task_management_database"; 
    public static final String TABLE_NAME = "DataTable"; 
    public static final String UID = "_id"; 
    public static final String TASK_NAME = "Task_name"; 
    public static final String CONTACT_NAME = "Contact_Name"; 
    public static final String CONTACT_NUMBER = "Contact_Number"; 
    public static final String CONTACT_EMAIL = "Contact_Email"; 
    public static final String DESCRIPTION = "Description"; 
    public static final String REMARKS = "Remarks"; 
    public static final String DATE = "Date"; 
    public static final String TIME = "Time"; 
    public static final String ESTIMATED_COMPLETION_DATE = "EstCompDate"; 
    public static final String ESTIMATED_COMPLETION_TIME = "EstCompTime"; 
    public static final String ACTUAL_COMPLETION_DATE = "ActCompDate"; 
    public static final String ACTUAL_COMPLETION_TIME = "ActCompTime"; 
    public static final String NOTIFY_DATE = "NotifyDate"; 
    public static final String NOTIFY_TIME = "NotifyTime" 
    public static final int DATABASE_VERSION = 1; 
    public static final String SUB_TASK_NUMBER = "sub_task_number"; 
    public static final String CREATE_TABLE = " CREATE TABLE " + TABLE_NAME + 
      "(" + UID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + TASK_NAME + " VARCHAR(250) UNIQUE," + CONTACT_NAME + " VARCHAR(250)," + CONTACT_NUMBER + " VARCHAR(250)," 
      + CONTACT_EMAIL + " VARCHAR(250)," + DESCRIPTION + " VARCHAR(250), " + REMARKS + " VARCHAR(250)," 
      + DATE + " VARCHAR(250)," + TIME + " VARCHAR(250)," + ESTIMATED_COMPLETION_DATE + " VARCHAR(250), " + ESTIMATED_COMPLETION_TIME + " VARCHAR(250), " 
      + ACTUAL_COMPLETION_DATE + " VARCHAR(250), " + ACTUAL_COMPLETION_TIME + " VARCHAR(250), " + NOTIFY_DATE + " VARCHAR(250), " + NOTIFY_TIME + " VARCHAR(250), ");"; 
    public static final String DROP_TABLE = "DROP TABLE IF EXISTS" + TABLE_NAME; 



    // private Context context; 

    public SQLiteHelper(Context context) { 
     // TODO Auto-generated constructor stub 

     super(context, DATABASE_NAME, null, DATABASE_VERSION); 
    } 


    // This is the method which is executed first 
    @Override 
    public void onCreate(SQLiteDatabase db) { 

     try { 
      db.execSQL(CREATE_TABLE); 

     } catch (SQLException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

    } 

    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     // TODO Auto-generated method stub 

     try { 
      db.execSQL(DROP_TABLE); 


     } catch (SQLException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     onCreate(db); 

    } 

} 

我已示於機器人,如何插入數據以及如何讀取數據創建數據庫的樣品的例子。你需要按照你的應用程序進行修改。內部類擴展SQLiteOpenHelper,並從主類訪問屬性。

希望這可以幫助你。

+0

如果這對你有幫助,那麼請投票接受答案。 – Keshav1234 2015-04-01 09:34:31