18 |
# $Source$ |
# $Source$ |
19 |
# $Id$ |
# $Id$ |
20 |
|
|
21 |
from sqlite import connect |
# Pysqlite version 1. and 2. behaves quiet differently |
22 |
|
# Pysqlite uses a different paramstyle. The older version |
23 |
|
# support format and pyformat while pysqlite2 supports only qmark |
24 |
|
# and named. |
25 |
|
# The sqlite2 boolean variable is used to manage specific part of the code |
26 |
|
try: |
27 |
|
# Using SQLITE 2.x |
28 |
|
sqlite2 = True |
29 |
|
from pysqlite2 import dbapi2 as sqlite |
30 |
|
except ImportError: |
31 |
|
# Using SQLITE 1.x |
32 |
|
sqlite2 = False |
33 |
|
import sqlite |
34 |
|
|
35 |
|
|
36 |
from base import TitledObject |
from base import TitledObject |
37 |
|
|
53 |
|
|
54 |
def __init__(self, filename): |
def __init__(self, filename): |
55 |
self.filename = filename |
self.filename = filename |
56 |
self.conn = connect(filename) |
self.conn = sqlite.connect(filename) |
57 |
# Counters to produce unique table and column names |
# Counters to produce unique table and column names |
58 |
self.num_tables = 0 |
self.num_tables = 0 |
59 |
self.num_cols = 0 |
self.num_cols = 0 |
366 |
right_template = right.internal_name |
right_template = right.internal_name |
367 |
params = () |
params = () |
368 |
else: |
else: |
369 |
right_template = "%s" |
if sqlite2: |
370 |
|
right_template = "?" |
371 |
|
else: right_template = "%s" |
372 |
params = (right,) |
params = (right,) |
373 |
|
|
374 |
query = "SELECT id FROM %s WHERE %s %s %s ORDER BY id;" \ |
query = "SELECT id FROM %s WHERE %s %s %s ORDER BY id;" \ |
423 |
# longer than any of the column names |
# longer than any of the column names |
424 |
id_key = max([len(col.name) for col in self.columns]) * "x" |
id_key = max([len(col.name) for col in self.columns]) * "x" |
425 |
|
|
426 |
insert_template = "INSERT INTO %s (id, %s) VALUES (%%(%s)s, %s);" \ |
if sqlite2: |
427 |
|
insert_template = "INSERT INTO %s (id, %s) VALUES (%s, %s);" \ |
428 |
|
% (self.tablename, |
429 |
|
", ".join([col.internal_name |
430 |
|
for col in self.columns]), |
431 |
|
'?', |
432 |
|
", ".join(["?" for col in self.columns])) |
433 |
|
|
434 |
|
else: |
435 |
|
insert_template = "INSERT INTO %s (id, %s) VALUES (%%(%s)s, %s);" \ |
436 |
% (self.tablename, |
% (self.tablename, |
437 |
", ".join([col.internal_name |
", ".join([col.internal_name |
438 |
for col in self.columns]), |
for col in self.columns]), |
443 |
for i in range(table.NumRows()): |
for i in range(table.NumRows()): |
444 |
row = table.ReadRowAsDict(i) |
row = table.ReadRowAsDict(i) |
445 |
row[id_key] = i |
row[id_key] = i |
446 |
cursor.execute(insert_template, row) |
if sqlite2: |
447 |
|
params = [i] |
448 |
|
for col in self.columns: |
449 |
|
params.append(row[col.name]) |
450 |
|
cursor.execute(insert_template, params) |
451 |
|
else: |
452 |
|
cursor.execute(insert_template, row) |
453 |
self.db.conn.commit() |
self.db.conn.commit() |
454 |
|
|
455 |
|
|
689 |
|
|
690 |
def Width(self, col): |
def Width(self, col): |
691 |
return self.table.Width(col) |
return self.table.Width(col) |
692 |
|
|
693 |
|
def write_record(self, row, values): |
694 |
|
"""Write the values to the given row. |
695 |
|
|
696 |
|
This is a very experimental feature which doesn't work in all |
697 |
|
cases, so you better know what you're doing when calling this |
698 |
|
method. |
699 |
|
""" |
700 |
|
self.table.write_record(row, values) |