简体   繁体   中英

sqlite3 unknown database schema when using a checkpoint?

I am getting:

sqlite3.OperationalError: unknown database schema

conn = sqlite3.connect('tick.db', detect_types=sqlite3.PARSE_DECLTYPES, timeout=20,isolation_level=None)
# conn1 = sqlite3.connect('nifty_tick.db', detect_types=sqlite3.PARSE_DECLTYPES, timeout=20,isolation_level=None)

c = conn.cursor()
# c1 = conn1.cursor()

c.execute('PRAGMA journal_mode=wal')

def tick_entry1(inst,timestamp,ltp, bid, ask):
    if inst == 12335874:
        c.execute('INSERT INTO niftyfut (timestamp, close, bid, ask) VALUES (?,?,?,?)',
                  (timestamp, ltp, bid, ask))

def on_ticks(ws, ticks):
    global c, conn
    for t in ticks:
        if t['instrument_token'] == 12335874:
            timestamp = t['timestamp']
            ltp = t['last_price']
            inst = t['instrument_token']

            try:
                tick_entry1(inst,timestamp,ltp)
            except:
                # print('problem with db')
                pass
    c.execute('PRAGMA schema.wal_checkpoint(FULL);')

I have tried:

c.execute('PRAGMA schema.wal_checkpoint(FULL);')

and

c.execute('PRAGMA schema.wal_checkpoint(FULL)')

Edit

I just tried:

c.execute('PRAGMA wal_checkpoint(FULL)')

It seems to work. Now wondering if the following should be executed at the start.:

c.execute("PRAGMA wal_autocheckpoint = 0")

The message is saying that no database has a schema named schema.

In the documentation the schema is italic, which indicates that it is symbolic rather than an actual value and would be replaced with an appropriate value, the value is used to distinguish between attached databases.

eg if you used

ATTACH DATABASE the_database_path AS database2 /*<<<<<<<<<< THE SCHEMA is database2 */; 

then you could use

PRAGMA database2.wal_checkpoint(FULL); 

to checkpoint the attached database.

The initial database's schema is main but isn't required as it's the default if no schema is supplied.

Hence why c.execute('PRAGMA wal_checkpoint(FULL)') worked (as would c.execute('PRAGMA main.wal_checkpoint(FULL)') ). ie the are effectively the same.

If you use c.execute("PRAGMA wal_autocheckpoint = 0") then you will have to manage all the checkpointing as auto-checkpointing will be turned off (note that closing all the database connections checkpoints).

You may wish to consider:-

Disabling the automatic checkpoint mechanism. In its default configuration, SQLite will checkpoint the WAL file at the conclusion of any transaction when the WAL file is more than 1000 pages long. However, compile-time and run-time options exist that can disable or defer this automatic checkpoint. If an application disables the automatic checkpoint, then there is nothing to prevent the WAL file from growing excessively. Write-Ahead Logging - 6. Avoiding Excessively Large WAL Files

I'd suggest not using PRAGMA wal_autocheckpoint = 0 autocheckpointing does not hinder forced checkpointing, other than if a forced checkpoint happens after an auto checkpoint (and all pages are written) and nothing has been updated then it will do nothing (gracefully), otherwise more pages would be written to the database file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM