Mercurial > hg > egg-tcls
comparison convert_feeds_db.tcl @ 134:089b59f94067
Add creation and conversion utilities for SQLite3 format feeds databases.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Tue, 04 Jun 2013 13:22:59 +0300 |
parents | |
children | 4c51eeba993f |
comparison
equal
deleted
inserted
replaced
133:9f290e1ee738 | 134:089b59f94067 |
---|---|
1 #!/usr/bin/tclsh | |
2 # TCL script for converting Feed databases from | |
3 # flatfile format to SQLite3 database. | |
4 # | |
5 # Written by Matti 'ccr' Hamalainen <ccr@tnsp.org> | |
6 # (C) Copyright 2011 Tecnic Software productions (TNSP) | |
7 # | |
8 package require sqlite3 | |
9 source [file dirname [info script]]/util_convert.tcl | |
10 | |
11 ### Check commandline arguments | |
12 if {$argc != 2} { | |
13 puts "Usage: $argv0 <input_flat_file_db> <output_sqlite_db_file>" | |
14 exit 0 | |
15 } | |
16 | |
17 set db_input [lindex $argv 0] | |
18 set db_output [lindex $argv 1] | |
19 | |
20 ### Ask for confirmation | |
21 puts "Conversion of '$db_input' to SQLite database '$db_output'." | |
22 puts "NOTICE! This WILL destroy the current data in the target database!" | |
23 | |
24 if {![confirm_yesno "Proceed"]} { | |
25 exit 0 | |
26 } | |
27 | |
28 ### Open flatfile for reading | |
29 if {[catch {set fd [open $db_input r]} uerrmsg]} { | |
30 puts "Could not open file '$db_input' for reading: $uerrmsg" | |
31 exit 1 | |
32 } | |
33 | |
34 ### Open SQLite database, drop old tables, create new | |
35 open_db $db_output | |
36 | |
37 drop_table_feeds | |
38 create_table_feeds | |
39 | |
40 | |
41 ### Convert data | |
42 puts -nonewline "Converting database, please wait " | |
43 set nline 0 | |
44 seek $fd 0 start | |
45 | |
46 while {![eof $fd]} { | |
47 incr nline | |
48 gets $fd uline | |
49 set items [split $uline "½"] | |
50 if {[llength $items] == 4} { | |
51 set sql "INSERT INTO feed_data (feed,utime,url,title) VALUES ('[lescape $items 1]', [lindex $items 0], '[lescape $items 2]', '[lescape $items 3]')" | |
52 if {[catch {dbh eval $sql} uerrmsg]} { | |
53 puts "\nError ($nline): $uerrmsg on:\n$sql" | |
54 exit 15 | |
55 } | |
56 } | |
57 | |
58 if {[expr $nline % 10] == 1} { | |
59 puts -nonewline "." | |
60 flush stdout | |
61 } | |
62 } | |
63 | |
64 dbh close | |
65 close $fd | |
66 | |
67 puts "DONE." |