android - 将 URL 存储在数据库中?

标签 android

将应用程序中使用的 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()));
}
}

=

最佳答案

要在 SQLite 数据库中存储,最好的方法是创建一个扩展 SQLiteOpenHelper 父类(super class)的类。在这个类中,您将定义要在数据库中插入、删除、创建表格等。 一种最简单的方法是-如果您知道数据库的结构(如果您只在一个表格中存储 URL),您可以使用 SQLiteBrowser 创建数据库。如果您有兴趣,我可以给您代码和浏览器的链接。很好好运!!

更新:

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!

关于android - 将 URL 存储在数据库中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6766156/

相关文章:

android - 如何像iPhone或EasyTouch应用程序一样进行Android辅助触摸?

c# - 对于 Xamarin c# ANDROID 的 ZXing.Mobile 有问题

android - 在android中创建自定义LockScreen

android - AppCompatButton 在 Android Marshmallow 上不显示

android - adb 服务器没有响应

android - 如何使用 SQL 查找特定时间范围内的条目总数?

java - 为什么要在发送 Intent 后调用 finish() 方法?

java - 接收从适配器到 Fragment 的回调

android - 权限不起作用

java - 水平 EditText 一次滚动一个字符