Search notes:

SQLite: Export and import

Loading «embedded data» with a Python script

The following example tries to demonstrate how data that is embedded in a Python script can be imported into an SQLite database using the Python module sqlite3.
#!/usr/bin/env python3

import re
import os
import sqlite3

dbFileName  = 'load-test.db'

if os.path.exists(dbFileName):
   os.remove(dbFileName)

db = sqlite3.connect(dbFileName)

cur = db.cursor()

cur.execute('''
create table data (
   num_1    integer not null,
   num_2    integer not null,
   val_1    text    not null,
   val_2    text    not null
)
'''
)

cur.execute('pragma synchronous  = off'   )
cur.execute('pragma journal_mode = memory')

cur.execute('begin transaction')

cur.executemany("insert into data(num_1, num_2, val_1, val_2) values (?,?,?,'constant text')", [
 #
 # make sure the individual inserted fields have the
 # correct data type:
 #
   [
      int(fields[0]),
      int(fields[1]),
          fields[2]
   ]
   for fields in [ re.split(' +', line.strip())
   for line in '''
   1   101  first
  22     2  second
 333   303  third
4444    44  fourth
'''.split('\n')
    if line            # With Python's peculiarty, the ''' ‥ ''' construct creates an empty line at the beginning which needs to be removed
]])

cur.execute('end transaction')

See also

The SQLite shell command .import.
Export and import a database with .dump.

Index

Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 8 attempt to write a readonly database in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php:51 Stack trace: #0 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(51): PDOStatement->execute(Array) #1 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(66): id_of(Object(PDO), 'uri', '/notes/developm...') #2 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(30): insert_webrequest_('/notes/developm...', 1788462965, '216.73.217.21', 'Mozilla/5.0 App...', NULL) #3 /home/httpd/vhosts/renenyffenegger.ch/httpsdocs/notes/development/databases/SQLite/import-export/index(95): insert_webrequest() #4 {main} thrown in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php on line 51