As of 2016-02-26, there will be no more posts for this blog. s/blog/pba/
Showing posts with label SQLite. Show all posts

$ sqlite3 places.sqlite 'select rev_host, sum(visit_count), sum(visit_count) * 100.0 / (select sum(visit_count) from moz_places) from moz_places group by rev_host order by sum(visit_count) desc limit 20;' | sed 's/\.|/ /;s/|/ /' | while read rev_host count percent ; do printf '%7d (%6.2f%%) %s\n' $count $percent $(echo "$rev_host" | rev) ; done
  18182 ( 23.25%) www.flickr.com
  14258 ( 18.23%) www.google.com
   4585 (  5.86%) draft.blogger.com
   3652 (  4.67%) mail.google.com
   2994 (  3.83%) appengine.google.com
   2973 (  3.80%) twitter.com
   1809 (  2.31%) localhost
   1683 (  2.15%) code.google.com
   1338 (  1.71%) friendfeed.com
   1175 (  1.50%) dfed
   1033 (  1.32%) groups.google.com
    991 (  1.27%) www.blogger.com
    764 (  0.98%) bbs.archlinux.org
    740 (  0.95%) www.google.com.tw
    658 (  0.84%) brps.appspot.com
    655 (  0.84%) flickr.com
    569 (  0.73%) typewar.com
    559 (  0.71%) www.last.fm
    552 (  0.71%) feedburner.google.com
    521 (  0.67%) www.youtube.com

I really need to quit visiting this page.

You can find places.sqlite in ~/.mozilla/firefox/<profile>/. The counts do not much what Page Info dialog reports, I have no idea what cause the differences. Page Info dialog gives smaller counts.

I don't see any place you can get a list, so I decided to dig into the database of Firefox. It's quite interesting that it has a reverse host field not a host field, the characters of host string in reverse. To get the order back, just pass it to rev.

Today I ran into a funny situation. I was using sqlite3, this is my first time to use it. I was just to execute a very simple SQL statement, that looks like,
connection.execute('INSERT INTO table_name (column_name) VALUES (?)', 'column_value')

If you have run similar statement, you probably won't forget the result of above statement. I have to admit that I didn't read sqlite3 with much care. I got an error message saying the statement has 1 parameter but I pass 12 parameters to it.

Even you haven't run into this, you probably can guess what the problem is. sqlite3 do len('column_value') without knowing its type. I tried to use another way, tuple, then I found out that I didn't know one fact of tuple.

Now, here is a list, please answer by reading this script:
#!/usr/bin/python

quiz = [
[],
{},
(),
'12345',
[12345],
['12345'],
(12345),
('12345'),
('12345',),
]

for q in quiz:
print '%10s ->' % repr(q),
try:
print len(q)
except Exception, e:
print e

Answers (select them to read):
[] -> 0
{} -> 0
() -> 0
'12345' -> 5
[12345] -> 1
['12345'] -> 1
12345 -> object of type 'int' has no len()
'12345' -> 5
('12345',) -> 1

So here is right way to do:
connection.execute('INSERT INTO table_name (column_name) VALUES (?)', ('column_value',))