Database problems - schema correct?

Okay, so I am one these people who suffer from recurrent database corruption problems. Those are possibly hardware related, according to what ppl say on forums (but the reason seems terribly difficult to trace).
Anyways, the repair routine for corrupted databases involves using the sqlite3 command line tool, you are supposed to run:

sqlite3 corrupted.db .recover > fixed.sql

Never worked in my case since many months, error message:

sql error: no such table: sqlite_dbpage (1)

I never understood what that might mean, not being familiar with SQL databases. I have now researched a bit, and one possibility seems to be that the db schema is not what the recover command expects.
The odd thing is that the error message comes up not only with the db that HA diagnosed to be corrupted, but ALSO when I run it on a copy of a freshly created db (autocreated by HA after the corrupt db was dumped and replaced automatically - I then shut down HA core, copied the NEW db to another computer, and ran the sqlite3 recover on the NEW db, getting the same error as on the corrupted dbs - which noone else of the ppl struggling with db corruption seemed to be getting).
So, what is wrong with my FRESH and non-corrupt db???
Below is the schema that I extracted - is there anything wrong with it?
The one thing that seems suspicious to me is :

CREATE TABLE sqlite_stat1(tbl,idx,stat);

because that shows up as an empty table. But that might be due to the table being freshly created.

Any insights?

Thanks a lot!

CREATE TABLE event_data (
	data_id INTEGER NOT NULL, 
	hash BIGINT, 
	shared_data TEXT, 
	PRIMARY KEY (data_id)
);
CREATE TABLE event_types (
	event_type_id INTEGER NOT NULL, 
	event_type VARCHAR(64), 
	PRIMARY KEY (event_type_id)
);
CREATE TABLE state_attributes (
	attributes_id INTEGER NOT NULL, 
	hash BIGINT, 
	shared_attrs TEXT, 
	PRIMARY KEY (attributes_id)
);
CREATE TABLE states_meta (
	metadata_id INTEGER NOT NULL, 
	entity_id VARCHAR(255), 
	PRIMARY KEY (metadata_id)
);
CREATE TABLE statistics_meta (
	id INTEGER NOT NULL, 
	statistic_id VARCHAR(255), 
	source VARCHAR(32), 
	unit_of_measurement VARCHAR(255), 
	unit_class VARCHAR(255), 
	has_mean BOOLEAN, 
	has_sum BOOLEAN, 
	name VARCHAR(255), 
	mean_type SMALLINT NOT NULL, 
	PRIMARY KEY (id)
);
CREATE TABLE recorder_runs (
	run_id INTEGER NOT NULL, 
	start DATETIME NOT NULL, 
	"end" DATETIME, 
	closed_incorrect BOOLEAN NOT NULL, 
	created DATETIME NOT NULL, 
	PRIMARY KEY (run_id)
);
CREATE TABLE migration_changes (
	migration_id VARCHAR(255) NOT NULL, 
	version SMALLINT NOT NULL, 
	PRIMARY KEY (migration_id)
);
CREATE TABLE schema_changes (
	change_id INTEGER NOT NULL, 
	schema_version INTEGER, 
	changed DATETIME NOT NULL, 
	PRIMARY KEY (change_id)
);
CREATE TABLE statistics_runs (
	run_id INTEGER NOT NULL, 
	start DATETIME NOT NULL, 
	PRIMARY KEY (run_id)
);
CREATE TABLE events (
	event_id INTEGER NOT NULL, 
	event_type CHAR(0), 
	event_data CHAR(0), 
	origin CHAR(0), 
	origin_idx SMALLINT, 
	time_fired CHAR(0), 
	time_fired_ts FLOAT, 
	context_id CHAR(0), 
	context_user_id CHAR(0), 
	context_parent_id CHAR(0), 
	data_id INTEGER, 
	context_id_bin BLOB, 
	context_user_id_bin BLOB, 
	context_parent_id_bin BLOB, 
	event_type_id INTEGER, 
	PRIMARY KEY (event_id), 
	FOREIGN KEY(data_id) REFERENCES event_data (data_id), 
	FOREIGN KEY(event_type_id) REFERENCES event_types (event_type_id)
);
CREATE TABLE states (
	state_id INTEGER NOT NULL, 
	entity_id CHAR(0), 
	state VARCHAR(255), 
	attributes CHAR(0), 
	event_id SMALLINT, 
	last_changed CHAR(0), 
	last_changed_ts FLOAT, 
	last_reported_ts FLOAT, 
	last_updated CHAR(0), 
	last_updated_ts FLOAT, 
	old_state_id INTEGER, 
	attributes_id INTEGER, 
	context_id CHAR(0), 
	context_user_id CHAR(0), 
	context_parent_id CHAR(0), 
	origin_idx SMALLINT, 
	context_id_bin BLOB, 
	context_user_id_bin BLOB, 
	context_parent_id_bin BLOB, 
	metadata_id INTEGER, 
	PRIMARY KEY (state_id), 
	FOREIGN KEY(old_state_id) REFERENCES states (state_id), 
	FOREIGN KEY(attributes_id) REFERENCES state_attributes (attributes_id), 
	FOREIGN KEY(metadata_id) REFERENCES states_meta (metadata_id)
);
CREATE TABLE statistics (
	id INTEGER NOT NULL, 
	created CHAR(0), 
	created_ts FLOAT, 
	metadata_id INTEGER, 
	start CHAR(0), 
	start_ts FLOAT, 
	mean FLOAT, 
	mean_weight FLOAT, 
	min FLOAT, 
	max FLOAT, 
	last_reset CHAR(0), 
	last_reset_ts FLOAT, 
	state FLOAT, 
	sum FLOAT, 
	PRIMARY KEY (id), 
	FOREIGN KEY(metadata_id) REFERENCES statistics_meta (id) ON DELETE CASCADE
);
CREATE TABLE statistics_short_term (
	id INTEGER NOT NULL, 
	created CHAR(0), 
	created_ts FLOAT, 
	metadata_id INTEGER, 
	start CHAR(0), 
	start_ts FLOAT, 
	mean FLOAT, 
	mean_weight FLOAT, 
	min FLOAT, 
	max FLOAT, 
	last_reset CHAR(0), 
	last_reset_ts FLOAT, 
	state FLOAT, 
	sum FLOAT, 
	PRIMARY KEY (id), 
	FOREIGN KEY(metadata_id) REFERENCES statistics_meta (id) ON DELETE CASCADE
);
CREATE TABLE sqlite_stat1(tbl,idx,stat);
CREATE INDEX ix_event_data_hash ON event_data (hash);
CREATE UNIQUE INDEX ix_event_types_event_type ON event_types (event_type);
CREATE INDEX ix_state_attributes_hash ON state_attributes (hash);
CREATE UNIQUE INDEX ix_states_meta_entity_id ON states_meta (entity_id);
CREATE UNIQUE INDEX ix_statistics_meta_statistic_id ON statistics_meta (statistic_id);
CREATE INDEX ix_recorder_runs_start_end ON recorder_runs (start, "end");
CREATE INDEX ix_statistics_runs_start ON statistics_runs (start);
CREATE INDEX ix_events_event_type_id_time_fired_ts ON events (event_type_id, time_fired_ts);
CREATE INDEX ix_events_context_id_bin ON events (context_id_bin);
CREATE INDEX ix_events_data_id ON events (data_id);
CREATE INDEX ix_events_time_fired_ts ON events (time_fired_ts);
CREATE INDEX ix_states_old_state_id ON states (old_state_id);
CREATE INDEX ix_states_context_id_bin ON states (context_id_bin);
CREATE INDEX ix_states_metadata_id_last_updated_ts ON states (metadata_id, last_updated_ts);
CREATE INDEX ix_states_last_updated_ts ON states (last_updated_ts);
CREATE INDEX ix_states_attributes_id ON states (attributes_id);
CREATE UNIQUE INDEX ix_statistics_statistic_id_start_ts ON statistics (metadata_id, start_ts);
CREATE INDEX ix_statistics_start_ts ON statistics (start_ts);
CREATE UNIQUE INDEX ix_statistics_short_term_statistic_id_start_ts ON statistics_short_term (metadata_id, start_ts);
CREATE INDEX ix_statistics_short_term_start_ts ON statistics_short_term (start_ts);

and was this the name of your corrupted DB ?, or you just copied and pasted this example ?

‘corrupted.db’ is just an example.
I used different, pretty arbitrary names for the corrupted DB, because most of the time I found it more practical to use a shorter name than what the system produces for the old DB upon replacing the old with the new one…
Don’t know if I ever used the standard HA DB name, or the DB name after HA appended the date and time string, should I have done that?

Im not sure what you exactly are saying here, or saying “you are doing”

Did you find any help in the official sqlite website ?

well, it doesn’t really matter to HA, it’s used for sqlite, to save ANALYZE data in

Also here i have no idea what you mean, nor why

Sorry, im blank again ( When does HA append a date & time-string ? ) you mean on the corrupted DB ?

You are aware that this makes one think, you just copy/pasted the command , but where ? and where do you have your “corrupted.db” ( Which im sure is not its Name when you copied it, or moved it from HA )

And Now this whole post , suddenly gives the impression that you are use to handling DB’s, Or are you just bullshitting me , with your “renaming” story, why renaming it ?

Why not make it simple, copy-paste the DB to a location where you have access to the sql-command, or try to fix it in optional sqlite-tools, i mean if it’s been happening frequently as you say , with constant corrupted DB’s, it would also make more sense trying to fix this issue

My final short answer, Yes the New DB_schema, HA creates after a DB-Crash/Corruption Is correct ( no i have not gone into details of your Posted DB_Scheme ) Just swiped through as i have reason to believe HA would somehow create a new “faulty schema”

Sorry if I caused confusion… I’ll try to explain again.
I do copy the corrupted DB, to the file name of which HA automatically appends Date and Time, to a different computer, execute the sqlite3 recover command on it, that command executes for a few seconds (so, yes, it finds the file), and then gives the output I pasted before
sql error: no such table: sqlite_dbpage (1)
I did consult the sqlite website and came out empty on that. The hint about the ‘sqlite_dbpage (1)’ pointing to an incorrect schema - I believe indicating that there is somehow inconsistent information about the number of tables that exists in the DB, or that .recover misreads such information - was found on another forum, to which I did not save the link, sorry.
As, unfortunately, the DB corruption is a recurring event, like about once a month, I have a degree of routine in copying the corrupted files around. That also means that corrupted DB files accumulate in the target directory, originally distinguished only by the DATE_TIME string that HA appended… difficult to distinguish when executing the sqlite3 recover thing, so I copied or renamed them. No particular system to that.

Others reported success with the sqlite3 recover thing, but it never worked for me, always failing with that error line. Then, I discovered that I get that error also if I execute sqlite3 recover on a fresh, intact DB!
Which led me to suspect that something in my individual HA setup, meaning some integration?, is somehow influencing how HA defines the DB schema for the new DB. I don’t know how and why that should be the case, but with that confusing error, I am still lost. That is why I’m asking. No intent of bullshitting anyone.

(And yes, of course it would be best if the DB got never corrupted. I’ll research memory fault issues on the machine etc very soon, I have tried excluding issues with the VM HA runs in, whatever… very annoying).

Do you have the latest version of sqlite3 ?
SQLite Download Page .
Choose “precompiled versions” for the os of your choice

Holy XXXX!

Downloaded sqlite tools ver 3.53 for Linux, ran the line with that, and it just works.

Before that, I had used sqlite3 ver 3.45, as it comes with the Linux Mint repository.

Thanks a lot, I would have never figured that out on my own.

To be honest, Thanx “Google Search” … did I just say that ? :laughing: , well might be The Only thing which justify Google on my Devices