首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >将xml插入解析为SQLite db并在片段中显示数据

将xml插入解析为SQLite db并在片段中显示数据
EN

Stack Overflow用户
提问于 2019-01-08 02:43:30
回答 2查看 455关注 0票数 1

我分析了一个xml,它可以工作,但现在我想将数据存储在一个SQLite db中,因为这是每个电话的本地数据。我想在db中插入xml的数据,然后从db加载数据。我不知道如何创建一个SQLite db,它将检索数据,我可以保存那里的新条目。

这是我的代码Bookmark.class

Bookmark.class

代码语言:javascript
运行
AI代码解释
复制
public class Bookmark {
    String name, id, nativeUrl, searchUrl;
    int icon;
    int viewType;


    public String getName() { return name; }
    public void setName(String name) {
        this.name = name;
    }

    public int getIcon() { return icon; }
    public void setIcon(int icon) {
        this.icon = icon;
    }

    public String getId(){
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getNativeUrl() {
        return nativeUrl;
    }

    public void setNativeUrl(String nativeUrl) {
        this.nativeUrl = nativeUrl;
    }

    public String getSearchUrl() {
        return searchUrl;
    }

    public void setSearchUrl(String searchUrl) {
        this.searchUrl = searchUrl;
    }
    public int getViewType() {
        return viewType;
    }

    public void setViewType(int viewType) {
        this.viewType = viewType;
    }


    @Override
    public String toString() {
        return "Bookmark{" +
                "name='" + name + '\'' +
                ", icon='" + icon + '\'' +
                ", id='" + id + '\'' +
                ", nativeUrl='" + nativeUrl + '\'' +
                ", searchUrl='" + searchUrl + '\'' +
                '}';
    }

}

这是Adapter.class

Adapter.class

代码语言:javascript
运行
AI代码解释
复制
public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context context;
    ArrayList<Bookmark> arrayList = new ArrayList<>();
    public static final int ITEM_TYPE_ONE = 0;
    public static final int ITEM_TYPE_TWO = 1;


    public MyAdapter(Context context, ArrayList<Bookmark> arrayList) {
        this.context = context;
        this.arrayList = arrayList;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        View view = null;
        if (viewType == ITEM_TYPE_ONE) {
            view = LayoutInflater.from(context).inflate(R.layout.grid_item, parent, false);
            return new ViewHolder(view);
        } else if (viewType == ITEM_TYPE_TWO) {
            view = LayoutInflater.from(context).inflate(R.layout.add_bookmark, parent, false);
            return new ButtonViewHolder(view);
        }else {
            return  null;
        }

    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
        final int itemType = getItemViewType(position);
        if (itemType == ITEM_TYPE_ONE) {
            final ViewHolder viewHolder = (ViewHolder) holder;
            viewHolder.tvName.setText(arrayList.get(position).getName());
            viewHolder.tvIcon.setImageResource(arrayList.get(position).getIcon());
       //     viewHolder.tvId.setText(arrayList.get(position).getId());
            viewHolder.tvSearchUrl.setText(arrayList.get(position).getSearchUrl());
            viewHolder.tvNativeUrl.setText(arrayList.get(position).getNativeUrl());

        } else if (itemType == ITEM_TYPE_TWO) {
            ButtonViewHolder buttonViewHolder = (ButtonViewHolder) holder;
            buttonViewHolder.imgButton.setImageResource(arrayList.get(position).getIcon());
        }


    }

    @Override
    public int getItemViewType(int position) {
        // based on you list you will return the ViewType 
        if (arrayList.get(position).getViewType() == 0) {
            return ITEM_TYPE_ONE;
        } else {
            return ITEM_TYPE_TWO;
        }
    }

    @Override
    public int getItemCount() {
        return arrayList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        TextView tvName, tvId, tvSearchUrl, tvNativeUrl;

        ImageView tvIcon;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tvName = itemView.findViewById(R.id.textView);
            tvIcon = itemView.findViewById(R.id.image_view);
//            tvId = itemView.findViewById(R.id.tvId);
            tvSearchUrl = itemView.findViewById(R.id.tvSiteURL);
            tvNativeUrl = itemView.findViewById(R.id.tvNativeUrl);
            // tvName.setTextColor(Color.parseColor("#FFFFFF"));
    }
    }

    public class ButtonViewHolder extends RecyclerView.ViewHolder {


        ImageView imgButton;

        public ButtonViewHolder(@NonNull View itemView) {
            super(itemView);

            imgButton = itemView.findViewById(R.id.image_button_add);

            imgButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, ActivityChangeBookmark.class);
                    v.getContext().startActivity(intent);
                }
            });

        }
    }

这是Fragment.class,我向您展示了解析的xml

Fragment.class

代码语言:javascript
运行
AI代码解释
复制
public class FragmentBookmark extends Fragment {
    ArrayList<Bookmark> arrayList = new ArrayList<>();
    MyAdapter myAdapter;
    View paramView;
    RecyclerView myRecyclerView;
    private Context mContext;
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mContext = context;
    }
    @Nullable
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
       paramView = inflater.inflate(R.layout.bookmark, container, false);
        myRecyclerView =  paramView.findViewById(R.id.myRecyclerView);
        // myRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        myRecyclerView.setLayoutManager(new GridLayoutManager(mContext, 4));
        myRecyclerView.setHasFixedSize(true);
        myAdapter = new MyAdapter(mContext, arrayList);
        myRecyclerView.setAdapter(myAdapter);
        try {
            XmlResourceParser xpp = getResources().getXml(R.xml.bookmarks);
            while (xpp.getEventType() != XmlPullParser.END_DOCUMENT) {
                if (xpp.getEventType() == XmlPullParser.START_TAG) {
                    if (xpp.getName().equals("Bookmark")) {
                        Bookmark bookmark = new Bookmark();
                        bookmark.setName(xpp.getAttributeValue(null, "name"));
                        bookmark.setSearchUrl(xpp.getAttributeValue(null, "searchUrl"));
                        bookmark.setNativeUrl(xpp.getAttributeValue(null, "nativeUrl"));
                        int drawableResourceId = getResources().getIdentifier(xpp.getAttributeValue(null, "icon"),"drawable", mContext.getPackageName());
                        bookmark.setIcon(drawableResourceId);
                        bookmark.setViewType(0);
                        arrayList.add(bookmark);

                    }
                }
                xpp.next();
            }
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        myAdapter.notifyDataSetChanged();
        Bookmark bookmark = new Bookmark();
        bookmark.setViewType(1);
        bookmark.setIcon(R.drawable.add_new_bookmark_icon);
        arrayList.add(bookmark);
       return paramView;
    }
    }

这是.XML

代码语言:javascript
运行
AI代码解释
复制
<Bookmarks>
    <Bookmark name="Bing" hidden="" icon="bing" id="0" nativeUrl="" searchUrl="https://www.bing.com" />
    <Bookmark name="Google"  hidden="true" icon="google" id="1" nativeUrl="" searchUrl="https://www.google.com" />
    <Bookmark name="Youtube" hidden="" icon="youtube" id="2" nativeUrl="" searchUrl="http://m.youtube.com" />
    <Bookmark name="Facebook" hidden="" icon="facebook" id="3" nativeUrl="facebook://" searchUrl="https://m.facebook.com" />
    <Bookmark name="Twitter" hidden="" icon="twitter" id="4" nativeUrl="" searchUrl="https://mobile.twitte.com" />
    <Bookmark name="Instagram" hidden="" icon="instagram" id="5" nativeUrl="instagram://" searchUrl="https://instagram.com" />
    <Bookmark name="Gmail" hidden="" icon="gmail" id="6" nativeUrl="googlemail://" searchUrl="https://gmail.com" />
    <Bookmark name="Translate" hidden="" icon="google_translate" id="7" nativeUrl="" searchUrl="https://" />
    <Bookmark name="Amazon" hidden="" icon="amazon" id="8" nativeUrl="" searchUrl="https://www.amazon.com" />
    <Bookmark name="Wikipedia" hidden=""  icon="wiki" id="9"  searchUrl="http://en.m.wikipedia.org/w/index.php?title=Main_Page" />
    <Bookmark name="Weather" hidden="" icon="weathercom" id="10" searchUrl="http://weather.com" />
    <Bookmark name="eBay" hidden="" id="9" icon="ebay"  searchUrl="http://ebay.to/1VPVeAs" />
    <Bookmark name="Apple" id="10" icon="apple"  searchUrl="http://www.apple.com" hidden="true" />
</Bookmarks>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-01-08 12:24:20

要使用SQLite数据库,首先要设计它(确定要存储的数据,并从中确定要存储数据的表和列)。

从XML中可以看出,您希望存储以下数据:

  1. 名称作为字符串(它等同于SQLite的文本)
  2. 隐藏作为布尔值(SQLite没有布尔值的类型,因此可以使用整数)
  3. 图标作为文本
  4. id作为整数
  5. nativeurl作为文本
  6. 搜索作为文本

这些将是列,书签似乎是表名的自然选择(为了方便起见,假设单个表适合)。

理想情况下,您应该对数据和需求进行更深入的分析,例如约束和索引。然而,为了便于演示,假设是id (因为它可能意味着id创建了上述一组(一行))

总之,听起来您需要一个名为书签的表,其中有6个列,列为id (我们将称之为_id (有时在Android中是必需的))构成主键。如上所示的其他列。

要创建这样的表,您需要创建告诉SQLite创建表所需的SQL。以下各点适用:

代码语言:javascript
运行
AI代码解释
复制
CREATE TABLE IF NOT EXISTS bookmark (_id INTEGER PRIMARY KEY, name TEXT, hidden INTEGER, icon TEXT, nativeurl TEXT, searchurl TEXT);

但是,您不能只运行上面的操作。首先,您需要一个数据库,其中的表(数据库中可能有许多表)将驻留在其中。

在Android中创建数据库的典型方法(但不是唯一的方法)是使用SQLiteOpenHelper类,为此您必须创建一个子类。沿着class yourOpenHelper extends SQliteOpenHelper { ......... }的路线。

当第一次创建数据库时,需要重写两个方法 onCreate onUpgrade - onCreate运行(注意事项onCreate只在数据库的生存期内运行一次)。此时,数据库似乎没有表。因此,这通常是您创建表的地方。-当传递给超级构造函数的第四个参数增加时(这个值使用常数DBVERSION以下),onUpgragde就会运行。

对表名和列名使用常量,然后始终使用这些常量是非常明智的。从上面你可以得到(作为一个例子):

代码语言:javascript
运行
AI代码解释
复制
public class BookmarkDBHelper extends SQLiteOpenHelper {

    public static final String DBNAME = "bookmarks.db"; // The name of the database file
    public static final int DBVERSION = 1;  // The Database version

    public static final String TBL_BOOKMARK = "bookmark";
    public static final String COL_ID = BaseColumns._ID; // equates to _id
    public static final String COl_NAME = "name";
    public static final String COl_HIDDEN = "hidden";
    public static final String COL_ICON = "icon";
    public static final String COL_NATIVEURL = "nativeurl";
    public static final String COL_SEARCHURL = "searchurl";

    SQLiteDatabase mDB;

    public BookmarkDBHelper(Context context) {
        super(context, DBNAME, null, DBVERSION);
        // Forces creation of the database (if it doesn't already exist)
        // and stores it when the BookmarkDBHelpr is instantiated
        mDB = this.getWritableDatabase();
    }

    /**
     * This creates the table(s) NOTE only automatically runs once
     */
    @Override
    public void onCreate(SQLiteDatabase db) {

        // The SQL to be used to create the table
        String crt_bookmark_tbl_sql = "CREATE TABLE IF NOT EXISTS " + TBL_BOOKMARK + "(" +
                COL_ID + " INTEGER PRIMARY KEY, " +
                COl_NAME + " TEXT, " +
                COl_HIDDEN + " INTEGER, " +
                COL_ICON + " TEXT, " +
                COL_NATIVEURL + " TEXT," +
                COL_SEARCHURL + " TEXT" +
                ")";
        db.execSQL(crt_bookmark_tbl_sql); // CREATE THE TABLE

    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

    }

    /**
     * Adds a row
     */
    public long addBookMark(long id, String name, boolean hidden, String icon, String nativeurl, String searchurl) {
        ContentValues cv = new ContentValues();
        cv.put(COL_ID,id); // NOTE will not insert if id already exists.
        cv.put(COl_NAME,name);
        cv.put(COl_HIDDEN,hidden);
        cv.put(COL_ICON,icon);
        cv.put(COL_NATIVEURL,nativeurl);
        cv.put(COL_SEARCHURL,searchurl);
        // uses the convenience insert method that builds the SQL
        return mDB.insert(TBL_BOOKMARK,null,cv);
    }

    /**
     * Example of extracting data from the database
     */
    public void logAllBookmarkRows() {
        String hasval = " and has a value of ";
        String[] columns = new String[]{"*"};
        Cursor csr = mDB.query(TBL_BOOKMARK,columns,null,null,null,null,null);
        StringBuilder sb = new StringBuilder("Table ").append(TBL_BOOKMARK)
                .append(" has ")
                .append(String.valueOf(csr.getCount()))
                .append(" rows. The are :-");
        while (csr.moveToNext()) {
            sb.append("\n ROW ").append(String.valueOf(csr.getPosition() + 1));
            sb.append("\n\tCOLUMN ").append(COL_ID)
                    .append(hasval)
                    .append(String.valueOf(csr.getLong(csr.getColumnIndex(COL_ID))));
            sb.append("\n\tCOLUMN ").append(COl_NAME)
                    .append(hasval)
                    .append(csr.getString(csr.getColumnIndex(COl_NAME)));
            sb.append("\n\tCOLUMN ").append(COl_HIDDEN)
                    .append(hasval)
                    .append(String.valueOf(csr.getInt(csr.getColumnIndex(COl_HIDDEN)) > 0));
            sb.append("\n\tCOLUMN").append(COL_ICON)
                    .append(hasval)
                    .append(csr.getString(csr.getColumnIndex(COL_ICON)));
            sb.append("\n\tCOLUMN ").append(COL_NATIVEURL)
                    .append(hasval)
                    .append(csr.getString(csr.getColumnIndex(COL_NATIVEURL)));
            sb.append("\n\tCOLUMN ").append(COL_SEARCHURL)
                    .append(hasval)
                    .append(csr.getString(csr.getColumnIndex(COL_SEARCHURL)));
        }
        csr.close(); //<<<<< Should ALWAYS close a Cursor when done with it.
        Log.d("BOOKMARKDATA",sb.toString());
    }
}

在一项活动中你:-

  1. 创建传递上下文的上述类的实例。
  2. 使用实例化的BookmarkDBHelper对象的方法

例如:-

代码语言:javascript
运行
AI代码解释
复制
public class MainActivity extends AppCompatActivity {

    BookmarkDBHelper mDBhlpr; // Declare the mDBHlpr object

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mDBhlpr = new BookmarkDBHelper(this); // Instantiate mDBHlpr
        mDBhlpr.addBookMark(1,"Coogle",false,"coogle","www.coogle.notcom","https://www.coogle.notcom");
        mDBhlpr.addBookMark(2,"Bong",true,"bong","","https://www.bong.net");
        mDBhlpr.logAllBookmarkRows();
    }
}
  • 上面的代码将2行(第一次运行)添加到书签表中,然后使用logAllBookMarkRows从数据库中提取数据,将结果写入日志。

结果

代码语言:javascript
运行
AI代码解释
复制
2019-01-09 07:09:26.817 2112-2112/ptfc.populatetablefromcursor D/BOOKMARKDATA: Table bookmark has 2 rows. The are :-
     ROW 1
        COLUMN _id and has a value of 1
        COLUMN name and has a value of Coogle
        COLUMN hidden and has a value of false
        COLUMNicon and has a value of coogle
        COLUMN nativeurl and has a value of www.coogle.notcom
        COLUMN searchurl and has a value of https://www.coogle.notcom
     ROW 2
        COLUMN _id and has a value of 2
        COLUMN name and has a value of Bong
        COLUMN hidden and has a value of true
        COLUMNicon and has a value of bong
        COLUMN nativeurl and has a value of 
        COLUMN searchurl and has a value of https://www.bong.net

备注这仅仅是为了引导您进入原则上您可以创建SQLite数据库、保存数据和检索数据以便回答以下问题的阶段:

我不知道如何创建一个SQLite db,它将检索数据,我可以保存那里的新条目。

,您无疑需要对上面的内容进行调整,以适应,例如(但不限于),比如修改addBookmark方法,以将BookMark作为参数。

备注

如果第二次运行上述操作,它将工作并产生相同的输出,但这两行将不会被添加,因为id的行是相同的(_id整数主元表示一个UNQIUE约束)。将引发两个例外,但被困,日志将包括类似于:-

代码语言:javascript
运行
AI代码解释
复制
2019-01-09 07:30:51.736 2295-2295/? E/SQLiteDatabase: Error inserting name=Coogle icon=coogle searchurl=https://www.coogle.notcom _id=1 hidden=false nativeurl=www.coogle.notcom
    android.database.sqlite.SQLiteConstraintException: UNIQUE constraint failed: bookmark._id (code 1555 SQLITE_CONSTRAINT_PRIMARYKEY)
        at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method)
        at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:796)
        at android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:788)
        at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:86)
        at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1564)
        at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1433)
        at ptfc.populatetablefromcursor.BookmarkDBHelper.addBookMark(BookmarkDBHelper.java:70)
        at ptfc.populatetablefromcursor.MainActivity.onCreate(MainActivity.java:21)
        at android.app.Activity.performCreate(Activity.java:7136)
        at android.app.Activity.performCreate(Activity.java:7127)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2894)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3049)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1809)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6680)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
2019-01-09 07:30:51.738 2295-2295/? E/SQLiteDatabase: Error inserting name=Bong icon=bong searchurl=https://www.bong.net _id=2 hidden=true nativeurl=
    android.database.sqlite.SQLiteConstraintException: UNIQUE constraint failed: bookmark._id (code 1555 SQLITE_CONSTRAINT_PRIMARYKEY)
        at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method)
        at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:796)
        at android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:788)
        at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:86)
        at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1564)
        at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1433)
        at ptfc.populatetablefromcursor.BookmarkDBHelper.addBookMark(BookmarkDBHelper.java:70)
        at ptfc.populatetablefromcursor.MainActivity.onCreate(MainActivity.java:22)
        at android.app.Activity.performCreate(Activity.java:7136)
        at android.app.Activity.performCreate(Activity.java:7127)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2894)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3049)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1809)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6680)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)

附加内容

根据注释,您需要从数据库中提取行(请参阅logAllBookmarks如何获取数据)到ArrayList (即arrayList)中。

为了适应DB代码,BookMark类已更改为:-

代码语言:javascript
运行
AI代码解释
复制
public class Bookmark {
    String name, id, nativeUrl, searchUrl;
    long db_id; //<<<<<<<<< ADDED should really use long for id's
    String icon_name; //<<<<<<<<<< ADDED
    int icon;
    int viewType; //<<<<<<<<<< COLUMN HIDDEN in DB

    // ADDED as needed because empty contructor only exists by default if no other constructors exist
    public Bookmark() {
    }

    public Bookmark(long dbid, String name, String icon_name, String nativeUrl, String searchUrl, int hidden ) {

        this.db_id = dbid;
        this.id = String.valueOf(db_id);
        this.name = name;
        this.icon_name = icon_name;
        //<<<<<<<<<< ....... Shoud get icon id and set it here
        this.nativeUrl = nativeUrl;
        this.searchUrl = searchUrl;
        this.viewType = hidden;
    }

    //<<<<<<<<<< START Of NEW GETTERS AND SETTERS
    public void setDb_id(long db_id) {
        this.db_id = db_id;
    }

    public long getDb_id() {
        return db_id;
    }

    public void setIcon_name(String icon_name) {
        this.icon_name = icon_name;
    }

    public String getIcon_name() {
        return icon_name;
    }
    //<<<<<<<<<< END OF NEW GETTERS AND SETTERS


    public String getName() { return name; }
    public void setName(String name) {
        this.name = name;
    }

    public int getIcon() { return icon; }
    public void setIcon(int icon) {
        this.icon = icon;
    }

    public String getId(){
        return id;
    }

    public void setId(String id) {
        this.id = id;
        this.db_id = Integer.parseInt(id);
    }

    public String getNativeUrl() {
        return nativeUrl;
    }

    public void setNativeUrl(String nativeUrl) {
        this.nativeUrl = nativeUrl;
    }

    public String getSearchUrl() {
        return searchUrl;
    }

    public void setSearchUrl(String searchUrl) {
        this.searchUrl = searchUrl;
    }
    public int getViewType() {
        return viewType;
    }

    public void setViewType(int viewType) {
        this.viewType = viewType;
    }


    @Override
    public String toString() {
        return "Bookmark{" +
                "name='" + name + '\'' +
                ", icon='" + icon + '\'' +
                ", id='" + id + '\'' +
                ", nativeUrl='" + nativeUrl + '\'' +
                ", searchUrl='" + searchUrl + '\'' +
                '}';
    }
}
  • 备注见注释,因为您可能对viewType有一些问题

现在,添加到BookmarDBHelper.java,中的以下方法将从数据库返回书签的ArrayList:

代码语言:javascript
运行
AI代码解释
复制
public ArrayList<Bookmark> getAllBookmarks() {
    ArrayList<Bookmark> rv  = new ArrayList<>();
    Cursor csr = mDB.query(TBL_BOOKMARK,null,null,null,null,null,null);
    while (csr.moveToNext()) {
        rv.add(new Bookmark(
                csr.getLong(csr.getColumnIndex(COL_ID)),
                csr.getString(csr.getColumnIndex(COl_NAME)),
                csr.getString(csr.getColumnIndex(COL_ICON)),
                csr.getString(csr.getColumnIndex(COL_NATIVEURL)),
                csr.getString(csr.getColumnIndex(COL_SEARCHURL)),
                csr.getInt(csr.getColumnIndex(COl_HIDDEN))
        ));
    }
    return rv;
}

- Note if no rows exists then the returned ArrayList will have a size of 0.

例如使用(就像上面的活动一样) "-

代码语言:javascript
运行
AI代码解释
复制
    ArrayList<Bookmark> arrylist = mDBhlpr.getAllBookmarks();
    for (Bookmark b: arrylist) {
        Log.d("BOOKMARKFROMLIST",b.toString());
    }

结果如下:-

代码语言:javascript
运行
AI代码解释
复制
2019-01-09 12:30:44.663 1701-1701/ptfc.populatetablefromcursor D/BOOKMARKFROMLIST: Bookmark{name='Coogle', icon='0', id='1', nativeUrl='www.coogle.notcom', searchUrl='https://www.coogle.notcom'}
2019-01-09 12:30:44.665 1701-1701/ptfc.populatetablefromcursor D/BOOKMARKFROMLIST: Bookmark{name='Bong', icon='0', id='2', nativeUrl='', searchUrl='https://www.bong.net'}
票数 1
EN

Stack Overflow用户

发布于 2019-01-10 17:49:35

附加-使它在片段中工作(某种程度上)

如评论所述

我理解你,我已经让你的答案被接受,但你没有写代码,如何在我的片段中显示保存的书签。在片段中显示来自解析xml的数据和来自sqlite的数据。

下面的代码基于您的代码加载数据库并填充RecyclerView的数组列表。

Bookmark.java是按照您的Bookmar.java,而不是以前在问题中使用的。

使用的BookmarkDBHelper.java是从上面修改的(主要变化是getAllBookmarks已被激发),并且是:-

代码语言:javascript
运行
AI代码解释
复制
public class BookmarkDBHelper extends SQLiteOpenHelper {

    public static final String DBNAME = "bookmarks.db"; // The name of the database file
    public static final int DBVERSION = 1;  // The Database version

    public static final String TBL_BOOKMARK = "bookmark";
    public static final String COL_ID = BaseColumns._ID; // equates to _id
    public static final String COl_NAME = "name";
    public static final String COl_HIDDEN = "hidden";
    public static final String COL_ICON = "icon";
    public static final String COL_NATIVEURL = "nativeurl";
    public static final String COL_SEARCHURL = "searchurl";

    SQLiteDatabase mDB;

    public BookmarkDBHelper(Context context) {
        super(context, DBNAME, null, DBVERSION);
        // Forces creation of the database (if it doesn't already exist)
        // and stores it when the BookmarkDBHelpr is instantiated
        mDB = this.getWritableDatabase();
    }

    /**
     * This creates the table(s) NOTE only automatically runs once
     */
    @Override
    public void onCreate(SQLiteDatabase db) {

        // The SQL to be used to create the table
        String crt_bookmark_tbl_sql = "CREATE TABLE IF NOT EXISTS " + TBL_BOOKMARK + "(" +
                COL_ID + " INTEGER PRIMARY KEY, " +
                COl_NAME + " TEXT, " +
                COl_HIDDEN + " INTEGER, " +
                COL_ICON + " TEXT, " +
                COL_NATIVEURL + " TEXT," +
                COL_SEARCHURL + " TEXT" +
                ")";
        db.execSQL(crt_bookmark_tbl_sql); // CREATE THE TABLE

    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

    }

    /**
     * Adds a row
     */
    public long addBookMark(long id, String name, boolean hidden, String icon, String nativeurl, String searchurl) {
        ContentValues cv = new ContentValues();
        cv.put(COl_NAME,name);
        cv.put(COl_HIDDEN,hidden);
        cv.put(COL_ICON,icon);
        cv.put(COL_NATIVEURL,nativeurl);
        cv.put(COL_SEARCHURL,searchurl);
        // uses the convenience insert method that builds the SQL
        return mDB.insert(TBL_BOOKMARK,null,cv);
    }

    public ArrayList<Bookmark> getAllBookmarks() {
        ArrayList<Bookmark> rv  = new ArrayList<>();
        Cursor csr = mDB.query(TBL_BOOKMARK,null,null,null,null,null,null);
        while (csr.moveToNext()) {
            Bookmark b = new Bookmark();
            b.setId(csr.getString(csr.getColumnIndex(COL_ID)));
            b.setName(csr.getString(csr.getColumnIndex(COl_NAME)));
            b.setIcon(csr.getInt(csr.getColumnIndex(COL_ICON)));
            b.setViewType(csr.getInt(csr.getColumnIndex(COl_NAME)));
            b.setNativeUrl(csr.getString(csr.getColumnIndex(COL_NATIVEURL)));
            b.setSearchUrl(csr.getString(csr.getColumnIndex(COL_SEARCHURL)));
            rv.add(b);
        }
        return rv;
    }


    /**
     * Example of extracting data from the database
     */
    public void logAllBookmarkRows() {
        String hasval = " and has a value of ";
        String[] columns = new String[]{"*"};
        Cursor csr = mDB.query(TBL_BOOKMARK,columns,null,null,null,null,null);
        StringBuilder sb = new StringBuilder("Table ").append(TBL_BOOKMARK)
                .append(" has ")
                .append(String.valueOf(csr.getCount()))
                .append(" rows. The are :-");
        while (csr.moveToNext()) {
            sb.append("\n ROW ").append(String.valueOf(csr.getPosition() + 1));
            sb.append("\n\tCOLUMN ").append(COL_ID)
                    .append(hasval)
                    .append(String.valueOf(csr.getLong(csr.getColumnIndex(COL_ID))));
            sb.append("\n\tCOLUMN ").append(COl_NAME)
                    .append(hasval)
                    .append(csr.getString(csr.getColumnIndex(COl_NAME)));
            sb.append("\n\tCOLUMN ").append(COl_HIDDEN)
                    .append(hasval)
                    .append(String.valueOf(csr.getInt(csr.getColumnIndex(COl_HIDDEN)) > 0));
            sb.append("\n\tCOLUMN").append(COL_ICON)
                    .append(hasval)
                    .append(csr.getString(csr.getColumnIndex(COL_ICON)));
            sb.append("\n\tCOLUMN ").append(COL_NATIVEURL)
                    .append(hasval)
                    .append(csr.getString(csr.getColumnIndex(COL_NATIVEURL)));
            sb.append("\n\tCOLUMN ").append(COL_SEARCHURL)
                    .append(hasval)
                    .append(csr.getString(csr.getColumnIndex(COL_SEARCHURL)));
        }
        csr.close(); //<<<<< Should ALWAYS close a Cursor when done with it.
        Log.d("BOOKMARKDATA",sb.toString());
    }
}

MyAdapter.java (没有测试或更改,我跳过让RecyclerView工作,因为有太多的代码布局等是不可用的)。

FragmentBoomark.java

所有的工作都是在onCreateView onAttach方法中完成的,从而消除了的工作需求(所以它不工作,但是)。

有几个额外的方法被调用。请注意,有些是纯粹用于调试/测试的。添加的方法如下:-

  • loadBookMarksFromXML
    • 从bookmark.xml加载数据库(基本上是代码的副本,但如果DB 中没有行,则加载到DB first )。

  • buildBookmarkArrayListfromDB
    • 根据注释构建arraylist,更改DB,然后应该调用它,需要刷新RecyclerView (我认为是notifydatasetChanged,或者类似的notifydatasetChanged)。

您注意到,一些代码已经注释掉了XML解析(可能把这个注释掉了),另一些代码主要是为了不需要编写/猜测其他代码。

:-

代码语言:javascript
运行
AI代码解释
复制
public class FragmentBookmark extends Fragment {

    BookmarkDBHelper mDB;

    ArrayList<Bookmark> arrayList = new ArrayList<>();
    MyAdapter myAdapter;
    View paramView;
    RecyclerView myRecyclerView;
    private Context mContext;
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mContext = context;
        mDB = new BookmarkDBHelper(mContext);
        mDB.logAllBookmarkRows();
        loadBookMarksFromXML();
        buildBookmarkArrayListfromDB();
        mDB.logAllBookmarkRows();

    }
    @Nullable
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        paramView = inflater.inflate(R.layout.bookmark, container, false);
        myRecyclerView =  paramView.findViewById(R.id.myRecyclerView);
        myRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        myRecyclerView.setLayoutManager(new GridLayoutManager(mContext, 4));
        myRecyclerView.setHasFixedSize(true);
        myAdapter = new MyAdapter(mContext, arrayList);
        myRecyclerView.setAdapter(myAdapter);
        /* MOVED
        try {
            XmlResourceParser xpp = getResources().getXml(R.xml.bookmarks);
            while (xpp.getEventType() != XmlPullParser.END_DOCUMENT) {
                if (xpp.getEventType() == XmlPullParser.START_TAG) {
                    if (xpp.getName().equals("Bookmark")) {
                        Bookmark bookmark = new Bookmark();
                        bookmark.setName(xpp.getAttributeValue(null, "name"));
                        bookmark.setSearchUrl(xpp.getAttributeValue(null, "searchUrl"));
                        bookmark.setNativeUrl(xpp.getAttributeValue(null, "nativeUrl"));
                        int drawableResourceId = getResources().getIdentifier(xpp.getAttributeValue(null, "icon"),"drawable", mContext.getPackageName());
                        bookmark.setIcon(drawableResourceId);
                        bookmark.setViewType(0);
                        arrayList.add(bookmark);
                    }
                }
                xpp.next();
            }
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        */
        myAdapter.notifyDataSetChanged();
        Bookmark bookmark = new Bookmark();
        bookmark.setViewType(1);
        //bookmark.setIcon(R.drawable.add_new_bookmark_icon);
        arrayList.add(bookmark);
        return paramView;
    }


    private void loadBookMarksFromXML() {

        // MAY WISH TO ONLY DO THIS ONCE as bookmarks would be loaded OTHERWISE DELETE LINE BELOW
        if(DatabaseUtils.queryNumEntries(mDB.getWritableDatabase(),BookmarkDBHelper.TBL_BOOKMARK) > 0 ) return;
        try {
            XmlResourceParser xpp = getResources().getXml(R.xml.bookmarks);
            while (xpp.getEventType() != XmlPullParser.END_DOCUMENT) {
                if (xpp.getEventType() == XmlPullParser.START_TAG) {
                    if (xpp.getName().equals("Bookmark")) {
                        Bookmark bookmark = new Bookmark();
                        bookmark.setName(xpp.getAttributeValue(null, "name"));
                        bookmark.setSearchUrl(xpp.getAttributeValue(null, "searchUrl"));
                        bookmark.setNativeUrl(xpp.getAttributeValue(null, "nativeUrl"));
                        int drawableResourceId = getResources().getIdentifier(xpp.getAttributeValue(null, "icon"),"drawable", mContext.getPackageName());
                        bookmark.setIcon(drawableResourceId);
                        bookmark.setViewType(0);
                        if (bookmark.getId() == null) {
                            bookmark.setId("-1");
                        }
                        mDB.addBookMark(
                                Long.valueOf(bookmark.getId()),
                                bookmark.getName(),
                                bookmark.getViewType() > 0,
                                String.valueOf(bookmark.getIcon()),
                                bookmark.getNativeUrl(),
                                bookmark.getSearchUrl()
                        );
                    }
                }
                xpp.next();
            }
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        mDB.logAllBookmarkRows();
    }

    // CALL ME WHENEVER BOOKMARKS change
    private void buildBookmarkArrayListfromDB() {
        arrayList.clear();
        arrayList.addAll(mDB.getAllBookmarks());
    }
}

结果

  • (请注意,NPE末尾是createView运行时(正如前面所述,要根据猜测运行createView要做的事情太多))

:-

代码语言:javascript
运行
AI代码解释
复制
2019-01-11 13:00:58.405 8021-8021/so54090082.so54090082 D/BOOKMARKDATA: Table bookmark has 0 rows. The are :-
2019-01-11 13:00:58.415 8021-8021/so54090082.so54090082 D/BOOKMARKDATA: Table bookmark has 13 rows. The are :-
     ROW 1
        COLUMN _id and has a value of 1
        COLUMN name and has a value of Bing
        COLUMN hidden and has a value of false
        COLUMNicon and has a value of 0
        COLUMN nativeurl and has a value of 
        COLUMN searchurl and has a value of https://www.bing.com
     ROW 2
        COLUMN _id and has a value of 2
        COLUMN name and has a value of Google
        COLUMN hidden and has a value of false
        COLUMNicon and has a value of 0
        COLUMN nativeurl and has a value of 
        COLUMN searchurl and has a value of https://www.google.com
     ROW 3
        COLUMN _id and has a value of 3
        COLUMN name and has a value of Youtube
        COLUMN hidden and has a value of false
        COLUMNicon and has a value of 0
        COLUMN nativeurl and has a value of 
        COLUMN searchurl and has a value of http://m.youtube.com
     ROW 4
        COLUMN _id and has a value of 4
        COLUMN name and has a value of Facebook
        COLUMN hidden and has a value of false
        COLUMNicon and has a value of 0
        COLUMN nativeurl and has a value of facebook://
        COLUMN searchurl and has a value of https://m.facebook.com
     ROW 5
        COLUMN _id and has a value of 5
        COLUMN name and has a value of Twitter
        COLUMN hidden and has a value of false
        COLUMNicon and has a value of 0
        COLUMN nativeurl and has a value of 
        COLUMN searchurl and has a value of https://mobile.twitte.com
     ROW 6
        COLUMN _id and has a value of 6
        COLUMN name and has a value of Instagram
        COLUMN hidden and has a value of false
        COLUMNicon and has a value of 0
        COLUMN nativeurl and has a value of instagram://
        COLUMN searchurl and has a value of https://instagram.com
     ROW 7
        COLUMN _id and has a value of 7
        COLUMN name and has a value of Gmail
        COLUMN hidden and has a value of false
        COLUMNicon and has a value of 0
        COLUMN nativeurl and has a value of googlemail://
        COLUMN searchurl and has a value of https://gmail.com
     ROW 8
        COLUMN _id and has a value of 8
        COLUMN name and has a value of Translate
        COLUMN hidden and has a value of false
        COLUMNicon and has a value of 0
        COLUMN nativeurl and has a value of 
        COLUMN searchurl and has a value of https://
     ROW 9
        COLUMN _id and has a value of 9
        COLUMN name and has a value of Amazon
        COLUMN hidden and has a value of false
        COLUMNicon and has a value of 0
        COLUMN nativeurl and has a value of 
        COLUMN searchurl and has a value of https://www.amazon.com
     ROW 10
        COLUMN _id and has a value of 10
        COLUMN name and has a value of Wikipedia
        COLUMN hidden and has a value of false
        COLUMNicon and has a value of 0
        COLUMN nativeurl and has a value of null
        COLUMN searchurl and has a value of http://en.m.wikipedia.org/w/index.php?title=Main_Page
     ROW 11
        COLUMN _id and has a value of 11
        COLUMN name and has a value of Weather
        COLUMN hidden and has a value of false
        COLUMNicon and has a value of 0
        COLUMN nativeurl and has a value of null
        COLUMN searchurl and has a value of http://weather.com
     ROW 12
        COLUMN _id and has a value of 12
        COLUMN name and has a value of eBay
        COLUMN hidden and has a value of false
        COLUMNicon and has a value of 0
        COLUMN nativeurl and has a value of null
        COLUMN searchurl and has a value of http://ebay.to/1VPVeAs
     ROW 13
        COLUMN _id and has a value of 13
        COLUMN name and has a value of Apple
        COLUMN hidden and has a value of false
        COLUMNicon and has a value of 0
        COLUMN nativeurl and has a value of null
        COLUMN searchurl and has a value of http://www.apple.com
2019-01-11 13:00:58.418 8021-8021/so54090082.so54090082 D/BOOKMARKDATA: Table bookmark has 13 rows. The are :-
     ROW 1
        COLUMN _id and has a value of 1
        COLUMN name and has a value of Bing
        COLUMN hidden and has a value of false
        COLUMNicon and has a value of 0
        COLUMN nativeurl and has a value of 
        COLUMN searchurl and has a value of https://www.bing.com
     ROW 2
        COLUMN _id and has a value of 2
        COLUMN name and has a value of Google
        COLUMN hidden and has a value of false
        COLUMNicon and has a value of 0
        COLUMN nativeurl and has a value of 
        COLUMN searchurl and has a value of https://www.google.com
     ROW 3
        COLUMN _id and has a value of 3
        COLUMN name and has a value of Youtube
        COLUMN hidden and has a value of false
        COLUMNicon and has a value of 0
        COLUMN nativeurl and has a value of 
        COLUMN searchurl and has a value of http://m.youtube.com
     ROW 4
        COLUMN _id and has a value of 4
        COLUMN name and has a value of Facebook
        COLUMN hidden and has a value of false
        COLUMNicon and has a value of 0
        COLUMN nativeurl and has a value of facebook://
        COLUMN searchurl and has a value of https://m.facebook.com
     ROW 5
        COLUMN _id and has a value of 5
        COLUMN name and has a value of Twitter
        COLUMN hidden and has a value of false
        COLUMNicon and has a value of 0
        COLUMN nativeurl and has a value of 
        COLUMN searchurl and has a value of https://mobile.twitte.com
     ROW 6
        COLUMN _id and has a value of 6
        COLUMN name and has a value of Instagram
        COLUMN hidden and has a value of false
        COLUMNicon and has a value of 0
        COLUMN nativeurl and has a value of instagram://
        COLUMN searchurl and has a value of https://instagram.com
     ROW 7
        COLUMN _id and has a value of 7
        COLUMN name and has a value of Gmail
        COLUMN hidden and has a value of false
        COLUMNicon and has a value of 0
        COLUMN nativeurl and has a value of googlemail://
        COLUMN searchurl and has a value of https://gmail.com
     ROW 8
        COLUMN _id and has a value of 8
        COLUMN name and has a value of Translate
        COLUMN hidden and has a value of false
        COLUMNicon and has a value of 0
        COLUMN nativeurl and has a value of 
        COLUMN searchurl and has a value of https://
     ROW 9
        COLUMN _id and has a value of 9
        COLUMN name and has a value of Amazon
        COLUMN hidden and has a value of false
        COLUMNicon and has a value of 0
        COLUMN nativeurl and has a value of 
        COLUMN searchurl and has a value of https://www.amazon.com
     ROW 10
        COLUMN _id and has a value of 10
        COLUMN name and has a value of Wikipedia
        COLUMN hidden and has a value of false
        COLUMNicon and has a value of 0
        COLUMN nativeurl and has a value of null
        COLUMN searchurl and has a value of http://en.m.wikipedia.org/w/index.php?title=Main_Page
     ROW 11
        COLUMN _id and has a value of 11
        COLUMN name and has a value of Weather
        COLUMN hidden and has a value of false
        COLUMNicon and has a value of 0
        COLUMN nativeurl and has a value of null
        COLUMN searchurl and has a value of http://weather.com
     ROW 12
        COLUMN _id and has a value of 12
        COLUMN name and has a value of eBay
        COLUMN hidden and has a value of false
        COLUMNicon and has a value of 0
        COLUMN nativeurl and has a value of null
        COLUMN searchurl and has a value of http://ebay.to/1VPVeAs
     ROW 13
        COLUMN _id and has a value of 13
        COLUMN name and has a value of Apple
        COLUMN hidden and has a value of false
        COLUMNicon and has a value of 0
        COLUMN nativeurl and has a value of null
        COLUMN searchurl and has a value of http://www.apple.com
2019-01-11 13:00:58.434 8021-8021/so54090082.so54090082 D/AndroidRuntime: Shutting down VM
2019-01-11 13:00:58.436 8021-8021/so54090082.so54090082 E/AndroidRuntime: FATAL EXCEPTION: main
    Process: so54090082.so54090082, PID: 8021
    java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView$LayoutManager)' on a null object reference
        at so54090082.so54090082.ui.fragmentbookmark.FragmentBookmark.onCreateView(FragmentBookmark.java:53)

后续运行的结果(即先前加载的数据) :-

代码语言:javascript
运行
AI代码解释
复制
2019-01-11 13:09:29.363 8159-8159/so54090082.so54090082 D/BOOKMARKDATA: Table bookmark has 13 rows. The are :-
     ROW 1
        COLUMN _id and has a value of 1
        COLUMN name and has a value of Bing
        COLUMN hidden and has a value of false
        COLUMNicon and has a value of 0
        COLUMN nativeurl and has a value of 
        COLUMN searchurl and has a value of https://www.bing.com
..........
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54090082

复制
相关文章
python在sqlite中插入数据
python通过引入sqlite的包,就能够直接操作sqlite数据库 import sqlite3 import math cx=sqlite3.connect("mydatabase.sqli
py3study
2020/01/06
3.9K0
shell 往DB中插入数据脚本
用户5760343
2022/05/18
5390
shell 往DB中插入数据脚本
phpMySQL查询全部数据并在HTML表格中显示结果
mysqli_fetch_array() 函数从结果集中取得一行作为关联数组,或数字数组,或二者兼有。
哆哆Excel
2022/10/25
7.1K0
phpMySQL查询全部数据并在HTML表格中显示结果
玩转SQLite4:SQLite数据插入与查看
点击插入数据的图标,依次输出几条数据。注意id这一项在点击插入数据时,是自动填充的,这是因为之前在创建表时,对id字段勾选了“自增”。
xxpcb
2021/12/16
1.6K0
玩转SQLite4:SQLite数据插入与查看
Python使用SQLite插入大量数据
使用Python爬虫代理IP时,最先使用了sqlite作为存储ip数据库,sqlite简单、灵活、轻量、开源,和文件系统一样。而当大量插入爬取的数据时,出现了严重的耗时,查看一起资料后,发现:sqlite在每条insert都使用commit的时候,就相当于每次访问时都要打开一次文件,从而引起了大量的I/O操作,耗时严重。 下面是每次插入后,提交事务处理,每次插入的时间,单位是秒。
py3study
2020/01/09
3.5K0
将XML文档表示为DOM
%XML.Document类和%XML.Node类使可以将任意XML文档表示为DOM(文档对象模型)。然后,可以导航此对象并对其进行修改。还可以创建一个新的DOM并将其添加到其中。
用户7741497
2022/07/05
4750
网页中显示xml,直接显示xml格式的文件
第一种方法 使用<pre></pre>包围代码(在浏览器中测试不行啊,但是在富编辑器中又可以,怪); 使用<xmp></xmp>包围代码(官方不推荐,但是效果不错哦,在firefox和IE都可以); 使用<textarea></textarea>包围代码(在富编辑器和浏览器中均可以 ); <pre>不能在页面显示;<xmp>不错,可行;<textarea>是在文本域中显示,达不到想要的效果。 <xmp>     <div>对html的用法作个说明</div>     <div><span id="demo"
cloudskyme
2018/03/19
4.8K0
如何使用wifi_db将Aircrack-ng数据解析至SQLite数据库并提取有价值信息
 关于wifi_db  wifi_db是一款功能强大的数据解析脚本,该脚本可以将Aircrack-ng数据解析至一个SQLite数据库中,并提取出类似握手包、MGT识别信息、AP信息、客户端信息、探针信息、WPS信息和AP全局概览等有价值的数据。  功能介绍  1、显示隐藏网络信息; 2、显示已连接客户端及其各自AP的详细数据; 3、识别连接到AP的客户端探针,从而深入了解流氓AP的潜在安全风险; 4、提取握手信息,并在hashcat中使用以方便破解密码; 5、显示来自企业网络的身份信息,包括用于身份验证的
FB客服
2023/04/09
8720
如何使用wifi_db将Aircrack-ng数据解析至SQLite数据库并提取有价值信息
将数据压缩到图片并在前端做解析渲染
在做前后端数据交互的时候,你一定遇到过这样的问题:数据量大,尤其是在气象行业!在本文,讲解一种将数据压缩到图片并在前端实现数据的解析与展示的功能。
牛老师讲GIS
2019/08/14
7860
用Elasticsearch存储图片并在Kibana中显示
能用Elasticsearch来存储图片吗?有不少朋友都问过这个问题,Elasticsearch作为一个NoSQL数据库,一个搜索引擎,一个大数据存储系统,原则上来说,对于各种结构化,非结构化数据,文本类,非文本类数据都能够存储。即图片也是可以用来存储的,但现实中这种实际的操作方式是不常见的,因为对象存储等基础设施会是一个更低成本的选择。不过,考虑某些综合场景,比如,用户希望只搭建一套大数据系统来支撑不同的使用需求,那么Elasticsearch确实是比Hadoop生态这种包含非常多组件的系统要简单得多。
点火三周
2021/03/02
8.5K0
Python中XML数据结构详细解析
物联网应用过程中,设备采集数据后,一般通过终端采集器网关转发或web server服务打包成xml或json数据格式传输到数据中心或云平台,最后经数据解析、数据分析及数据可视化。开发环节涉及末端设备数据采集、数据转发、数据解析等流程。
用户8949263
2022/04/08
2.3K0
Python中XML数据结构详细解析
【Groovy】Xml 反序列化 ( 使用 XmlParser 解析 Xml 文件 | 删除 Xml 文件中的节点 | 增加 Xml 文件中的节点 | 将修改后的 Xml 数据输出到文件中 )
在 【Groovy】Xml 反序列化 ( 使用 XmlParser 解析 Xml 文件 | 获取 Xml 文件中的节点和属性 | 获取 Xml 文件中的节点属性 ) 博客基础上 , 删除 Xml 文件中的节点信息 ;
韩曙亮
2023/03/30
6.3K0
【Groovy】Xml 反序列化 ( 使用 XmlParser 解析 Xml 文件 | 删除 Xml 文件中的节点 | 增加 Xml 文件中的节点 | 将修改后的 Xml 数据输出到文件中 )
sqlite 获取最后插入id
SQLite数据库中的表均有一个特殊的rowid字段,它是一个不重复的64位有符号整数,默认起始值为1。rowid别名为oid或_rowid_,但在创建表的SQL声明语句中只能使用rowid作为关键字。如果在创建表的时候设置了类型为integer的具有自动增长性的主键,那么这时的主键相当于是rowid的别名。
随心助手
2020/06/09
6.3K0
关于批量插入:Python sqlite
   个人遇到了一个需要互操作 Access 与 sqlite 的项目,其中涉及了诸多 join 和 relation。最重要的是数据的互相流动,这个项目使用了 pypyodbc 与 sqlobject,不免涉及了批量操作的问题。我将详细记录 sqlite 中的批量操作问题。
py3study
2020/01/06
2.6K0
Java中解析XML文件
树结构,有助于更好地理解、掌握,代码易于编写,在解析过程中树结构是保存在内存中,方便修改
头发还在
2023/10/16
2980
用PHP解析XML为数组的方法
可扩展标记语言,标准通用标记语言的子集,一种用于标记电子文件使其具有结构性的标记语言。
魏杰
2022/12/23
1.1K0
android 读取本地数据库db文件(Android sqlite)
今天我们主要说 本地数据库sqlite这种方式,实现读取一个本地数据库db文件的功能。为了方便说明,我举个例子来讲:
全栈程序员站长
2022/09/03
3.6K0
android 读取本地数据库db文件(Android sqlite)
如何将xml转换成excel_java 解析xml
import org.apache.poi.hssf.usermodel.HSSFCell;
全栈程序员站长
2022/09/23
9070
如何将xml转换成excel_java 解析xml
点击加载更多

相似问题

解析XML并在MySQL表中插入数据

30

解析xml并在mysql表中插入数据

36

无法将数据插入到db sqLite

10

xml数据解析并在UITableview上显示

11

解析xml数据并在视图中显示

12
添加站长 进交流群

领取专属 10元无门槛券

AI混元助手 在线答疑

扫码加入开发者社群
关注 腾讯云开发者公众号

洞察 腾讯核心技术

剖析业界实践案例

扫码关注腾讯云开发者公众号
领券
社区富文本编辑器全新改版!诚邀体验~
全新交互,全新视觉,新增快捷键、悬浮工具栏、高亮块等功能并同时优化现有功能,全面提升创作效率和体验
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文