File: sqlite3.py

package info (click to toggle)
python-petl 1.7.17-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 2,224 kB
  • sloc: python: 22,617; makefile: 109; xml: 9
file content (43 lines) | stat: -rw-r--r-- 962 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
from __future__ import division, print_function, absolute_import
import os


# fromsqlite3()
###############

os.remove('example.db')

import petl as etl
import sqlite3
# set up a database to demonstrate with
data = [['a', 1],
        ['b', 2],
        ['c', 2.0]]
connection = sqlite3.connect('example.db')
c = connection.cursor()
_ = c.execute('drop table if exists foobar')
_ = c.execute('create table foobar (foo, bar)')
for row in data:
    _ = c.execute('insert into foobar values (?, ?)', row)

connection.commit()
c.close()
# now demonstrate the petl.fromsqlite3 function
table = etl.fromsqlite3('example.db', 'select * from foobar')
table


# tosqlite3()
##############

os.remove('example.db')

import petl as etl
table1 = [['foo', 'bar'],
          ['a', 1],
          ['b', 2],
          ['c', 2]]
_ = etl.tosqlite3(table1, 'example.db', 'foobar', create=True)
# look what it did
table2 = etl.fromsqlite3('example.db', 'select * from foobar')
table2