0

I would like to execute this query:

select datetime(date/1000,'unixepoch','localtime') as DATE, address as RECEIVED, body as BODY from sms;

And save it's output to a .csv file in a specified directory. Usually in Ubuntu terminal it is far more easy to manually give commands to save the output of the above query to a file. But i am not familiar with Python-sqlite3. I would like to know how do i execute this query and save it's output to custom directory in a .csv file. Please help me out !

1 Answers1

0

Quick and dirty:

import sqlite

db = sqlite.connect('database_file')
cursor = db.cursor()

cursor.execute("SELECT ...")
rows = cursor.fetchall() 

# Itereate rows and write your CSV

cursor.close()
db.close()

Rows will be a list with all matching records, which you can then iterate and manipulate into your csv file.

If you just want to make a csv file, look at the csv module. The following page should get you going https://docs.python.org/2/library/csv.html

You can also look at the pandas module to help create the file.

tryexceptpass
  • 529
  • 5
  • 14