historystamp
historystamp

Reputation: 1478

How to display db table names

The end goal is to modify Firefox cookies.sqlite db before starting Firefox.

At present, I want to display what tables are in the db. I copied the cookies.sqlite db to my desktop. I'm working with the db on my desktop.

This is my first time using sqlite. I copied some code, but I'm not able to read what tables are in the db. List of tables, db schema, dump etc using the Python sqlite3 API

Here is what I get in the terminal. I see the listing of the tables.

me $ ls cookies.sqlite 
cookies.sqlite
me $ sqlite3 cookies.sqlite
SQLite version 3.8.10.2 2015-05-20 18:17:19
Enter ".help" for usage hints.
sqlite> .tables
moz_cookies
sqlite> .exit
me $ # I run my python script
me $ ./sqlTutorial.bash 
SQLite version: 3.8.10.2
table records...
[]
more table records...
me $ cat dump.sql
BEGIN TRANSACTION;
COMMIT;
me $ 

python code. All I want to do is display the tables in the cookie.sqlite db.

#! /bin/python 

import sqlite3 as lite
import sys

con = lite.connect('cookie.sqlite')

with con:

    cur = con.cursor()    
    cur.execute('SELECT SQLITE_VERSION()')

    data = cur.fetchone()

    print ("SQLite version: %s" % data)

print ("table records...")

cursor = con.cursor()
cursor.execute("SELECT name FROM sqlite_master WHERE type='table';")
print(cursor.fetchall())

print ("more table records...")

with open('dump.sql', 'w') as f:
    for line in con.iterdump():
        f.write('%s\n' % line)

Upvotes: 0

Views: 2039

Answers (1)

laalto
laalto

Reputation: 152927

As you noticed, the database name is cookies.sqlite not cookie.sqlite.

Why didn't I get an error? I though python would error out on. con = lite.connect('cookie.sqlite')

Connecting to a sqlite database either opens an existing database file, or creates a new one if the database didn't exist.

Upvotes: 2

Related Questions