How to export sqlite to CSV in Python without being formatted as a list?

后端 未结 3 1378
别那么骄傲
别那么骄傲 2020-12-05 10:41

Here is what I currently have:

conn = sqlite3.connect(dbfile)
conn.text_factory = str ## my current (failed) attempt to resolve this
cur = conn.cursor()
data         


        
3条回答
  •  孤街浪徒
    2020-12-05 11:25

    Converting an sqlite database table to csv file can also be done directly using sqlite3 tools:

    >sqlite3 c:/sqlite/chinook.db
    sqlite> .headers on
    sqlite> .mode csv
    sqlite> .output data.csv
    sqlite> SELECT customerid,
       ...>        firstname,
       ...>        lastname,
       ...>        company
       ...>   FROM customers;
    sqlite> .quit
    

    The above sqlite3 commands will will create a csv file called data.csv in your current directory (of course this file can be named whatever you choose). More details are available here: http://www.sqlitetutorial.net/sqlite-export-csv/

提交回复
热议问题