Can I generate XML Data from the SQLite Database?

2019-06-15 00:50发布

问题:

In such DBMS as Oracle or PostgreSQL there are functions for generating XML Data from the database: http://docs.oracle.com/cd/B19306_01/appdev.102/b14259/xdb13gen.htm or http://www.postgresql.org/docs/current/static/functions-xml.html#AEN15086

My question are there something similar in SQLite database? May be there are some additional tools, libraries or even standard tools for this purpose?

回答1:

You can also download SQlite database browser and then open your database. After that export each table to csv/html



回答2:

If you are using C#.NET there can be two ways for it.

  1. You can get the data into datasets and use the .WriteXml() method to write into the xml file.

    var sqc = new SqlConnection(ConfigurationManager.ConnectionStrings["AddsConnectionString"].ConnectionString);       
    
    sqc.Open();
    
    var q = "select * from adv";
    
    var sqd = new SqlDataAdapter(q, sqc);
    
    var ds = new DataSet();
    
    sqd.Fill(ds,"adv");
    
    ds.WriteXml("d:/data.xml");
    

Do not forget to include System.Xml

  1. OR else you can use Linq.


回答3:

you can download SQLite Studio. And directliy export the tables or database to xml file.enter image description here



回答4:

There exists a qt sqlite driver plugin, so if you decide to make the convertion program yourself, then you can easily do it through c++ and qt. Load driver, open database, extract whatever information you want with queries, output them to file in whatever format you want.

Otherwise, someone already made a convertion program: Video with exportation of db to whatever format

There are many possible ways to solve this.



回答5:

The answer is "yes and no." It isn't the same, but SQLite is actually pretty capable in this regard. You do have to roll your own solution however.

To understand how to do this you have to understand how SQLite differs from a database like PostgreSQL or Oracle. While the latter two are servers which communicate with clients over sockets (network or UNIX), SQLite is a library which communicates with the rest of the application using in-process memory. SQLite has no specific XML tools but it has infrastructure for you to create your own. Given that you didn't specify the language you are working with (and this makes the embedded db approach different) I will discuss how to think about it, and then provide pointers to the Python and C API documentation.

In PostgreSQL you extend SQL by providing functions which get run when they are called. These live in the backend and are instantiated when called. In SQLite, your process is the same as the backend so anything you have in your library is in the same process as SQLite. This means you can extend SQL as a language in arbitrary ways just by telling SQLite what function to call when it sees a function it doesn't understand. Thus you would build this up by defining function calls and then registering them with SQLite.

The Python documentation provides a complete working example with how to register a Python function with SQLite such that it generates an md5hash when called. The same approach can be used to generate whole XML documents. I am noting it first because this gives a good example of what you can do.

The SQLite documentation covers this in detail from a C perspective. From here you can define your own aggregates, your own functions, and much more. So you have all the building blocks you need for something like this, just the last mile is not provided.