2011-07-20 122 views
1

將應用程序中使用的URL存儲爲靜態的最佳方式是什麼?將URL存儲在數據庫中?

例如,我想爲每次打開應用程序時調用的提要保存URL。

什麼是最好的方法?

一個例子會很棒!

這裏是我的Feed.class

public class Feed implements Comparable<Feed> { 

static private HashMap<String, Feed> FEEDS=new HashMap<String,Feed>(); 

static{ 
    addFeed("Example", "http://www.example.xom/rssfedd/") 

} 
private String name; 
private String url; 


public static Feed addFeed(String name, String url){ 
    Feed result = new Feed(name,url); 
    addFeed(result); 
    return(result); 



} 
private static void addFeed(Feed feed){ 
    FEEDS.put(feed.getKey(), feed); 


} 
    public static ArrayList<Feed>getFeeds(){ 
     ArrayList<Feed> result = new ArrayList<Feed>(FEEDS.values()); 
     Collections.sort(result); 

     return(result); 
    } 

    private Feed getFeed(String key){ 
     return(FEEDS.get(key)); 

    } 
    private Feed(String name, String url){ 
     this.name = name; 
     this.url = url; 

    } 
    public String getKey(){ 
     return(toString()); 

    } 
    public String toString(){ 
     return(name); 
    } 
    public String getUrl(){ 
     return(url); 

    } 
public int compareTo(Feed another) { 

    return (toString().compareTo(another.toString())); 
} 
} 

=

回答

3

對於存儲在SQLite數據庫中,最好的方法是創建一個擴展SQLiteOpenHelper超類的類。在這個類中,您將定義要插入到數據庫中的函數,刪除,創建表格等等。 一個最簡單的方法是,如果你知道數據庫的結構(如果你只在一個tabel中存儲URL),你可以使用SQLiteBrowser創建數據庫。如果你有興趣,我可以給你代碼和鏈接到瀏覽器。祝你好運!!

UPDATE:

public class DBAdapter extends SQLiteOpenHelper { 

public static final String DATABASE_PATH = "data/data/com.Server_1/database"; 
public static final String DATABASE_NAME = "gps_date"; 

public static final String TABLE_1 = "user"; 
public static final String TABLE_2 = "route"; 
public static final String TABLE_3 = "data"; 

public static final String KEY_ROWID_1 = "_id"; 
public static final String KEY_USER = "user"; 

public static final String KEY_ROWID_2 = "_id"; 
public static final String KEY_SURSA = "sursa"; 
public static final String KEY_DESTINATIE = "destinatie"; 
public static final String KEY_DATE = "date"; 
public static final String KEY_USER_ID = "user_id"; 

public static final String KEY_ROWID_3 = "_id"; 
public static final String KEY_LONGITUDE = "longitude"; 
public static final String KEY_LATITUDE = "latitude"; 
public static final String KEY_SPEED = "speed"; 
public static final String KEY_TIME = "time"; 
public static final String KEY_USER_ID_ = "user_id"; 

public boolean t = false; 
public SQLiteDatabase db; 
private final Context myContext; 

public DBAdapter(Context context) { 
    super(context, DATABASE_NAME, null, 1); 
    this.myContext = context; 

} 

@Override 
public void onCreate(SQLiteDatabase db) { 
    createDB(); 
} 

@Override 
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
    Log.w("SqlHelper", "Upgrading database from version " + oldVersion 
      + " to " + newVersion + ", which will destroy all old data"); 
    onCreate(db); 
} 

public void createDatabase() { 
    createDB(); 
} 


private void createDB() { 

    boolean dbExist = DBExists(); 

    if (!dbExist) { 

     copyDBFromResource(); 

    } 

} 


private boolean DBExists() { 

    SQLiteDatabase db = null; 

    try { 
     String databasePath = DATABASE_PATH + DATABASE_NAME; 
     db = SQLiteDatabase.openDatabase(databasePath, null, 
       SQLiteDatabase.OPEN_READWRITE); 
     db.setLocale(Locale.getDefault()); 
     db.setLockingEnabled(true); 
     db.setVersion(1); 

    } catch (SQLiteException e) { 

     Log.e("SqlHelper", "database not found"); 

    } 

    if (db != null) { 

     db.close(); 

    } 

    return db != null ? true : false; 
} 



private void copyDBFromResource() { 

    InputStream inputStream = null; 
    OutputStream outStream = null; 
    String dbFilePath = DATABASE_PATH + DATABASE_NAME; 

    try { 

     inputStream = myContext.getAssets().open(DATABASE_NAME); 

     outStream = new FileOutputStream(dbFilePath); 

     byte[] buffer = new byte[1024]; 
     int length; 
     while ((length = inputStream.read(buffer)) > 0) { 
      outStream.write(buffer, 0, length); 
     } 

     outStream.flush(); 
     outStream.close(); 
     inputStream.close(); 

    } catch (IOException e) { 

     throw new Error("Problem copying database from resource file."); 

    } 

} 


public void openDataBase() throws SQLException { 

    String myPath = DATABASE_PATH + DATABASE_NAME; 
    db = SQLiteDatabase.openDatabase(myPath, null, 
      SQLiteDatabase.OPEN_READWRITE); 

} 

@Override 
public synchronized void close() { 

    if (db != null) 
     db.close(); 

    super.close(); 

} 


public long insertData1(String user) { 
    ContentValues initialValues = new ContentValues(); 
    initialValues.put(KEY_USER, user); 

    return db.insert(TABLE_1, null, initialValues); 
} 


public long insertData2(String sursa, String destinatie, String date, 
     int user_id) { 

    ContentValues initialValues = new ContentValues(); 
    initialValues.put(KEY_SURSA, sursa); 
    initialValues.put(KEY_DESTINATIE, destinatie); 
    initialValues.put(KEY_DATE, date); 
    initialValues.put(KEY_USER_ID, user_id); 
    return db.insert(TABLE_2, null, initialValues); 
} 

public long insertData3(String longitude, String latitude, float speed, 
     String time, int user_id) { 
    ContentValues initialValues = new ContentValues(); 
    initialValues.put(KEY_LONGITUDE, longitude); 
    initialValues.put(KEY_LATITUDE, latitude); 
    initialValues.put(KEY_SPEED, speed); 
    initialValues.put(KEY_TIME, time); 
    initialValues.put(KEY_USER_ID_, user_id); 

    return db.insert(TABLE_3, null, initialValues); 
} 


public Cursor getCursor(String prefix) { 

    String[] args = { prefix }; 

    String[] asColumnsToReturn = new String[] {KEY_ROWID_2, 
      KEY_SURSA, KEY_DESTINATIE, KEY_DATE, KEY_USER_ID}; 

    Cursor mCursor = db.query(TABLE_2, asColumnsToReturn, 
      "sursa like '' || ? || '%'", args, null, null, "sursa", null); 
    return mCursor; 
} 

public Cursor getCursor1(String prefix1) 
{ 
    Cursor mCursor=db.query(TABLE_1, new String[] {KEY_ROWID_1, KEY_USER},KEY_ROWID_1 + "=" + prefix1, 
      null,null,null,null); 
    if(mCursor != null) 


     if (mCursor != null) { 
      mCursor.moveToFirst(); 
     } 
    return mCursor; 

} 
public Cursor getCursor2(String TABLE_NAME, String prefix2) { 

    Cursor mCursor = db.query(TABLE_NAME, new String[] { KEY_ROWID_2, 
      KEY_SURSA, KEY_DESTINATIE, KEY_DATE, KEY_USER_ID }, KEY_USER_ID 
      + "=" + prefix2, null, null, null, null); 

    if (mCursor != null) { 
     mCursor.moveToFirst(); 
    } 
    return mCursor; 

} 

public Cursor getCursor3(String TABLE_NAME, String prefix) { 

    Cursor mCursor = db.query(TABLE_NAME, 
      new String[] { KEY_ROWID_3, KEY_LONGITUDE, KEY_LATITUDE, 
        KEY_SPEED, KEY_TIME, KEY_USER_ID }, KEY_USER_ID + "=" 
        + prefix, null, null, null, null); 
    return mCursor; 

} 


public Cursor getViteze(int prefix, String arg) { 

    Cursor mCursor = db.query(TABLE_3, 
      new String[] { KEY_ROWID_3, KEY_LONGITUDE, KEY_LATITUDE, 
        KEY_SPEED, KEY_TIME, KEY_USER_ID_ }, KEY_SPEED + ">" 
        + prefix + " AND " + KEY_USER_ID_ + "=" + arg, null, 
      null, null, KEY_SPEED + " DESC"); 
    return mCursor; 
} 


public boolean updateRoute(int rowId, String destinatie) { 
    ContentValues args = new ContentValues(); 

    args.put(KEY_DESTINATIE, destinatie); 

    return db.update(TABLE_2, args, KEY_ROWID_2 + "=" + rowId, null) > 0; 
} 

public Cursor getAllData() { 
    return db.query(TABLE_1, new String[] { KEY_ROWID_1, KEY_USER }, null, 
      null, null, null, null); 
} 


public Cursor getAllData2() { 
    return db.query(TABLE_2, new String[] { KEY_ROWID_2, KEY_SURSA, 
      KEY_DATE }, null, null, null, null, null); 
} 

public String[] getAllfromDB(String TABLE_NAME, String KEY) { 

    Cursor cursor = this.db.query(TABLE_NAME, new String[] { KEY }, null, 
      null, null, null, null); 

    if (cursor.getCount() > 0) { 
     String[] str = new String[cursor.getCount()]; 
     int i = 0; 
     while (cursor.moveToNext()) { 
      str[i] = cursor.getString(cursor.getColumnIndex(KEY)); 

      i++; 
     } 
     return str; 
    } else { 
     return new String[] {}; 
    } 
} 


public boolean deleteRoute(long rowID) { 

    return db.delete(TABLE_2, KEY_ROWID_2 + "=" + rowID, null) > 0; 
} 


public boolean deleteDateRoute(long rowID) { 

    return db.delete(TABLE_3, KEY_USER_ID_ + "=" + rowID, null) > 0; 
} 

這裏是鏈接:http://sourceforge.net/projects/sqlitebrowser/

Now, a few things about how to use this: 

1.Create the structure of the DB using the browser and put it in the assets folder of your application. 

2. You will have to change the variable `DATABASE_PATH` accoring to your application, this way: 

"data/data/`name of the package`/database" 
3.You will also have to change the name of your `DATABASE_NAME`. 
4.The code I sent you is done for three tabels, you will have to adapt it to your needs. 
Good luck! 
+0

代碼和鏈接將是偉大的! – yoshi24

+0

好的...只需要幾分鐘! – adrian

0

是您的問題: 「如何使用數據庫來存儲數據嗎?」 (在這種情況下,您真的會將這個問題與這些不必要的細節混淆),或者您只是在問「如何存儲URL?」。

如果後者...一個URL只是文本,那麼簡單地將它存儲爲一個文本字符串。

如果前者,http://www.vogella.de/articles/AndroidSQLite/article.html可能會有所幫助。

+0

您是否認爲我需要上述課程? – yoshi24

0

如果你只需要存儲一個網址,你並不需要一個數據庫。

使用Activity.getSharedPreferences()Context.openFileInput(filename)/Context.openFileOutput(filename, mode)應該沒問題。 您只需使用一些簡短的面向文件的代碼來存儲和檢索您的URL。